/** * 阿里借呗计息还款规则说明实现 *  * 测试阿里案例和韩哥案例通过 * @return */package jdongtech.jiebaiUtils;

import java.util.Arrays;import java.util.Calendar;import java.util.HashMap;import java.util.Map;

import jdongtech.interestUtils.AverageCapitalPlusInterestUtils;

public class advanceRepayMore {    public static void main(String[] args) {

        Calendar lendDay = Calendar.getInstance(); // 借款日期       Calendar rebackDay = Calendar.getInstance();// 还款日期      double rebackInvest = 0; // 还款金额     double invest = 0; // 借款本金       int month = 0; // 期数     double yearRate = 0; // 年利率      int acctOffsetDay = 15; // 平移日期      int accountDay = 25; // 账单日,蚂蚁会把账单日设置成借款当日

      // 文件示例       lendDay.set(Calendar.MONTH, 9);       lendDay.set(Calendar.DAY_OF_MONTH, 13);       rebackInvest = 100.24;       // rebackInvest = 60.04;     rebackDay.set(Calendar.MONTH, 9);     rebackDay.set(Calendar.DAY_OF_MONTH, 14);     invest = 1200; // 本金     month = 12; // 期数        yearRate = 7.2 / 100; // 年利率     acctOffsetDay = 15;      accountDay = 25;

       // 韩哥示例       lendDay.set(Calendar.MONTH, 4);       lendDay.set(Calendar.DAY_OF_MONTH, 13);       rebackInvest = 60.04;        rebackDay.set(Calendar.MONTH, 4);     rebackDay.set(Calendar.DAY_OF_MONTH, 13);     invest = 120; // 本金      month = 6; // 期数     yearRate = 14.4 / 100; // 年利率        acctOffsetDay = 15;      accountDay = 13; // 账单日,蚂蚁会把账单日设置成借款当日

      double dateRate = yearRate / 360;        int[] daysCount = new int[month];        int IncreaseFlag = 0;        Calendar lendCalOffset = (Calendar) lendDay.clone();     lendCalOffset.add(Calendar.DATE, acctOffsetDay);      Calendar accCal = (Calendar) lendDay.clone();        accCal.set(Calendar.DAY_OF_MONTH, accountDay);        Calendar accCalBegin = (Calendar) accCal.clone();        if (lendCalOffset.before(accCal)) {       } else {          accCalBegin.add(Calendar.MONTH, 1);           IncreaseFlag = 1;        }     Calendar accCalEnd = (Calendar) accCal.clone();      accCalEnd.add(Calendar.MONTH, month - 1);

       System.out.println(lendDay.getTime() + "借款日期");        System.out.println(accCalBegin.getTime() + "开始");      System.out.println(accCalEnd.getTime() + "结束");        int daysLending = RepayUtils.daysOffset(lendDay, accCalEnd);     System.out.println("借款经历" + daysLending + "天");

       Calendar accCalPerEnd = (Calendar) accCalBegin.clone();      for (int i = 0; i < month; i++) {           Calendar accCalPerBeg;            if (i == 0) {               accCalPerBeg = (Calendar) lendDay.clone();           } else {              accCalPerBeg = (Calendar) accCalPerEnd.clone();              accCalPerBeg.add(Calendar.MONTH, -1);         }

           int daysPer = RepayUtils.daysOffset(accCalPerBeg, accCalPerEnd);         daysCount[i] = daysPer;          accCalPerEnd.add(Calendar.MONTH, 1);      }

       System.out.println("部分还款前计划:");     normalRepay.getPerMonthPrincipalInterestBig(invest, dateRate, month, daysCount, IncreaseFlag);

