代码展示:
package experimentWork.bankAccount;
class SavingAccount {
private int id; //账号
private double balance; //余额
private double rate; //存款的利率
private int lastDate; //上次变更的时期
private double accumulation; //余额按日累加之和
//构造函数
public SavingAccount(int _data, int _id, double _rate) {
lastDate = _data;
id = _id;
rate = _rate;
System.out.println(_data + "\t#" + id + " is created");
}
//存入现金
public void deposit(int _data, double _amount){
record(_data, _amount);
}
//取出现金
public void withdraw(int _data, double _amount){
if(_amount > balance){
System.out.println("Error: not enough money");
} else{
record(_data, -_amount);
}
}
//结算利息,每年1月1日调用一次该函数
public void settle(int _data){
double interest = accumulata(_data) * rate /365; //计算年息
if(interest != 0){
record(_data, interest);
}
accumulation = 0;
}
public void show(){
System.out.println("#" + id + "\tBalance:" + balance);
}
//记录一笔账,data为日期,amount为金额,desc为说明
private void record(int data, double amount) {
accumulation = accumulata(data);
lastDate = data;
amount = Math.floor(amount * 100 + 0.5) / 100; //未完成 保留小数点后两位
balance += amount;
System.out.println(data + "\t#" + id + "\t" + amount + "\t" + balance);
}
//获得到指定日期为止的存款金额按日累积值
final double accumulata(int data) {//const
return accumulation + balance * (data - lastDate);
}
}
public class part_4_9 {
public static void main(String args[]){
//建立几个账户
SavingAccount sa0 = new SavingAccount(1, 21325302, 0.015);
SavingAccount sa1 = new SavingAccount(1, 58320212, 0.015);
//几笔账目
sa0.deposit(5, 5000);
sa1.deposit(25, 10000);
sa0.deposit(45, 5500);
sa1.withdraw(60, 4000);
//开户后第99天到了银行的计息日,结算所有账户信息
sa0.settle(90);
sa1.settle(90);
//输出各个账户信息
sa0.show();
System.out.println();
sa1.show();
System.out.println();
}
}