贷款计算器

182 阅读1分钟
package com.liny;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 6.115利率 ==》第[ 17 ]期 ==》 本金 [ 2997.26 ] ==> 利息 [ 886.98 ] ==> 剩余本金 [ 171061.70 ] ==》 已还本金 [ 48938.30 ] ==》 已还利息 [ 17093.78 ]
 * 4.500利率 ==》第[ 17 ]期 ==》 本金 [ 3073.10 ] ==> 利息 [ 646.36 ] ==> 剩余本金 [ 169289.97 ] ==》 已还本金 [ 50710.03 ] ==》 已还利息 [ 12520.79 ]
 * *********************************以下是等额本息还款详情**********************************
 *
 * 月   利  率 ==》 0.00375000000000000000
 * 首月还款利息 ==》 825.00000000000000000000
 * 首月还款本金 ==》 2894.45805102040947941424
 *
 * 月       供 ==》 3719.45805102040947941424
 * 总共要还本金 ==》 220000
 * 总共要还利息 ==》 29203.70
 *
 * 本期 可 结清 ==》 17
 * 已还 总 利息 ==》 12520.79
 * 已还 总 本金 ==》 50710.03
 *
 *
 */
public class LoanCalculator {


    public static void main(String[] args) {
        /***** 贷款计算器 ******/
        // 总贷款金额
        BigDecimal loanTotal = BigDecimal.valueOf(220000L);
        // 年利率 4.1% ==> 0.041
        BigDecimal rateOfYear = BigDecimal.valueOf(0.06115);
        // 计划还款年限
        BigDecimal planMonth = BigDecimal.valueOf(67L);
        // 还款方式 1、等额本息 2、等额本金
        int type = 1;
        // 每月存款
        BigDecimal deposit = BigDecimal.valueOf(10000L);
        BigDecimal depositTotal = BigDecimal.ZERO;

        // 等额本息计算
        if (type == 1) {

            // 月利率
            BigDecimal rateOfMonth = rateOfYear.divide(BigDecimal.valueOf(12L), 20, RoundingMode.HALF_UP);
            // 月供
            BigDecimal tempNumber = (BigDecimal.ONE.add(rateOfMonth)).pow(planMonth.intValue()).setScale(20, RoundingMode.HALF_UP);
            BigDecimal monthlyPayAmount = loanTotal
                    .multiply(rateOfMonth)
                    .multiply(tempNumber)
                    .divide( tempNumber.subtract(BigDecimal.ONE), 20, RoundingMode.HALF_UP);

            // 首月还款利息
            BigDecimal firstPayInterest = loanTotal.multiply(rateOfYear).divide(BigDecimal.valueOf(12L), 20, RoundingMode.HALF_UP);
            // 首月还款本金
            BigDecimal firstPayLoan = monthlyPayAmount.subtract(firstPayInterest);

            BigDecimal unpaidLoanTotal = BigDecimal.valueOf(loanTotal.doubleValue());
            BigDecimal paidTotalInterest = BigDecimal.ZERO;
            BigDecimal paidTotalLoan = BigDecimal.ZERO;
            BigDecimal tempPaidTotalInterest = BigDecimal.ZERO;
            BigDecimal tempPaidTotalLoan = BigDecimal.ZERO;
            BigDecimal willTotalInterest = BigDecimal.ZERO;
            int settleMonth = 0;

            for (int i = 1; i < planMonth.intValue()+1; i++) {
                BigDecimal currentMonthInterest = unpaidLoanTotal.multiply(rateOfMonth).setScale(2, RoundingMode.HALF_UP);
                BigDecimal currentMonthLoan = monthlyPayAmount.subtract(currentMonthInterest).setScale(2, RoundingMode.HALF_UP);
                BigDecimal stayLoanTotal = unpaidLoanTotal.subtract(currentMonthLoan);
                depositTotal = depositTotal.add(deposit);
                unpaidLoanTotal = stayLoanTotal;
                tempPaidTotalInterest = tempPaidTotalInterest.add(currentMonthInterest);
                tempPaidTotalLoan = tempPaidTotalLoan.add(currentMonthLoan);
                willTotalInterest = willTotalInterest.add(currentMonthInterest);
                if (settleMonth == 0) {
                    paidTotalInterest = paidTotalInterest.add(currentMonthInterest);
                    paidTotalLoan = paidTotalLoan.add(currentMonthLoan);
                }
                if (settleMonth == 0 && depositTotal.compareTo(stayLoanTotal) > 0) {
                    settleMonth = i;
                }
                System.out.println("第[ "+ i +" ]" +
                        "==》 本金 [ "+ currentMonthLoan.toPlainString() +" ] " +
                        "==> 利息 [ "+ currentMonthInterest.toPlainString() +" ] " +
                        "==> 剩余本金 [ "+ stayLoanTotal.toPlainString() +" ] " +
                        "==》 已还本金 [ "+ tempPaidTotalLoan.toPlainString() +" ] " +
                        "==》 已还利息 [ "+ tempPaidTotalInterest.toPlainString() +" ]");
            }


            System.out.println();
            System.out.println("*********************************以下是等额本息还款详情**********************************");
            System.out.println();

            System.out.println("月   利  率 ==》 " + rateOfMonth.toPlainString());
            System.out.println("首月还款利息 ==》 " + firstPayInterest.toPlainString());
            System.out.println("首月还款本金 ==》 " + firstPayLoan.toPlainString());
            System.out.println();

            System.out.println("月       供 ==》 " + monthlyPayAmount.toPlainString());
            System.out.println("总共要还本金 ==》 " + loanTotal.toPlainString());
            System.out.println("总共要还利息 ==》 " + willTotalInterest.toPlainString());
            System.out.println();

            System.out.println("本期 可 结清 ==》 " + settleMonth);
            System.out.println("已还 总 利息 ==》 " + paidTotalInterest.toPlainString());
            System.out.println("已还 总 本金 ==》 " + paidTotalLoan.toPlainString());
            System.out.println();

        } else {

        }
    }

}