package SavingsAccount02;
public class SavingsAccount {
private int id; //账号
private double balance; //余额
private double rate; //存款的年利率
private int lastDate; //上次变更余额的时期
private double accumulation; //余额按日累加之和
private static double total; //所有账户的总金额
//记录一笔帐,date为日期,amount为金额,desc为说明
private void record(int date, double amount){
accumulation = accumulate(date);
lastDate = date;
amount = Math.floor(amount * 100 + 0.5) / 100; //保留小数点后两位
balance += amount;
total += amount;
System.out.println(date+"\t#"+ id +"\t" +amount + "\t" +balance);
}
//获得到指定日期为止的存款金额按日累积值
private final double accumulate(int date) {
return accumulation + balance * (date - lastDate);
}
public SavingsAccount(int date, int id, double rate){
this.id=id;
this.rate=rate;
lastDate=date;
accumulation=0;
balance=0;
System.out.println(date+"\t#"+id+" is created");
}
public final int getId() { return id; }
public final double getBalance() { return balance; }
public final double getRate() { return rate; }
public static double getTotal() { return total; }
//存入现金
public void deposit(int date, double amount){
record(date,amount);
}
//取出现金
public void withdraw(int date, double amount){
if (amount > getBalance())
System.out.println("Error: not enough money");
else
record(date, -amount);
}
//结算利息,每年1月1日调用一次该函数
public void settle(int date){
double interest = accumulate(date) * rate / 365; //计算年息
if (interest != 0)
record(date, interest);
accumulation = 0;
}
//显示账户信息
public final void show(){
System.out.println("#" +id + "\tBalance:" + balance);
}
public static void main(String args[]){
SavingsAccount sa0=new SavingsAccount(1, 21325302, 0.015);
SavingsAccount sa1=new SavingsAccount(1, 58320212, 0.015);
sa0.deposit(5, 5000);
sa1.deposit(25, 10000);
sa0.deposit(45, 5500);
sa1.withdraw(60, 4000);
sa0.settle(90);
sa1.settle(90);
sa0.show();
System.out.println(" ");
sa1.show();
System.out.println(" ");
System.out.println("Total: " + sa1.getTotal());
}
}
1 #21325302 is created
1 #58320212 is created
5 #21325302 5000.0 5000.0
25 #58320212 10000.0 10000.0
45 #21325302 5500.0 10500.0
60 #58320212 -4000.0 6000.0
90 #21325302 27.64 10527.64
90 #58320212 21.78 6021.78
#21325302 Balance:10527.64
#58320212 Balance:6021.78
Total: 16549.42
public class SavingsAccount {
private int id; //账号
private double balance; //余额
private double rate; //存款的年利率
private int lastDate; //上次变更余额的时期
private double accumulation; //余额按日累加之和
private static double total; //所有账户的总金额
//记录一笔帐,date为日期,amount为金额,desc为说明
private void record(int date, double amount){
accumulation = accumulate(date);
lastDate = date;
amount = Math.floor(amount * 100 + 0.5) / 100; //保留小数点后两位
balance += amount;
total += amount;
System.out.println(date+"\t#"+ id +"\t" +amount + "\t" +balance);
}
//获得到指定日期为止的存款金额按日累积值
private final double accumulate(int date) {
return accumulation + balance * (date - lastDate);
}
public SavingsAccount(int date, int id, double rate){
this.id=id;
this.rate=rate;
lastDate=date;
accumulation=0;
balance=0;
System.out.println(date+"\t#"+id+" is created");
}
public final int getId() { return id; }
public final double getBalance() { return balance; }
public final double getRate() { return rate; }
public static double getTotal() { return total; }
//存入现金
public void deposit(int date, double amount){
record(date,amount);
}
//取出现金
public void withdraw(int date, double amount){
if (amount > getBalance())
System.out.println("Error: not enough money");
else
record(date, -amount);
}
//结算利息,每年1月1日调用一次该函数
public void settle(int date){
double interest = accumulate(date) * rate / 365; //计算年息
if (interest != 0)
record(date, interest);
accumulation = 0;
}
//显示账户信息
public final void show(){
System.out.println("#" +id + "\tBalance:" + balance);
}
public static void main(String args[]){
SavingsAccount sa0=new SavingsAccount(1, 21325302, 0.015);
SavingsAccount sa1=new SavingsAccount(1, 58320212, 0.015);
sa0.deposit(5, 5000);
sa1.deposit(25, 10000);
sa0.deposit(45, 5500);
sa1.withdraw(60, 4000);
sa0.settle(90);
sa1.settle(90);
sa0.show();
System.out.println(" ");
sa1.show();
System.out.println(" ");
System.out.println("Total: " + sa1.getTotal());
}
}
1 #21325302 is created
1 #58320212 is created
5 #21325302 5000.0 5000.0
25 #58320212 10000.0 10000.0
45 #21325302 5500.0 10500.0
60 #58320212 -4000.0 6000.0
90 #21325302 27.64 10527.64
90 #58320212 21.78 6021.78
#21325302 Balance:10527.64
#58320212 Balance:6021.78
Total: 16549.42