用Java写个小程序:创建银行账号类SavingAccoun...2
发布网友
发布时间:2024-03-06 09:22
我来回答
共1个回答
热心网友
时间:2024-07-26 17:51
import java.util.Scanner;
public class SavingAccount {
public static double account = 3000;
public static double rate = 0.03;
public void update(double rate){
this.rate = rate;
}
//flag 用来计算年利息和月利息,flag等true计算月利息
public double calc(double account , double rate , boolean flag){
if(flag)
return account * rate / 12;
return account * rate;
}
static class Test{
public static void main(String[] args) {
System.out.println("原始存款:"+SavingAccount.account + " \n原始利率:" +SavingAccount.rate );
SavingAccount sa = new SavingAccount();
System.out.println("原始年利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,false));
System.out.println("原始月利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,true));
System.out.println("######请输入新的利率######");
Scanner scanner = new Scanner(System.in);
sa.update(scanner.nextDouble());
System.out.println("更新后的利率:"+SavingAccount.rate);
System.out.println("更新后的年利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,false));
System.out.println("更新后的月利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,true));
//更新后的存款怎么算? 你是多长时间更新一次?
System.out.println("更新后的存款:"+(SavingAccount.account+sa.calc(SavingAccount.account,SavingAccount.rate,false)));
}
}
}
不知道对不对,我理解的是这样的了