Java实现等额本息

本文介绍了一款等额本息贷款计算器的实现方法,详细解释了如何使用Java进行每期本金、利息及总利息的计算,同时提供了计算每期还款日期的功能。
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 等额本息
 */
public class EqualPrincipalInterestUtil {

	/**
	 * 每期本金+利息
	 * 公式:每月偿还本息=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1)
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodMonthNum
	 *            期数
	 * @return
	 */
	public static double getPerPeriodPrincipalInterest(double invest, double periodRate, int periodMonthNum) {
		BigDecimal monthIncome = new BigDecimal(invest)
				.multiply(new BigDecimal(periodRate * Math.pow(1 + periodRate, periodMonthNum)))
				.divide(new BigDecimal(Math.pow(1 + periodRate, periodMonthNum) - 1), 4, BigDecimal.ROUND_HALF_UP);
		return monthIncome.doubleValue();
	}

	/**
	 * 每期利息
	 * 公式:每月偿还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodMonthNum
	 *            期数
	 * @return
	 */
	public static Map<Integer, BigDecimal> getPerPeriodInterest(double invest, double periodRate, int periodMonthNum) {
		Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
		BigDecimal periodInterest;
		for (int i = 1; i < periodMonthNum + 1; i++) {
			BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(periodRate));
			BigDecimal sub = new BigDecimal(Math.pow(1 + periodRate, periodMonthNum))
					.subtract(new BigDecimal(Math.pow(1 + periodRate, i - 1)));
			periodInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + periodRate, periodMonthNum) - 1),
					6, BigDecimal.ROUND_HALF_UP);
			periodInterest = periodInterest.setScale(4, BigDecimal.ROUND_HALF_UP);
			map.put(i, periodInterest);
		}
		return map;
	}

	/**
	 * 每期还款本金
	 * 
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodNum
	 *            期数
	 * @return
	 */
	public static Map<Integer, BigDecimal> getPerPeriodPrincipal(double invest, double periodRate, int periodNum) {
		BigDecimal monthIncome = new BigDecimal(invest)
				.multiply(new BigDecimal(periodRate * Math.pow(1 + periodRate, periodNum)))
				.divide(new BigDecimal(Math.pow(1 + periodRate, periodNum) - 1), 4, BigDecimal.ROUND_HALF_UP);
		Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
		Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();

		for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
			mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
		}
		return mapPrincipal;
	}

	/**
	 * 总利息
	 * 
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodNum
	 *            期数
	 * @return
	 */
	public static double getInterestCount(double invest, double periodRate, int periodNum) {
		BigDecimal count = new BigDecimal(0);
		Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);

		for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
			count = count.add(entry.getValue());
		}
		return count.doubleValue();
	}

	/**
	 * 本息总和
	 * 
	 * @param invest
	 *            本金
	 * @param periodRate
	 *            每期利率
	 * @param periodNum
	 *            期数
	 * @return
	 */
	public static double getPrincipalInterestCount(double invest, double periodRate, int periodNum) {
		BigDecimal perMonthInterest = new BigDecimal(invest);
		Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);
		for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
			perMonthInterest = perMonthInterest.add(entry.getValue());
		}
		return perMonthInterest.doubleValue();
	}

	/**
	 * 每期还款日期
	 * @param start_date
	 * 				起租日
	 * @param perPeriodMonthNum
	 * 				每期月数
	 * @param periodNum
	 * 				期数
	 * @return
	 */
	public static Map<Integer, String> getRepaymentDate(String start_date, int perPeriodMonthNum, int periodNum) {
		Map<Integer, String> periodRepaymentDate = new HashMap<Integer, String>();
		String nextRepaymentDate = start_date;
		periodRepaymentDate.put(1, nextRepaymentDate);
		for (int i = 2; i < periodNum + 1; i++) {
			nextRepaymentDate = getMonthAdd(perPeriodMonthNum, nextRepaymentDate, "yyyyMMdd");
			periodRepaymentDate.put(i, nextRepaymentDate);
		}
		return periodRepaymentDate;
	}

	/**
	 * 功能描述:返回指定日期加上多少月之后的时间<BR>
	 * @param from   yyyyMMdd
	 * @param day
	 * @param formatStr
	 * @return
	 */
	public static String getMonthAdd(int day,String from,String formatStr){
		SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
		Calendar calendar = Calendar.getInstance();
		try {
			calendar.setTime(sdf.parse(from));
		} catch (Exception e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.MONTH, day);
		String date = sdf.format(calendar.getTime());
		return date;
	}
	
	public static void main(String[] args) {  
        double invest = 10000; // 本金  
        int periodNum = 4;
        double periodRate = 0.12/3; // 年利率  
        System.out.println("等额本息---本金:" + invest);  
        double perMonthPrincipalInterest = getPerPeriodPrincipalInterest(invest, periodRate, periodNum);  
        System.out.println("等额本息---每期还款本息:" + perMonthPrincipalInterest);  
        Map<Integer, BigDecimal> mapInterest = getPerPeriodInterest(invest, periodRate, periodNum);  
        System.out.println("等额本息---每期还款利息:" + mapInterest);  
        Map<Integer, BigDecimal> mapPrincipal = getPerPeriodPrincipal(invest, periodRate, periodNum);  
        System.out.println("等额本息---每期还款本金:" + mapPrincipal);  
        double count = getInterestCount(invest, periodRate, periodNum);  
        System.out.println("等额本息---总利息:" + count);  
        double principalInterestCount = getPrincipalInterestCount(invest, periodRate, periodNum);  
        System.out.println("等额本息---应还本息总和:" + principalInterestCount);  
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值