链接
operate.java
====================================================================================================
伪代码:operate.java
目的: 进行一系列操作检验个人银行账户管理系统
====================================================================================================
00:建立几个账目对象, 初始化对象:: new SavingsAccount(date, id, rate)
01:在不同时间进行不同金额的存钱和取钱, 调用对象方法:: sa.deposit(date, money) sa.withdraw(date, money);
02:在银行的计息日, 结算账户的年息, 调用对象方法:: sa.settle(90);
03:输出显示账户信息, 调用对象方法:: sa.show();
====================================================================================================
SavingsAccount.java
====================================================================================================
伪代码:SavingsAccount.java
目的:实现个人银行账户管理系统
====================================================================================================
成员:
方法:
====================================================================================================
record函数介绍
====================================================================================================
伪代码:record
使用: 当存款金额将要改变时,调用record函数,计算帮助计算到当前日期截止的存款按日累计值,同时记录到当前日期的余额按日累加之和,用于结算利息
====================================================================================================
00:调用accumulate(int date),更新accumulation用于计算年利息,
01:更新lastDate = date;
02:更新当前账户金额, 要求保留小数点后两位
03:打印显示
====================================================================================================
5 #21325302 5000.01 5000.01
25 #58320212 10000.01 10000.01
45 #21325302 5500.01 10500.02
60 #58320212 -3999.99 6000.02
90 #21325302 27.64 10527.66
90 #58320212 21.79 6021.81
#21325302 Balance: 10527.66
#58320212 Balance: 6021.81
分析:
结果有极小的差别是正常的,因为是在不同的语言下进行测试的结果
+::增加输出所有账户的总金额
====================================================================================================
伪代码:SavingsAccount.java
目的:实现个人银行账户管理系统
====================================================================================================
成员:
+::
方法:
****+::
====================================================================================================
学习并掌握java增添静态属性与方法
1 #21325302 is created
1 #58320212 is created
5 #21325302 5000.01 5000.01
25 #58320212 10000.01 10000.01
45 #21325302 5500.01 10500.02
60 #58320212 -3999.99 6000.02
90 #21325302 27.64 10527.66
90 #58320212 21.79 6021.81
#21325302 Balance: 10527.66
#58320212 Balance: 6021.81
Total16549.47
+:: 对日期的准确表达的要求-----设计Date类
+:: 对多账户的管理-------------构造SavingsAccount对象数组
+:: 对用户名选取的范围增加-----String类的id
====================================================================================================
伪代码:Date.java
目的:实现个人银行账户管理系统的日期表达
====================================================================================================
成员:
+::
方法:
****+::
====================================================================================================
SavingsAccount对象数组的构造
//建立几个账户
SavingsAccount[] accounts = new SavingsAccount[5];
accounts[0] = new SavingsAccount(date, "S3755217", 0.015);
accounts[1] = new SavingsAccount(date, "02342342", 0.015);
Date类构造函数
Date(int year, int month, int day){
if (day <= 0 || day > getMaxDay()||month>12||month<1){
System.out.println("Invalid date: ");
show();
System.out.println();
System.exit(1);
}
this.year = year;
this.month = month;
this.day = day;
int years = year - 1;
//计算天数
totalDays = years * 365 + years / 4 - years / 100 + years / 400
+ DAYS_BEFORE_MONTH[month - 1] + day;
if (isLeapYear() && month > 2) totalDays++;
}
2008-11-1
#S3755217 created
2008-11-1
#02342342 created
2008-11-5
#S3755217 5000.01 5000.01 salary
2008-11-25
#02342342 10000.01 10000.01 sell stock 0323
2008-12-5
#S3755217 5500.01 10500.02 salary
2008-12-20
#02342342 -3999.99 6000.02 buy a laptop
2009-1-1
#S3755217 17.77 10517.79 interest
#S3755217 Balance: 10517.79
2009-1-1
#02342342 13.2 6013.22 interest
#02342342 Balance: 6013.22
+:: 辅助计算利息的累加器,将accumulate设置为类方便利息的计算
+:: 父类Account类,SavingsAcount和CreditAccount为继承的子类
+:: 对CreditAccount的设计方式的理解
====================================================================================================
伪代码:***Account.java***
目的:实现个人银行账户父类Account
====================================================================================================
**成员:**
**+**::
- id
- balance
- total
**方法:
****+**::
- Account(final Date date, String id) //构造函数
- record(final Date date, double amount, String desc) //记录一笔帐
- error(final String msg) //报告错误信息
- getId()
- getBalance()
- getTotal()
- show() //显示账户信息
====================================================================================================
====================================================================================================
伪代码:***Accumulator.java***
目的:实现个人银行账户管理系统的辅助计算利息的累加器
====================================================================================================
**成员:**
**+**::
- lastDate
- value
- sum
**方法:
****+**::
- Accumulator(final Date date, double value) //构造函数
- getSum(final Date date) //获得到日期date的累加结果
- change(final Date date, double value) //在date将数值变更为value
- reset(final Date date, double value) //初始化,将日期变为date,数值变为value,累加器清零
====================================================================================================
//Credit.java取出现金
void withdraw(final Date date, double amount, final String desc){
if (amount - getBalance() > credit) {
error("not enough credit");
} else {
record(date, -amount, desc);
//getDebt()<=0,change是改变,把前一段时间的利息算好,又取钱了,更新的负债传给利息累加器
acc.change(date, getDebt());
}
}
2008-11-1
#S3755217created
2008-11-1
#02342342created
2008-11-1
#C5392394created
2008-11-5
#S3755217 5000.01 5000.01 salary
2008-11-15
#C5392394 -1999.99 -1999.99 buy a cell
2008-11-25
#02342342 10000.01 10000.01 sell stock 0323
2008-12-1
#C5392394 -15.99 -2015.98 interest
2008-12-5
#S3755217 5500.01 10500.02 salary
2008-12-20
#02342342 -3999.99 6000.02 buy a laptop
2009-1-1
#S3755217 17.77 10517.79 interest
2009-1-1
#02342342 13.2 6013.22 interest
2009-1-1
#C5392394 -31.24 -2047.22 interest
2009-1-1
#C5392394 -49.99 -2097.21 annual fee
#S3755217 Balance: 10517.79
#02342342 Balance: 6013.22
#C5392394 Balance: -2097.21
Available credit:7902.79
Total: 14433.800000000005
+:: 学会使用java三大特性之多态
+:: 用do…while和switch语句实现系统高级操作
+:: 关于java的虚函数
public static void main(String[] args) throws IndexOutOfBoundsException, IOException {
Date date = new Date(2008, 11, 1); //起始日期
//建立几个账户
SavingsAccount[] sa = new SavingsAccount[5];
sa[0] = new SavingsAccount(date, "S3755217", 0.015);
sa[1] = new SavingsAccount(date, "02342342", 0.015);
CreditAccount ca = new CreditAccount(date, "C5392394", 10000, 0.0005, 50);
Account[] accounts = {sa[0], sa[1], ca};
final int n = accounts.length; //账户总数
System.out.println("(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit");
char cmd;
do {
//显示日期和总金额
date.show();
System.out.println("\tTotal: " + getTotal() + "\tcommand> ");
int index, day;
double amount;
String desc;
Scanner sc;
cmd = (char) System.in.read();
switch (cmd) {
case 'd': //存入现金
sc = new Scanner(System.in);
index = sc.nextInt();
sc = new Scanner(System.in);
amount = sc.nextDouble();
sc = new Scanner(System.in);
desc = sc.next();
accounts[index].deposit(date, amount, desc);
break;
case 'w': //取出现金
sc = new Scanner(System.in);
index = sc.nextInt();
sc = new Scanner(System.in);
amount = sc.nextDouble();
sc = new Scanner(System.in);
desc = sc.next();
accounts[index].withdraw(date, amount, desc);
break;
case 's': //查询各账户信息
for (int i = 0; i < n; i++) {
System.out.println("[" + i + "]");
accounts[i].show();
System.out.println();
}
break;
case 'c': //改变日期
sc = new Scanner(System.in);
day = sc.nextInt();
if (day < date.getDay())
System.out.println("You cannot specify a previous day");
else if (day > date.getMaxDay())
System.out.println("Invalid day");
else
date = new Date(date.getYear(), date.getMonth(), day);
break;
case 'n': //进入下个月
if (date.getMonth() == 12)
date = new Date(date.getYear() + 1, 1, 1);
else
date = new Date(date.getYear(), date.getMonth() + 1, 1);
for (int i = 0; i < n; i++)
accounts[i].settle(date);
break;
}
} while (cmd != 'e');
}
略:因为操作性比较高,简单显示几项
2008-11-1
#S3755217created
2008-11-1
#02342342created
2008-11-1
#C5392394created
(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit
2008-11-1
Total: 0.0 command>
d
1
1000
sdf
2008-11-1
#02342342 1000.01 1000.01 sdf
2008-11-1
Total: 1000.01 command>
c
29
2008-11-29
Total: 1000.01 command>
n
2008-12-1
Total: 1000.01 command>
2008-12-1
Total: 1000.01 command>
n
2009-1-1
#02342342 2.51 1002.52 interest
2009-1-1
#C5392394 -49.99 -49.99 annual fee
2009-1-1
Total: 952.53 command>
2009-1-1
Total: 952.53 command>
n
2009-2-1
#C5392394 -0.77 -50.760000000000005 interest
2009-2-1
Total: 951.76 command>
2009-2-1
Total: 951.76 command>
n
2009-3-1
#C5392394 -0.71 -51.470000000000006 interest
2009-3-1
Total: 951.05 command>
2009-3-1
Total: 951.05 command>
+:: 使用java对象数组Collection
Collection c = new ArrayList();
//创建账户数组,元素个数为0
c.add(new Account);
更多操作见参考文章