Automatic calculation of principal and interest

Keywords: Programming Java

Xiaoming's monthly salary is 10000 yuan, and the salary is paid on the last day of each month. After receiving the salary, it is immediately deposited in a treasure. The interest of 10000 yuan in a treasure is 30 yuan per month (settlement at the end of the month), and the year-end bonus is 50000 yuan (paid in December). Excuse me, after N months, how much is Xiaoming's interest and total deposit?

The first month: (0 / 10000) * 30 + 10000 = 10030

The second month: 10030 + (10030 / 10000) * 30 + 10000 = 20030.09

The third month: 20030.09 + (20030.09 / 10000) * 30 + 10000 = 30090.18

Set the nth month's total deposit as K, then the nth month's total deposit J = K+K/10000*30+10000

The formula above is the deposit calculation of ordinary month, excluding year-end bonus. If the starting month of deposit is M, the logical breakdown of the nth month is as follows:

If (M+N-1)% 12 = = 0, the year-end bonus of the current month is 50000

If (M+N-1)% 12! =0, then the year-end bonus of the current month is 0

The procedure is as follows:

import java.math.BigDecimal;

/*
* @author szh
* @date   2019 November 15, 2015 3:34:50 PM
* @version 1.0
*/
public class Test {
    // A monthly salary
    private static final BigDecimal SALARY = new BigDecimal("10000");
    // Annual bonus
    private static final BigDecimal BONUSES = new BigDecimal("50000");
    // Monthly interest rate
    private static final BigDecimal RATE = new BigDecimal("30");
    // Interest unit
    private static final BigDecimal ONE_THOUSAND = new BigDecimal("10000");

    public static void main(String args[]) {
        // 50 months from May
        getResult(50, 5);
    }

    /***
     * N :Months, the number of months from the beginning of calculation to the current month
     * M : Month to start calculation
     */
    private static void getResult(int N, int M) {
        BigDecimal totalRate = new BigDecimal("0");// Total interest
        BigDecimal total = new BigDecimal("0");// Total amount of money in a month
        for (int i = 1; i < N + 1; i++) {
            // Interest on all deposits of the month
            BigDecimal monthRate = total.divide(ONE_THOUSAND).multiply(RATE).setScale(5, BigDecimal.ROUND_HALF_UP);
            totalRate = totalRate.add(monthRate);// Included in total interest
            total = total.add(monthRate);// Interest of the month in which the total deposit is added
            total = total.add(SALARY);// Total deposit added to current month's salary
            // If the current month is December, the total deposit is included in the year-end bonus
            if ((M + N - 1) % 12 == 0) {
                total = total.add(BONUSES);
            }
        }
        System.out.println("Total interest:" + totalRate);
        System.out.println("Total deposits:" + total);
    }
}

The console output is as follows:

Total interest: 38577.93653
Total deposit: 538577.93653

Posted by elearnindia on Fri, 15 Nov 2019 06:50:42 -0800