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

本文通过Java程序模拟了两个储蓄账户在不同年利率条件下的月度余额变化情况,验证了利息计算的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:金额计算未采用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中的计算结果一致。

2016 2016
Month Account1 Account2 Month Account1 Account2
Display Display Display Display
1 2006.67 3010 1 2090.16 3135.23
2 2013.36 3020.03 2 2098.86 3148.3
3 2020.07 3030.1 3 2107.61 3161.42
4 2026.8 3040.2 4 2116.39 3174.59
5 2033.56 3050.33 5 2125.21 3187.82
6 2040.33 3060.5 6 2134.07 3201.1
7 2047.14 3070.7 7 2142.96 3214.44
8 2053.96 3080.94 8 2151.89 3227.83
9 2060.81 3091.21 9 2160.85 3241.28
10 2067.68 3101.51 10 2169.86 3254.78
11 2074.57 3111.85 11 2178.9 3268.35
12 2081.48 3122.22 12 2187.98 3281.96




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值