实体类-银行账户余额推算表(Savings Account Class)
2016-07-08 23:37
363 查看
声明:金额计算未采用BigDecimal类。
代码如下:
运行结果:
本年度年利率为百分之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中的计算结果一致。
代码如下:
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 |
相关文章推荐
- Android 实现下载时主界面与详细界面一致更新
- 单元测试phpunit安装及使用实例
- Android 实现下载时主界面与详细界面一致更新
- Android局部模糊处理
- kafka 学习之初体验
- 说说js闭包
- jquery click事件多次执行解决办法
- 如何使Win32 应用程序兼容系统控件的主题
- 了解MySQL的字符集
- javaweb中action、service、model、dao层如何区分
- Android(ADT Android SDK Manager安装Intel Hardware Accelerated Execution Manager)
- Configuring OSPFv3
- Day9作业:socket之FTP工具
- 234. Palindrome Linked Lis
- HDU 2064 汉诺塔III
- Autolayout 自动布局
- Discuz X3.2实现手机号登录
- mysqldump 备份原理9
- MySQL常用命令总结
- Android之Log的日志打印信息的封装