程序功能:
1. 通过年利率计算月利率(精度为小数点后4位,即精确到万分之XX)
2. 根据起初账户余额推算每个月末的账户余额(本金+利息)
注:上一个版本没有使用BigDecimal类处理金额计算问题,本版本优化为使用BigDecimal类。
参考文章:实体类-银行账户余额推算表(Savings Account Class),https://blog.csdn.net/hpdlzu80100/article/details/51864598
运行结果:
本年度年利率为百分之4.00,每月账户余额推算如下: 月份 储户1 储户2 1 2006.60 3009.90 2 2013.22 3019.83 3 2019.87 3029.80 4 2026.53 3039.80 5 2033.22 3049.83 6 2039.93 3059.89 7 2046.66 3069.99 8 2053.41 3080.12 9 2060.19 3090.29 10 2066.99 3100.48 11 2073.81 3110.71 12 2080.65 3120.98
下一年度年利率为为百分之5.00,每月账户余额推算如下: 月份 储户1 储户2 1 2087.52 3131.28 2 2094.41 3141.61 3 2101.32 3151.98 4 2108.25 3162.38 5 2115.21 3172.82 6 2122.19 3183.29 7 2129.19 3193.79 8 2136.22 3204.33 9 2143.27 3214.91 10 2150.34 3225.52 11 2157.44 3236.16 12 2164.56 3246.84 |
代码:
1. 实体类SavingAccountBigDecimal
import java.math.BigDecimal;
import java.math.RoundingMode;
//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 SavingAccountBigDecimal
{
public static BigDecimal annualInterestRate = new BigDecimal(Double.toString(0.04));
public static BigDecimal monthlyInterestRate = annualInterestRate.divide(new BigDecimal(Integer.toString(12)), 4,RoundingMode.HALF_UP);
private BigDecimal savingsBalance;
public SavingAccountBigDecimal(BigDecimal savingsBalance)
{
this.savingsBalance=savingsBalance;
}
public BigDecimal calculateMonthlyInterest(){ //计算月末账户余额(本金+利息)
savingsBalance = savingsBalance.multiply(monthlyInterestRate.add(BigDecimal.ONE));
return savingsBalance;
}
public static void setAnnualInterestRate(BigDecimal annualInterestRate){ //修改年利率
SavingAccountBigDecimal.annualInterestRate = annualInterestRate;
}
}
2. 测试类
import java.math.BigDecimal;
public class SavingAccountTest
{
public static void main(String[] args)
{
SavingAccountBigDecimal saver1 = new SavingAccountBigDecimal(BigDecimal.valueOf(2000.00));
SavingAccountBigDecimal saver2 = new SavingAccountBigDecimal(BigDecimal.valueOf(3000.00));
SavingAccountBigDecimal.setAnnualInterestRate(BigDecimal.valueOf(0.04));
System.out.printf("本年度年利率为百分之%.2f,每月账户余额推算如下:\n",
SavingAccountBigDecimal.annualInterestRate.multiply(BigDecimal.valueOf(100)));
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());
}
SavingAccountBigDecimal.setAnnualInterestRate(BigDecimal.valueOf(0.05));
System.out.printf("\n下一年度年利率为为百分之%.2f,每月账户余额推算如下:\n",
SavingAccountBigDecimal.annualInterestRate.multiply(BigDecimal.valueOf(100)));
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());
}
}
}