      System.out.println("提前还款日期:" + rebackDay.getTime());       int curPeriod = 0;       int daysCurPeriod = RepayUtils.daysOffset(lendDay, rebackDay);       for (int i = 0; i < daysCount.length; i++) {            daysCurPeriod = daysCurPeriod - daysCount[i];            if (daysCurPeriod <= 0) { // 账单日算当前期              curPeriod = i;               daysCurPeriod = daysCurPeriod + daysCount[i];               i = daysCount.length;            }     }     System.out.println("提前还款当期期数:" + curPeriod);       System.out.println("提前还款当期天数:" + daysCurPeriod);       System.out.println("部分还款后计划:");     getPerMonthPrincipalInterestBigRebackSome(invest, dateRate, month, daysCount, IncreaseFlag, rebackInvest,             curPeriod, daysCurPeriod);    }

   /**    * 计算实际等额本息每月额度    *     * @return    */   public static double getPerMonthPrincipalInterestBigRebackSome(double invest, double dateRate, int totalmonth,            int[] daysCount, int IncreaseFlag, double rebackInvest, int curPeriod, int daysCurPeriod) {       IncreaseFlag = 1;        if (daysCurPeriod == 0) {           daysCurPeriod = 1;       }     double perMonthStandard = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(invest, dateRate * 360,               totalmonth);

        double perMonthMax = perMonthStandard * 1.01;        double[] PRperMonth = new double[totalmonth];        double[] PperMonth = new double[totalmonth];     double[] RperMonth = new double[totalmonth];     double[] PLeftperMonth = new double[totalmonth];     Map<Double, Double> lastCheckMap = new HashMap<Double, Double>();        Map<Double, double[]> PLeftperMonthMap = new HashMap<Double, double[]>();        Map<Double, double[]> PRperMonthMap = new HashMap<Double, double[]>();       Map<Double, double[]> PperMonthMap = new HashMap<Double, double[]>();        Map<Double, double[]> RperMonthMap = new HashMap<Double, double[]>();        Map<Double, Double> sumPRMap = new HashMap<Double, Double>();        Map<Double, Double> sumPMap = new HashMap<Double, Double>();     Map<Double, Double> sumRMap = new HashMap<Double, Double>();

       if (IncreaseFlag == 1) {            while (perMonthStandard < perMonthMax) {               PRperMonth = new double[totalmonth];             PperMonth = new double[totalmonth];              RperMonth = new double[totalmonth];              PLeftperMonth = new double[totalmonth];

                PRperMonth[0] = RepayUtils.num2second(perMonthStandard);             PLeftperMonth[0] = RepayUtils.num2second(invest);                RperMonth[0] = RepayUtils.num2secondDown(PLeftperMonth[0] * daysCount[0] * dateRate);                PperMonth[0] = RepayUtils.num2second(PRperMonth[0] - RperMonth[0]);              for (int j = 1; j < totalmonth; j++) {                  PRperMonth[j] = RepayUtils.num2second(perMonthStandard);                 PLeftperMonth[j] = RepayUtils.num2second(PLeftperMonth[j - 1] - PperMonth[j - 1]);                   RperMonth[j] = RepayUtils.num2secondDown(PLeftperMonth[j] * dateRate * daysCount[j]);                    PperMonth[j] = RepayUtils.num2second(PRperMonth[j] - RperMonth[j]);                  if (j == totalmonth - 1) {                      PperMonth[j] = RepayUtils.num2second(PLeftperMonth[j]);                      PRperMonth[j] = RepayUtils.num2second(PperMonth[j] + RperMonth[j]);                 }             }             double sumP = 0;             double sumR = 0;             double sumPR = 0;                for (int i = 0; i < PLeftperMonth.length; i++) {                    sumP = sumP + PperMonth[i];                 sumR = sumR + RperMonth[i];                 sumPR = sumPR + PRperMonth[i];              }             lastCheckMap.put(RepayUtils.num2second(perMonthStandard),                     Math.abs(PRperMonth[totalmonth - 1] - PRperMonth[totalmonth - 2]));               PLeftperMonthMap.put(RepayUtils.num2second(perMonthStandard), PLeftperMonth);             PRperMonthMap.put(RepayUtils.num2second(perMonthStandard), PRperMonth);               PperMonthMap.put(RepayUtils.num2second(perMonthStandard), PperMonth);             RperMonthMap.put(RepayUtils.num2second(perMonthStandard), RperMonth);             sumPRMap.put(RepayUtils.num2second(perMonthStandard), RepayUtils.num2second(sumPR));              sumPMap.put(RepayUtils.num2second(perMonthStandard), RepayUtils.num2second(sumP));                sumRMap.put(RepayUtils.num2second(perMonthStandard), RepayUtils.num2second(sumR));

              perMonthStandard = perMonthStandard + 0.01;         }     }

       Double resultKey = RepayUtils.getKeyByMinValue(lastCheckMap);        // 当期剩余天数     int remainDaysCurPeriod = daysCount[curPeriod] - daysCurPeriod;      double rebackPCurPeriod = RepayUtils.num2second(             rebackInvest - RepayUtils.num2secondDown(PLeftperMonth[curPeriod] * dateRate * daysCurPeriod));

     if (rebackPCurPeriod < PperMonthMap.get(resultKey)[curPeriod]) { // 还款小于当前期本金          PLeftperMonthMap.get(resultKey)[curPeriod] = RepayUtils                  .num2second(PLeftperMonthMap.get(resultKey)[curPeriod] - rebackPCurPeriod);           PperMonthMap.get(resultKey)[curPeriod] = RepayUtils                  .num2second(PperMonthMap.get(resultKey)[curPeriod] - rebackPCurPeriod);           RperMonthMap.get(resultKey)[curPeriod] = RepayUtils                  .num2secondDown(PLeftperMonthMap.get(resultKey)[curPeriod] * dateRate * remainDaysCurPeriod);         PRperMonthMap.get(resultKey)[curPeriod] = RepayUtils                 .num2second(RperMonthMap.get(resultKey)[curPeriod] + PperMonthMap.get(resultKey)[curPeriod]);

          System.out.println("等额本息每月还款额:" + resultKey);           System.out.println("每期经历" + Arrays.toString(daysCount));           System.out.println("每月余本金:" + Arrays.toString(PLeftperMonthMap.get(resultKey)));           System.out.println("每月还款额:" + Arrays.toString(PRperMonthMap.get(resultKey)));          System.out.println("每月还本金:" + Arrays.toString(PperMonthMap.get(resultKey)));           System.out.println("每月还利息:" + Arrays.toString(RperMonthMap.get(resultKey)));           double sumP = 0;         double sumR = 0;         double sumPR = 0;            for (int i = 0; i < PLeftperMonth.length; i++) {                sumP = sumP + PperMonthMap.get(resultKey)[i];               sumR = sumR + RperMonthMap.get(resultKey)[i];               sumPR = sumPR + PRperMonthMap.get(resultKey)[i];            }         sumPRMap.put(RepayUtils.num2second(resultKey), RepayUtils.num2second(sumPR));         sumPMap.put(RepayUtils.num2second(resultKey), RepayUtils.num2second(sumP));           sumRMap.put(RepayUtils.num2second(resultKey), RepayUtils.num2second(sumR));

         System.out.println("总还款额:" + sumPRMap.get(resultKey));         System.out.println("总还本金:" + sumPMap.get(resultKey));          System.out.println("总还利息:" + sumRMap.get(resultKey));

        } else { // 还款本金大于当期本金            int[] dayCountsAfter = new int[totalmonth - curPeriod];          double[] PRperMonthAfter = new double[totalmonth - curPeriod];           double[] PperMonthAfter = new double[totalmonth - curPeriod];            double[] RperMonthAfter = new double[totalmonth - curPeriod];            double[] PLeftperMonthAfter = new double[totalmonth - curPeriod];

          // P本金0的阶段 第一个月           double remainInvest = RepayUtils.num2second(PLeftperMonthMap.get(resultKey)[curPeriod] - rebackPCurPeriod);          PperMonthAfter[0] = RepayUtils.num2second(0);            PLeftperMonthAfter[0] = remainInvest;            RperMonthAfter[0] = RepayUtils.num2secondDown(PLeftperMonthAfter[0] * dateRate * remainDaysCurPeriod);           PRperMonthAfter[0] = RperMonthAfter[0];

            // P本金非0的再平衡阶段            int remainMonth = totalmonth - curPeriod - 1;            double perMonthStandardRec = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(remainInvest,                  dateRate * 360, remainMonth);         int[] daysCountRec = Arrays.copyOfRange(daysCount, curPeriod + 1, totalmonth); // 剩余天数数组            for (int i = 0; i < remainMonth; i++) {             dayCountsAfter[i + 1] = daysCountRec[i];                dayCountsAfter[0] = remainDaysCurPeriod;         }

           double perMonthMaxRec = perMonthStandardRec * 1.1;           double[] PRperMonthRec = new double[remainMonth];            double[] PperMonthRec = new double[remainMonth];         double[] RperMonthRec = new double[remainMonth];         double[] PLeftperMonthRec = new double[remainMonth];         Map<Double, Double> lastCheckMapRec = new HashMap<Double, Double>();         Map<Double, double[]> PLeftperMonthMapRec = new HashMap<Double, double[]>();         Map<Double, double[]> PRperMonthMapRec = new HashMap<Double, double[]>();            Map<Double, double[]> PperMonthMapRec = new HashMap<Double, double[]>();         Map<Double, double[]> RperMonthMapRec = new HashMap<Double, double[]>();         while (perMonthStandardRec < perMonthMaxRec) {             PRperMonthRec = new double[remainMonth];             PperMonthRec = new double[remainMonth];              RperMonthRec = new double[remainMonth];              PLeftperMonthRec = new double[remainMonth];

                PRperMonthRec[0] = RepayUtils.num2second(perMonthStandardRec);               PLeftperMonthRec[0] = RepayUtils.num2second(remainInvest);               RperMonthRec[0] = RepayUtils.num2secondDown(PLeftperMonthRec[0] * daysCountRec[0] * dateRate);               PperMonthRec[0] = RepayUtils.num2second(PRperMonthRec[0] - RperMonthRec[0]);             for (int j = 1; j < remainMonth; j++) {                 PRperMonthRec[j] = RepayUtils.num2second(perMonthStandardRec);                   PLeftperMonthRec[j] = RepayUtils.num2second(PLeftperMonthRec[j - 1] - PperMonthRec[j - 1]);                  RperMonthRec[j] = RepayUtils.num2secondDown(PLeftperMonthRec[j] * dateRate * daysCountRec[j]);                   PperMonthRec[j] = RepayUtils.num2second(PRperMonthRec[j] - RperMonthRec[j]);                 if (j == remainMonth - 1) {                     PperMonthRec[j] = RepayUtils.num2second(PLeftperMonthRec[j]);                        PRperMonthRec[j] = RepayUtils.num2second(PperMonthRec[j] + RperMonthRec[j]);                    }             }

               lastCheckMapRec.put(RepayUtils.num2second(perMonthStandardRec),                       Math.abs(PRperMonthRec[remainMonth - 1] - PRperMonthRec[remainMonth - 2]));

             PLeftperMonthMapRec.put(RepayUtils.num2second(perMonthStandardRec), PLeftperMonthRec);                PRperMonthMapRec.put(RepayUtils.num2second(perMonthStandardRec), PRperMonthRec);              PperMonthMapRec.put(RepayUtils.num2second(perMonthStandardRec), PperMonthRec);                RperMonthMapRec.put(RepayUtils.num2secondDown(perMonthStandardRec), RperMonthRec);                perMonthStandardRec = perMonthStandardRec + 0.01;           }

           Double resultKeyRec = RepayUtils.getKeyByMinValue(lastCheckMapRec);

            for (int i = 1; i < totalmonth; i++) {              PperMonthAfter[i] = RepayUtils.num2second(PperMonthMapRec.get(resultKeyRec)[i - 1]);             PLeftperMonthAfter[i] = RepayUtils.num2second(PLeftperMonthMapRec.get(resultKeyRec)[i - 1]);             RperMonthAfter[i] = RepayUtils.num2second(RperMonthMapRec.get(resultKeyRec)[i - 1]);             PRperMonthAfter[i] = RepayUtils.num2second(PRperMonthMapRec.get(resultKeyRec)[i - 1]);           }

           System.out.println("重新等额本息每月还款额:" + resultKeyRec);          System.out.println("重新每期经历" + Arrays.toString(dayCountsAfter));            System.out.println("重新每月余本金:" + Arrays.toString(PLeftperMonthAfter));          System.out.println("重新每月还款额:" + Arrays.toString(PRperMonthAfter));         System.out.println("重新每月还本金:" + Arrays.toString(PperMonthAfter));          System.out.println("重新每月还利息:" + Arrays.toString(RperMonthAfter));

            double sumP = 0;         double sumR = 0;         double sumPR = 0;            for (int i = 0; i < PLeftperMonth.length; i++) {                sumP = sumP + PperMonthAfter[i];                sumR = sumR + RperMonthAfter[i];                sumPR = sumPR + PRperMonthAfter[i];         }         sumPRMap.put(RepayUtils.num2second(resultKey), RepayUtils.num2second(sumPR));         sumPMap.put(RepayUtils.num2second(resultKey), RepayUtils.num2second(sumP));           sumRMap.put(RepayUtils.num2second(resultKey), RepayUtils.num2second(sumR));

         System.out.println("总还款额:" + sumPRMap.get(resultKey));         System.out.println("总还本金:" + sumPMap.get(resultKey));          System.out.println("总还利息:" + sumRMap.get(resultKey));      }     return resultKey; }

}

借呗提前还款算法模拟相关推荐

  1. 华南理工大学计算机操作系统课程设计大作业银行家死锁避免算法模拟,2016春操作系统大作业银行家死锁避免算法模拟.doc...

    文档介绍: 2016春操作系统大作业银行家死锁避免算法模拟20160501华南理工大学"计算机操作系统"课程设计大作业计算机科学与技术专业:春2015班级:号:2015047420 ...

  2. 欧盟「人脑计划」​最新进展:新算法模拟生物进化,为大脑如何工作提供新见解...

    来源:ScienceAI 编辑:凯霞 2013 年,欧盟「人脑计划」(Human Brain Project,HBP)正式启动,是欧洲最大的脑科学项目,也是欧盟资助的最大研究项目之一.是欧盟首批为期 ...

  3. OPT和LRU页面置换算法C语言代码,页面置换算法模拟——OPT、FIFO和LRU算法.doc

    实用标准文案 精彩文档 操作系统实验报告 页面置换算法模拟 --OFT.FIFO和LRU算法 班级:2013级软件工程1班 学号:X X X 姓名:萧氏一郎 数据结构说明: Memery[10]物理块 ...

  4. 计算机网络透明网桥的算法,网基课程设计曼切斯特编码模拟透明网桥自习域转发帧算法模拟IP分片模拟.doc...

    网基课程设计曼切斯特编码模拟透明网桥自习域转发帧算法模拟IP分片模拟 计算机网络基础 课 程 设 计 报 告 学号: 姓名: 班级: 题号:01.06.07 题目:曼切斯特编码模拟.透明网桥自学习域转 ...

  5. python抢红包程序算法,Python 抢红包算法模拟

    抢红包算法模拟 本篇内容为抢随机红包模拟算法,仅供参考. 方法 本篇使用的是二倍均值法,在此之前,先大概讲下普通随机法. 普通随机方法 该方法的原理是:每次都以 [最小值,剩余金额值] 之间进行随机取 ...

  6. 虚拟存储页面置换算法c语言,虚拟存储器管理页面置换算法模拟实验.doc

    虚拟存储器管理页面置换算法模拟实验 淮海工学院计算机工程学院 实验报告书 课程名:< 操作系统原理A > 题 目: 虚拟存储器管理 页面置换算法模拟实验 班 级: 软件*** 学 号: 2 ...

  7. 最近最久未使用页面置换算法C语言,LRU页面置换算法模拟-最近最久未使用置换算法...

    LRU页面置换算法模拟-最近最久未使用置换算法 LRU页面置换算法模拟-最近最久未使用置换算法|课程设计|计算机数据库课程设计 一.设计目的 1.用C语言实现最近最久未使用(LRU)置换算法. 2.了 ...

  8. 操作系统实验四:C++实现独占设备的分配与回收算法模拟

    目录 一.实验要求 1.实验名称 2.实验目的 3.实验内容 二.实验图解 三.实验代码 四.实验数据 1.设备插入功能 2.回收程序功能 3.查找功能 一.实验要求 1.实验名称 独占设备的分配与回 ...

  9. Sysweld笔记:利用稳态算法加速算法模拟焊接过程的残余应力

    作者:贾亚波博士,仿真秀专栏作者 在进行热力耦合的仿真过程中,如果模型足够的长并且热源速度恒定,通常其热学,相变以及热力耦合都会达到稳态的过程,因此如何直接计算稳态问题成为了大家研究的热点问题. 由于 ...

最新文章

  1. 金陵科技学院c语言测试,金陵科技学院C语言实验册.doc
  2. 自定义组合控件:下拉选择框
  3. Java集合篇:Map常用遍历方式 以及 性能对比
  4. .net集合类的研究--链表—ListDictionary,LinkedListT
  5. WCF 使用证书认证 方法
  6. iOS10权限设置问题以及xcdoe8更新细节问题
  7. 中南民族大学计算机类有什么具体专业,中南民族大学计算机科学学院计算机科学与技术专业简介...
  8. 找出无序数组中最小的k个数(top k问题)
  9. inputstream java_Java实现inputstream流的复制
  10. 阔别七载,阿里港交所这对“初恋”再牵手
  11. Windows10共享文件夹、打印机,可是网络上显示“未授予用户在此计算机上的请求登录类型”的解决方案
  12. 【HDOJ6992】Lawn of the Dead(线段树×, 模拟大法好√)
  13. office无法卸载无法安装的解决方法
  14. 使用Travis CI实现C++ CMake工程多平台编译
  15. 7z文件格式及其源码的分析(三)
  16. 计算机点阵存储空间,计算机存储量计算存储400个24*24点阵汉字字形所需的存储容量是________....
  17. 从零搭建Spring Boot脚手架(2):增加通用的功能
  18. Java读取串口数据
  19. s18服务器维护,8月11日S15至S18服务器合服公告!
  20. 真气APPiOS隐私策略

热门文章

  1. JavaScript RegExp 正则对象
  2. Qt——QLineEdit
  3. 使用vscode利用vue脚手架创建项目每次修改代码都会频繁编译
  4. 北理工计算机组成原理在线作业,[北京师范大学]20秋《计算机组成原理》离线作业...
  5. 【166】VS2022调试通过海康人脸抓拍SDK的C++代码
  6. 测试用例设计——WEB通用测试用例(转)
  7. Oracle数据优化,超级全
  8. 【thinkphp5操作redis系列教程】集合类型之sRem,sRemove
  9. warnings模块
  10. Ubuntu-18.04 安装 ROS 系统