public class Interest {
private ExpressRunner runner = new ExpressRunner();
public void initial() throws Exception{
runner.addFunctionOfClassMethod("等本等息", Interest.class.getName(), "method1",new Class[] {double.class,double.class,int.class}, null);
runner.addFunctionOfClassMethod("等额本息", Interest.class.getName(), "method2",new Class[] {double.class,double.class,int.class}, null);
runner.addFunctionOfClassMethod("等额本金", Interest.class.getName(), "method3",new Class[] {double.class,double.class,int.class}, null);
}
public static void main(String[] args) throws Exception {
Interest indemo = new Interest();
indemo.initial();
Object r = indemo.runner.execute("等本等息(1000,0.008,12)", null, null, false, true);
System.out.println(r);
Object r2 = indemo.runner.execute("等额本息(1000,0.008,12)", null, null, false, true);
System.out.println(r2);
Object r3 = indemo.runner.execute("等额本金(1000,0.008,12)", null, null, false, true);
System.out.println("等额本金每个月的还款列表"+r3);
}
/**
* 等本等息还款方式计算每个月偿还本金和利息
* @param invest
* @param yearRate
* @param totalMonth
* @return
*/
public String method1(double invest,double yearRate,int totalMonth) {
double d = invest/totalMonth + invest*yearRate/totalMonth;
return "等本等息方式每个月应还款"+d;
}
/**
* 等额本息还款方式计算每月偿还本金和利息
* @param invest
* @param yearRate
* @param totalMonth
* @return
*/
public String method2(double invest, double yearRate, int totalMonth) {
double monthRate = yearRate / 12;
BigDecimal monthIncome = new BigDecimal(invest).multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalMonth))).divide(new BigDecimal(Math.pow(1 + monthRate, totalMonth)-1),2,BigDecimal.ROUND_DOWN);
return "等额本息方式每个月应还款"+monthIncome.doubleValue();
}
public Map method3(double invest, double yearRate, int totalMonth) {
Map map = new HashMap();
// 每月本金
double monthPri = getPerMonthPrincipal(invest, totalMonth);
// 获取月利率
double monthRate = yearRate / 12;
monthRate = new BigDecimal(monthRate).setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
for (int i = 1; i <= totalMonth; i++) {
double monthRes = monthPri + (invest - monthPri * (i - 1)) * monthRate;
monthRes = new BigDecimal(monthRes).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
map.put(i, monthRes);
}
return map;
}
/**
* 等额本金计算获取还款方式为等额本金的每月偿还本金
*
* 公式:每月应还本金=贷款本金÷还款月数
*
* @param invest
* 总借款额(贷款本金)
* @param yearRate
* 年利率
* @param month
* 还款总月数
* @return 每月偿还本金
*/
public static double getPerMonthPrincipal(double invest, int totalMonth) {
BigDecimal monthIncome = new BigDecimal(invest).divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
return monthIncome.doubleValue();
}
}