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

声明:金额计算未采用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




  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的银行账户类的代码示例,实现了所述的属性和方法: ```python class BankAccount: def __init__(self, account_number, account_type, balance): self.account_number = account_number self.account_type = account_type self.balance = balance def deposit(self, amount): self.balance += amount def withdraw(self, amount): if self.balance >= amount: self.balance -= amount else: print("Insufficient balance") def __str__(self): return f"Account number: {self.account_number}, Account type: {self.account_type}, Balance: {self.balance}" ``` 在这个类中,`__init__`方法用于初始化账户号码、账户类型和余额属性,`deposit`方法用于向账户中存入指定金额,`withdraw`方法用于从账户中取出指定金额,`__str__`方法用于返回一个字符串,包含账户号码、账户类型和余额信息。注意,`withdraw`方法会检查余额是否足够,如果不足,会输出一条“余额不足”的消息。 可以使用以下代码创建一个银行账户对象并进行测试: ```python account = BankAccount("123456789", "Savings", 1000) print(account) # Output: Account number: 123456789, Account type: Savings, Balance: 1000 account.deposit(500) print(account) # Output: Account number: 123456789, Account type: Savings, Balance: 1500 account.withdraw(200) print(account) # Output: Account number: 123456789, Account type: Savings, Balance: 1300 account.withdraw(2000) # Output: Insufficient balance print(account) # Output: Account number: 123456789, Account type: Savings, Balance: 1300 ``` 在这个例子中,我们首先创建了一个银行账户对象,账户号码为“123456789”,账户类型为“Savings”,余额为1000。然后,我们使用`deposit`方法存入了500元,使用`withdraw`方法取出了200元。最后,我们试图从账户中取出2000元,但是因为余额不足,输出了一条“余额不足”的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值