java模拟计算等额本息贷款
2019-04-09 / JAVA / 1150 次围观 / 0 次吐槽 /import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class Demo6 { public static void main(String[] args) throws ParseException { // TODO Auto-generated method stub int amount = 10000; //借款金额 amount = amount*100;//转化单位为(分)计算 int month = 12; // 还款期数 double mRate = 0.012; //借款月利率 String str = "2019-04-09"; //设置借款日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(str); int mPrincipal = amount / month; //每月还款本金 int balancePrincipal = amount % month; //不能整除 有余 System.out.println("借款:"+amount / 100.0 + "元"); System.out.println("借款期数:"+month+"月"); System.out.println("每个月需要大约还款平均本金:"+mPrincipal / 100.0 + "元"); List<String> dates = Finance.replyDateList(date,month); List<Repayment> list = new ArrayList<Repayment>(); for(int i=0;i<month;i++) { Repayment repayment = new Repayment(); repayment.setPeriod(i+1);//设置期数 if(i == (month-1)) { //设置每月还款本金 repayment.setPrincipal((mPrincipal + balancePrincipal) / 100.0); }else { repayment.setPrincipal(mPrincipal / 100.0); } //计算每月需还利息,未能实现精确计算 int leftAmount = amount - mPrincipal * i; double interest = leftAmount * mRate; repayment.setInterest(interest / 100.0); repayment.setDate(dates.get(i));//设置还款日期 list.add(repayment); } for(Repayment rep : list) { System.out.println(rep); } }
public class Repayment { private double principal; //当月本金 private int period; //还款期数 private double interest; //当月利息 private String date; //还款日期 public Repayment() {} public Repayment(int principal, int period, int interest, String date) { this.principal = principal; this.period = period; this.interest = interest; this.date = date; } @Override public String toString() { return "Repayment [当月本金=" + principal + ", 还款期数=" + period + ", 当月利息=" + interest + ", 还款日期=" + date + "]"; } public double getPrincipal() { return principal; } public void setPrincipal(double principal) { this.principal = principal; } public int getPeriod() { return period; } public void setPeriod(int period) { this.period = period; } public double getInterest() { return interest; } public void setInterest(double interest) { this.interest = interest; } public String getDate() { return date; } public void setDate(String date) { this.date = date; }
计算贷款还款日期
public class Finance { /** * * @param borrowDate 借款日期 * @param borrowMonth 借款期数 * @return 全部期数还款日期 */ public static List<String> replyDateList(Date borrowDate,int borrowMonth){ List<String> list = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String str = sdf.format(borrowDate); String[] strs = str.split("-"); int y = Integer.parseInt(strs[0]); //年 int m = Integer.parseInt(strs[1]); //月 int d = Integer.parseInt(strs[2]); //日 for(int i=0;i<borrowMonth;i++) { //计算还款月的最后一天 Calendar calendar = Calendar.getInstance(); calendar.set(y, m+i+1, 1);//m不变 随着循环的遍历 i的值逐渐增大 m+i+1 来设置月份 calendar.add(Calendar.DAY_OF_MONTH, -1);//计算最后一天 int cy =calendar.get(Calendar.YEAR); int cm =calendar.get(Calendar.MONTH)+1; int cd =calendar.get(Calendar.DAY_OF_MONTH); //计算当月还款日 int huanKuanRe = d; //默认情况下还款日就是借款日 if(d > cd) { //借款日大于当月最大天数 huanKuanRe = cd; //设置为当月最后一天 } String huanKuanDate = cy +"-"+ cm +"-"+ huanKuanRe; list.add(huanKuanDate); } return list; } }
- 上一篇:Collections集合工具类
- 下一篇:Java中的异常
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.