借呗分期还款模拟

Java代码 
  1. /** 
  2.  * 阿里借呗计息还款规则说明实现 
  3.  *  
  4.  * @return 
  5.  */  
  6. package jdongtech.jiebaiUtils;  
  7.   
  8. import java.util.Arrays;  
  9. import java.util.Calendar;  
  10. import java.util.HashMap;  
  11. import java.util.Map;  
  12.   
  13. import jdongtech.interestUtils.AverageCapitalPlusInterestUtils;  
  14.   
  15. public class normalRepay {  
  16.     public static void main(String[] args) {  
  17.         // 需要手动填写  
  18.         double invest = 120; // 本金  
  19.         int month = 6;     // 期数  
  20.         double yearRate = 14.4 / 100; // 年利率  
  21.         int acctOffsetDay = 15;  
  22.         int accountDay = 13;     // 账单日,蚂蚁会把账单日设置成借款当日  
  23.         // 发起借款当天  
  24.         Calendar lendDay = Calendar.getInstance();  
  25.         lendDay.set(Calendar.MONTH, 4);  
  26.         lendDay.set(Calendar.DAY_OF_MONTH, 13);  
  27.   
  28.         double dateRate = yearRate / 360;  
  29.         int[] daysCount = new int[month];  
  30.         int IncreaseFlag = 0;  
  31.         Calendar lendCalOffset = (Calendar) lendDay.clone();  
  32.         lendCalOffset.add(Calendar.DATE, acctOffsetDay);  
  33.         Calendar accCal = (Calendar) lendDay.clone();  
  34.         accCal.set(Calendar.DAY_OF_MONTH, accountDay);  
  35.         Calendar accCalBegin = (Calendar) accCal.clone();  
  36.         if (lendCalOffset.before(accCal)) {  
  37.         } else {  
  38.             accCalBegin.add(Calendar.MONTH, 1);  
  39.             IncreaseFlag = 1;  
  40.         }  
  41.         Calendar accCalEnd = (Calendar) accCal.clone();  
  42.         accCalEnd.add(Calendar.MONTH, month-1);  
  43.   
  44.         System.out.println(lendDay.getTime() + "借款日期");  
  45.         System.out.println(accCalBegin.getTime() + "开始");  
  46.         System.out.println(accCalEnd.getTime() + "结束");  
  47.         int daysLending = RepayUtils.daysOffset(lendDay, accCalEnd);  
  48.         System.out.println("借款经历" + daysLending + "天");  
  49.   
  50.         Calendar accCalPerEnd = (Calendar) accCalBegin.clone();  
  51.         for (int i = 0; i < month; i++) {  
  52.             Calendar accCalPerBeg;  
  53.             if (i == 0) {  
  54.                 accCalPerBeg = (Calendar) lendDay.clone();  
  55.             } else {  
  56.                 accCalPerBeg = (Calendar) accCalPerEnd.clone();  
  57.                 accCalPerBeg.add(Calendar.MONTH, -1);  
  58.             }  
  59.   
  60.             int daysPer = RepayUtils.daysOffset(accCalPerBeg, accCalPerEnd);  
  61.             daysCount[i] = daysPer;  
  62.             accCalPerEnd.add(Calendar.MONTH, 1);  
  63.         }  
  64.         //dateRate=0.0003;  
  65.         getPerMonthPrincipalInterestBig(invest, dateRate, month, daysCount, IncreaseFlag);  
  66.     }  
  67.   
  68.     /** 
  69.      * 计算实际等额本息每月额度 
  70.      *  
  71.      * @return 
  72.      */  
  73.     public static double getPerMonthPrincipalInterestBig(double invest, double dateRate, int totalmonth, int[] daysCount,  
  74.             int IncreaseFlag) {  
  75.         IncreaseFlag = 1;  
  76.         double perMonthStandard = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(invest, dateRate * 360,  
  77.                 totalmonth);  
  78.           
  79.         double perMonthMax = perMonthStandard * 1.1;  
  80.         double[] PRperMonth = new double[totalmonth];  
  81.         double[] PperMonth = new double[totalmonth];  
  82.         double[] RperMonth = new double[totalmonth];  
  83.         double[] PLeftperMonth = new double[totalmonth];  
  84.         Map<Double, Double> lastCheckMap = new HashMap<Double, Double>();  
  85.         Map<Double, String> PLeftperMonthMap = new HashMap<Double, String>();  
  86.         Map<Double, String> PRperMonthMap = new HashMap<Double, String>();  
  87.         Map<Double, String> PperMonthMap = new HashMap<Double, String>();  
  88.         Map<Double, String> RperMonthMap = new HashMap<Double, String>();  
  89.         Map<Double, Double> sumPRMap = new HashMap<Double, Double>();  
  90.         Map<Double, Double> sumPMap = new HashMap<Double, Double>();  
  91.         Map<Double, Double> sumRMap = new HashMap<Double, Double>();  
  92.   
  93.         if (IncreaseFlag == 1) {  
  94.             while (perMonthStandard < perMonthMax) {  
  95.                 PRperMonth[0] = RepayUtils.num2second(perMonthStandard);  
  96.                 PLeftperMonth[0] = RepayUtils.num2second(invest);  
  97.                 RperMonth[0] = RepayUtils.num2secondDown(PLeftperMonth[0] * daysCount[0] * dateRate);  
  98.                 PperMonth[0] = RepayUtils.num2second(PRperMonth[0] - RperMonth[0]);  
  99.                 for (int j = 1; j < totalmonth; j++) {  
  100.                     PRperMonth[j] = RepayUtils.num2second(perMonthStandard);  
  101.                     PLeftperMonth[j] = RepayUtils.num2second(PLeftperMonth[j - 1] - PperMonth[j - 1]);  
  102.                     RperMonth[j] = RepayUtils.num2secondDown(PLeftperMonth[j] * dateRate * daysCount[j]);  
  103.                     PperMonth[j] = RepayUtils.num2second(PRperMonth[j] - RperMonth[j]);  
  104.                     if (j == totalmonth - 1) {  
  105.                         PperMonth[j] = RepayUtils.num2second(PLeftperMonth[j]);  
  106.                         PRperMonth[j] = RepayUtils.num2second(PperMonth[j] + RperMonth[j]);  
  107.                     }  
  108.                 }  
  109.                 double sumP = 0;  
  110.                 double sumR = 0;  
  111.                 double sumPR = 0;  
  112.                 for (int i = 0; i < PLeftperMonth.length; i++) {  
  113.                     sumP = sumP + PperMonth[i];  
  114.                     sumR = sumR + RperMonth[i];  
  115.                     sumPR = sumPR + PRperMonth[i];  
  116.                 }  
  117.                 lastCheckMap.put(RepayUtils.num2second(perMonthStandard),  
  118.                         Math.abs(PRperMonth[totalmonth - 1] - PRperMonth[totalmonth - 2]));  
  119.                 PLeftperMonthMap.put(RepayUtils.num2second(perMonthStandard), Arrays.toString(PLeftperMonth));  
  120.                 PRperMonthMap.put(RepayUtils.num2second(perMonthStandard), Arrays.toString(PRperMonth));  
  121.                 PperMonthMap.put(RepayUtils.num2second(perMonthStandard), Arrays.toString(PperMonth));  
  122.                 RperMonthMap.put(RepayUtils.num2second(perMonthStandard), Arrays.toString(RperMonth));  
  123.                 sumPRMap.put(RepayUtils.num2second(perMonthStandard), RepayUtils.num2second(sumPR));  
  124.                 sumPMap.put(RepayUtils.num2second(perMonthStandard), RepayUtils.num2second(sumP));  
  125.                 sumRMap.put(RepayUtils.num2second(perMonthStandard), RepayUtils.num2second(sumR));  
  126.   
  127.                 perMonthStandard = perMonthStandard + 0.01;  
  128.             }  
  129.         }  
  130.   
  131.         Double resultKey = RepayUtils.getKeyByMinValue(lastCheckMap);  
  132.   
  133.         System.out.println("等额本息每月还款额:" + resultKey);  
  134.         System.out.println("每期经历" + Arrays.toString(daysCount));  
  135.         System.out.println("每月余本金:" + PLeftperMonthMap.get(resultKey));  
  136.         System.out.println("每月还款额:" + PRperMonthMap.get(resultKey));  
  137.         System.out.println("每月还本金:" + PperMonthMap.get(resultKey));  
  138.         System.out.println("每月还利息:" + RperMonthMap.get(resultKey));  
  139.         System.out.println("总还款额:" + sumPRMap.get(resultKey));  
  140.         System.out.println("总还本金:" + sumPMap.get(resultKey));  
  141.         System.out.println("总还利息:" + sumRMap.get(resultKey));  
  142.         return resultKey;  
  143.     }  
  144.   
  145. }  
posted @ 2017-05-17 10:52  葫芦一生  阅读(772)  评论(0编辑  收藏  举报