0330 复利计算的单元测试情况

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年,那么每月等额本息还款多少?(算复利条件下等额还款金额)

二.单元测试预期结果及其代码。

测试模块

测试输入的数据

预期结果

误差允许的范围

最后测试结果

复利计算终值

     ( 本金,年利率,年限)

终值    
测试运算结果        (10000  ,0.03,  10) 13439.163793441223         1.0

            ture

 单利计算

( 本金,年利率,年限)

   

            

测试运算结果

(10000,0.03,10)

13000.0

0.0 

            ture

利率计算                            

( 终值,本金,年限)

 

 

 

 测试运算结果

(13439,10000,10) 

0.029998744652774967

0.001

             ture

                                 

     

三.单元测试代码

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;


public class compoundingTest {
    compounding compound;

    @Before
    public void setUp() throws Exception {
        compound=new compounding();
    }

    @Test
    public void testPrincipal_formula() {
        int Year=10;
        double i=0.03,P,S=13439;
         P = S/ Math.pow((1 + i ), Year);
         assertEquals(9999.878122297087,10000,0.5);
    }

    @Test
    public void testCompound_amount_formula() {
        int Year=10;
        double i=0.03,P,S=10000;
        P = S/ Math.pow((1 + i ), Year);
        assertEquals(13439.163793441223,13440,1.0);
    }

    @Test
    public void testSingle_interest_formula() {
         int Year=10;
          double F,i=0.03,P=10000;
            F = P *(1+i*Year);
            assertEquals(13439.163793441223,13440,1.0);
        
    }

    @Test
    public void testYears_formula() {
        int year;
        double i=0.03, F=13439, P=10000;
        year = (int) (Math.log(F / P) / Math.log(1 + i ));
        assertEquals(9,10,1.0);
    }

    @Test
    public void testRate_formula() {
        int Year=10;
        double rate;
        double F=13439, P=10000;
        rate = (Math.pow(F / P, 1.0 / Year) - 1);
        assertEquals(0.029998744652774967,0.03,0.01);
    }

    @Test
    public void testEqual_investment_years() {
        int Year=10;
        double i=0.03, F=13439, P=10000,final_value;
        final_value = P * (Math.pow(1 + i, Year) - 1) / i;
        assertEquals(114638.79311470741,114639,0.5);
    }

    @Test
    public void testEqual_investment_months() {
        int Year=10;
        double i=0.003,P=10000,final_value;
        final_value = P * 12 * (1 + i) * (Math.pow(1 + i, Year) - 1) / i;
        assertEquals(1219979.2737040932,1219980,1.0);
    }

    @Test
    public void testEqual_repayment_formula() {
        int Year=10;
        double refund;
        double i=0.03, F=13439, P=10000;
        refund = F * i / (12 * (1 + i) * (Math.pow(1 + i, Year) - 1));
        assertEquals(94.84553222222804,95,0.5);
    }

}

四.感受与体会

       刚开始单元测试是什么都不懂,慢慢了解后,才逐渐懂得了一点,不想该开始那样迷茫,因为不知道c语言的单元测试的,所以匆忙下改了java的版本,因为时间不够,接下来会不断完善程序,我相信我会慢慢进步。

复利程序java版源代码网址:http://www.cnblogs.com/RSTART/p/5339132.html

原文地址:https://www.cnblogs.com/RSTART/p/5339118.html