问题如下:
(储蓄账户类)创建类 SavingsAccount。使用静态变量年利率来存储所有账户持有人的年利率。该类的每个对象都包含一个私有实例变量 SavingBalance,指示储蓄者当前的存款金额。提供方法 calculateMonthlyInterest 以通过将 SavingBalance 乘以 AnnualInterestRate 除以 12 来计算每月利息——这个利息应该添加到 Savings-Balance 中。提供一个静态方法 modifyInterestRate 将年利率设置为新值。编写一个程序来测试类 SavingsAccount。实例化两个 SavingAccount 对象,saver1 和 saver2,余额分别为 $2000.00 和 $3000.00。将年利率设置为 4%,然后计算 12 个月的每个月的利息并打印两个储户的新余额。接下来,将年利率设置为 5%,计算下个月的利息并打印两个储蓄者的新余额。
我解决了这一切,但余额并没有增加——它保持不变。它应该随着年利率的每次变化而增加(至少,这是我所理解的)。
class SavingsAccount
{
static double annualInterestRate;
private double savingsBalance;
public SavingsAccount(double balance)
{
savingsBalance = balance;
}
public double calculateMonthlyInterest()
{
return (savingsBalance*annualInterestRate)/12;
}
public static void modifyInterestRate(double rate)
{
annualInterestRate = rate;
}
public static double getannualInterestRate(){return annualInterestRate;}
public double getsavingsBalance(){return savingsBalance;}
}
public class SavingsTest
{
public static void main(String args[])
{
SavingsAccount saver1 = new SavingsAccount(2000.0);
SavingsAccount saver2 = new SavingsAccount(3000.0);
SavingsAccount.modifyInterestRate(4);
System.out.printf("Balance for Saver1 = %.2f\nBalance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance()+saver1.calculateMonthlyInterest(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());
SavingsAccount.modifyInterestRate(5);
System.out.printf("New Balance for Saver1 = %.2f\nNew Balance for Saver2 = %.2f\nInterest Rate = %.2f\n\n",saver1.getsavingsBalance(),saver2.getsavingsBalance(),SavingsAccount.getannualInterestRate());
}
}