您的位置:首页 > 其它

实体类-银行账户余额推算表(Savings Account Class)

2016-07-08 23:37 363 查看
声明:金额计算未采用BigDecimal类。

代码如下:

package example;
//JHTP Exercise 8.5: Savings Account Class
//by pandenghuang@163.com
/**(Savings Account Class) Create class SavingsAccount. Use a static
* variable annualInterestRate to store the annual interest rate for
* all account holders. Each object of the class contains a private
* instance variable savingsBalance indicating the amount the saver
* currently has on deposit.Provide method calculateMonthlyInterest
* to calculate the monthly interest by multiplying the savingsBalance
* by annualInterestRate divided by 12—this interest should be added
* to savings-Balance. Provide a static method modifyInterestRate
* that sets the annualInterestRate to a new value. Write a program
* to test class SavingsAccount. Instantiate two savingsAccount objects,
* saver1 and saver2, with balances of $2000.00 and $3000.00, respectively.
* Set annualInterestRate to 4%, then calculate the monthly interest for
* each of 12 months and print the new balances for both savers. Next,
* set the annualInterestRate to 5%, calculate the next month’s interest
*  and print the new balances for both savers.
*/

class SavingAccount
{
public static double annualInterestRate;
private double savingsBalance;

public SavingAccount(double savingsBalance)
{
this.savingsBalance=savingsBalance;
}

public double calculateMonthlyInterest(){
savingsBalance+=savingsBalance*annualInterestRate/12;
return savingsBalance;
}

public static void modifyInterestRate(double annualIR){
annualInterestRate=annualIR;
}

}

public class SavingAccountTest
{
public static void main(String[] args)
{
SavingAccount saver1 = new SavingAccount(2000.00);
SavingAccount saver2 = new SavingAccount(3000.00);

SavingAccount.modifyInterestRate(0.04);
System.out.printf("本年度年利率为百分之%.2f,每月账户余额推算如下:\n",
100*SavingAccount.annualInterestRate);
System.out.println("月份\t储户1\t\t储户2");
for(int i=1;i<=12;i++){
System.out.printf("%d\t%.2f\t\t%.2f\n",i,saver1.calculateMonthlyInterest(),
saver2.calculateMonthlyInterest());
}

SavingAccount.modifyInterestRate(0.05);
System.out.printf("\n下一年度年利率为为百分之%.2f,每月账户余额推算如下:\n",
100*SavingAccount.annualInterestRate);
System.out.println("月份\t储户1\t\t储户2");
for(int i=1;i<=12;i++){
System.out.printf("%d\t%.2f\t\t%.2f\n",i,saver1.calculateMonthlyInterest(),
saver2.calculateMonthlyInterest());
}

}
}


运行结果:

本年度年利率为百分之4.00,每月账户余额推算如下:

月份 储户1 储户2

1 2006.67 3010.00

2 2013.36 3020.03

3 2020.07 3030.10

4 2026.80 3040.20

5 2033.56 3050.33

6 2040.33 3060.50

7 2047.14 3070.70

8 2053.96 3080.94

9 2060.81 3091.21

10 2067.68 3101.51

11 2074.57 3111.85

12 2081.48 3122.22

下一年度年利率为为百分之5.00,每月账户余额推算如下:

月份 储户1 储户2

1 2090.16 3135.23

2 2098.86 3148.30

3 2107.61 3161.42

4 2116.39 3174.59

5 2125.21 3187.82

6 2134.07 3201.10

7 2142.96 3214.44

8 2151.89 3227.83

9 2160.85 3241.28

10 2169.86 3254.78

11 2178.90 3268.35

12 2187.98 3281.96

测试数据:

与Excel中的计算结果一致。

20162016
MonthAccount1Account2MonthAccount1Account2
DisplayDisplayDisplayDisplay
12006.67301012090.163135.23
22013.363020.0322098.863148.3
32020.073030.132107.613161.42
42026.83040.242116.393174.59
52033.563050.3352125.213187.82
62040.333060.562134.073201.1
72047.143070.772142.963214.44
82053.963080.9482151.893227.83
92060.813091.2192160.853241.28
102067.683101.51102169.863254.78
112074.573111.85112178.93268.35
122081.483122.22122187.983281.96

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: