一.主要功能与需求分析
1.本金为100万,利率或者投资回报率为3%,投资年限为30年,那么,30年后所获得的利息收入:按复利计算公式来计算就是:1,000,000×(1+3%)^30
2.如果按照单利计算,本息的最终收益
3.假如30年之后要筹措到300万元的养老金,平均的年回报率是3%,那么,现在必须投入的本金是多少呢?
4.利率这么低,复利计算收益都这么厉害了,如果拿100万元去买年报酬率10%的股票,若一切顺利,过多长时间,100万元就变成200万元呢?
5.如果我希望在十年内将100万元变成200万元,应该找到报酬率在多少的投资工具来帮助我达成目标?如果想在5年后本金翻倍,报酬率就应至少为多少才行呢?
6.如果每年都将积蓄的3万元进行投资,每年都能获得3%的回报,然后将这些本利之和连同年金再投入新一轮的投资,那么,30年后资产总值将变为多少?如果换成每月定投3000呢?(定额定投收益计算办法)
7. 如果向银行贷款10万元,年利率6.5%,期限为10年,那么每月等额本息还款多少?(算复利条件下等额还款金额)
二.单元测试预期结果及其代码。
测试模块 |
测试输入 |
预期结果 |
运行结果 |
bug跟踪 |
复利计算 |
(本金,年限,利率,次数) |
终值 |
||
测试运算结果 |
(100.0,1,0.05,1) |
105.0 |
√ |
|
测试输出正数 |
(100.0,1,0.05,1) |
True |
√ |
|
测试输入负数 |
(-100.0,1,-0.05,1) | False | √ | 要添加输入控制 |
单利计算 |
(利率,本金,年限) |
终值 | ||
测试运算结果 | ("0.05","100.0","1") | 105.0 | √ | |
测试输入负数 | ("0.05","100.0","1") | True | √ | |
测试输出正数 | ("-0.05","-100.0","1") | False | √ | 要添加输入控制 |
投资年限 | (利率,本金,终值,次数) | |||
测试运算结果 | ("0.05","100.0","105.0","1") | 1 | √ | |
测试输出正数 | ("0.05","100.0","105.0","1") | True | √ | |
测试输入负数 | ("-0.05","-100.0","105.0","1") | false | √ | 要添加输入控制 |
...... | 以下结果与上表一致 |
1 package com.Junit.test; 2 3 import static org.junit.Assert.*; 4 import interest.BestProject; 5 import interest.CompoundInterrest; 6 import interest.InterestTime; 7 import interest.PeriodicIncome; 8 import interest.Principal; 9 import interest.Refund; 10 import interest.SingleInterest; 11 12 public class Test { 13 14 @org.junit.Test 15 public void testCompoundInterrest() { 16 double f = new CompoundInterrest("0.05","100.0","1","1").Interrest(); 17 assertEquals(105.0,f ,0.001); 18 assertTrue("输出为负数", f > 0); 19 double s = new CompoundInterrest("-0.05", "-100.0", "1", "1").Interrest(); 20 assertFalse(s > 0); 21 } 22 @org.junit.Test 23 public void testSingleInterest() { 24 double f = new SingleInterest("0.05","100.0","1").Interest(); 25 assertEquals(105.0, f, 0.001); 26 assertTrue("输出为负数", f > 0); 27 double s = new SingleInterest("-0.05","-100.0","1").Interest(); 28 assertFalse(s > 0); 29 } 30 @org.junit.Test 31 public void testInterestTime() { 32 int t = new InterestTime("0.05","100.0","105.0","1").Interrest(); 33 assertEquals(1, t); 34 assertTrue("输出为负数", t > 0); 35 double s = new InterestTime("-0.05","-100.0","105.0","1").Interrest(); 36 assertFalse(s > 0); 37 } 38 @org.junit.Test 39 public void testPeriodicIncome() { 40 double f = new PeriodicIncome("0.01","100","1").Interrest(); 41 assertEquals(101.0, f ,0.001); 42 assertTrue("输出为负数", f > 0); 43 double s = new PeriodicIncome("-0.01","-100","1").Interrest(); 44 assertFalse(s > 0); 45 } 46 @org.junit.Test 47 public void testPrincipal () { 48 double f = new Principal("0.05","105.0","1","1").Interrest(); 49 assertEquals(100.0, f, 0.001); 50 assertTrue("输出为负数", f > 0); 51 double s = new Principal("-0.05","-105.0","1","1").Interrest(); 52 assertFalse(s > 0); 53 } 54 55 56 @org.junit.Test 57 public void testBestProject () { 58 double f = new BestProject("200","100","1","1").Interrest(); 59 assertEquals(1.0, f, 0.001); 60 assertTrue("输出为负数", f > 0); 61 double s = new BestProject("-200","100","1","1").Interrest(); 62 assertFalse(s > 0); 63 } 64 @org.junit.Test 65 public void testRefund () { 66 double f = new Refund("0.87","1200","3").Interrest(); 67 assertEquals(94.6147, f ,0.001); 68 assertTrue("输出为负数", f > 0); 69 double s = new Refund("-0.87","-1200","3").Interrest(); 70 assertFalse(s > 0); 71 } 72 73 }
三.运行情况
四.代码链接
https://github.com/liezh/Compound-Interest-5.0-Junit