Calculation principle and Java implementation of mortgage loan

sunfulv 2020-11-09 23:48:43
calculation principle java implementation mortgage


Number part (6) Mortgage Calculator-- Mortgage calculator

Title Description :

Mortgage Calculator – Calculate the monthly payments of a fixed term mortgage over given Nth terms at a given interest rate. Also figure out how long it will take the user to pay back the loan.

Topic translation :

Mortgage calculator —— At a given interest rate , Calculate the fixed term mortgage loan in N Monthly repayment of . At the same time, calculate how long it takes for the user to repay the loan .

  1. What is a mortgage loan
    A mortgage is a loan that provides private assets as a guarantee for debt , It's mostly a mortgage loan from Ba when buying real estate .

  2. The type of mortgage
    There are many types of mortgages , The type of mortgage is defined by the following factors .

    • The interest rate (interest) : It is divided into fixed rate and floating rate
    • Time limit (Term) : Mortgage loans usually have a maximum repayment period
    • The amount and frequency of repayment (Payment amount and frequency) : Specify the time interval between two repayments and the number of repayments required in each period
    • Advance charge (PrePayment): Advance payment made by the lender in advance
  3. Repayment method
    Mortgage loans are usually paid in installments , With a fixed exchange rate , Set a repayment period , And then pay a certain amount on time every month .
    There are two common repayment methods : Equal principal and interest repayment and equal principal repayment
    The monthly repayment amount of the two repayment schemes is calculated as follows :
    Suppose the annual interest rate of the loan is r, The repayment period is Y year , The loan principal is P, The monthly repayment amount is A
    The monthly interest rate of the loan \(R =r/12\), The repayment period is \(N=12Y\)

    • Repayment of equal principal and interest :
      Equal principal and interest repayment refers to when repaying , The total amount of repayment per month is the same . The monthly payment of principal and interest is variable
      Suppose that t Months after repayment , The remaining total repayment amount is \(p(t)\).

      \[\begin{aligned} &p(0)=P\\ &p(1)=p(0)(1+R)-A=P(1+R)-A\\ &p(2)=P(1)(1+R)-A=[P(1+R)-A](1+R)-A=P(1+R)^2-(1+R)A-A\\ &...\\ &p(t)=P(1+R)^t-A(1+R)^{t-1}-A(1+R)^{t-2}-...-A(1+R)-A\\ \end{aligned} \]

      We can get the balance after the monthly payment \(p(t)\) The expression of :

      \[\begin{aligned} p(t)&=P(1+R)^t-A\sum_{i=0}^{t-1}(1+R)^i\\ &=P(1+R)^t-A\frac{1-(1+R)^t}{1-(1+R)}\\ &=P(1+R)^t-A\frac{(1+R)^t-1}{R} \end{aligned}\]

      The number of repayment periods we have given is N, in other words \(p(N)=0\), We can work out the monthly repayment amount A.
      By the equation

      \[\begin{aligned} P(n)=P(1+R)^N-A\frac{(1+R)^N-1}{R}=0\\ \end{aligned}\]

      You can get

      \[\begin{aligned} A&=\frac{PR(1+R)^N}{(1+R)^N-1}\\ \end{aligned}\]

      in other words , If the same amount of principal and interest is used to repay , The monthly amount of repayment is \(\frac{PR(1+R)^N}{(1+R)^N-1}\)

      Although the amount of repayment per month is the same , The interest and principal repayments vary from month to month
      The first t+1 The interest you need to pay back for each month \(i(t+1)\), Is the remaining repayment amount before the repayment of the month \(p(t)\) Multiply by the monthly interest rate R

      \[\begin{aligned} i(t+1)&=p(t)R\\ &=PR(1+R)^t-A(1+R)^t+A\\ &=(PR-A)(1+R)^t+A\\ &=((PR-A)(1+R)^{t-1}+A)(1+R)-A(1+R)+A\\ &=i(t)(1+R)-AR\\ \end{aligned}\]

      because \((PR-A)<0\), therefore \(i(t)\) It's about t The minus function of , That is to say, in the amount of repayment each month , The proportion of interest is reduced , And the proportion of principal is rising

    • Equal principal repayment :
      In the way of equal principal repayment , The monthly repayment principal is the same , But the interest paid each month is different , So the total amount of repayment per month changes .
      You need to pay back the principal every month pr Divide the total principal by the total number of repayment months .

      \[\begin{aligned} pr = \frac{P}{N} \end{aligned}\]

      You need to pay interest every month \(pi(t)\) = ( The principal - The sum of the principal returned )* Monthly interest rate

      \[\begin{aligned} pi(t)& = (P-pr(t-1))R\\ &=-prAt+(A+P)R\\ &=-\frac{PR}{N}t+(\frac{P}{N}+P)R\\ \end{aligned}\]

      You can see , Interest paid each month \(pi(t)\) It's about t The minus function of , It shows that the interest paid every month is gradually decreasing . As the amount of principal paid back each month remains unchanged , So the total amount of loan repayments per month is decreasing .

Program realization

User input loan exchange rate , The total amount of the loan , The repayment time limit and the choice repayment way .
The program outputs the total amount the user needs to repay each month, as well as the amount of principal and interest to be repaid .

import java.util.Scanner;
public class MortgageCalculator{
public static void mortgageCalcute(double P,double interest,int Y,int type){
// Input parameter total loan P, lending rate interest, Repayment period Y, Repayment type type(0 It means the same amount of principal and interest ,1 Means equal principal repayment )
switch(type){
case 0:
equalLoanPayment(P,interest,Y);
break;
case 1:
equalPrincipalPayment(P,interest,Y);
break;
}
}
public static void equalLoanPayment(double P,double interest,int Y){ // Equal principal and interest repayment calculation function
int N = Y*12;
double R = interest/12;
double A = P*R*Math.pow(1+R,N)/(Math.pow(1+R,N)-1);
System.out.printf(" Monthly repayment of principal and interest %7.2f\n",A*10000);
double[] pi = new double[N];
pi[0] = P*R;
System.out.printf(" The first 1 Months of interest to be paid :%8.2f The first 1 The principal to be paid back in six months is :%7.2f\n",pi[0]*10000,(A-pi[0])*10000);
for(int i=1;i<N;i++){
pi[i] = pi[i-1]*(1+R)-A*R;
System.out.printf(" The first %d Months of interest to be paid :%7.2f The first %d The principal to be paid back in six months is :%7.2f\n",i+1,pi[i]*10000,i+1,(A-pi[i])*10000);
}
}
public static void equalPrincipalPayment(double P,double interest,int Y){ // Equal principal repayment calculation function
int N = Y*12; // The total month of repayment
double R = interest/12; // The monthly interest rate for repayment
double A = P*1.0/N; // You need to pay back the principal every month
System.out.printf(" The principal to be repaid each month %7.2f\n",A*10000);
double[] pi = new double[N+1];
for(int i=1;i<=N;i++){
pi[i] = -P*R*1.0/N*i+(P/N+P)*R;
System.out.printf(" The first %d Months of interest to be paid :%7.2f. The first %d The principal and interest to be paid back for six months :%7.2f\n",i+1,pi[i]*10000,i+1,(pi[i]+A)*10000);
}
}
public static void main(String[] args){
//equalPrincipalPayment(45.4, 3.25/100, 15);
Scanner sc = new Scanner(System.in);
System.out.println(" Choose the repayment method : 0 Equal principal and interest ,1 Equal principal ");
int PaymentType = sc.nextInt();
System.out.println(" Enter the total amount of repayment ( Company : ten thousand ), Annual interest rate of repayment ( percentage ) And repayment period , Space off ");
double Payment = sc.nextDouble();
double interest = sc.nextDouble();
int years = sc.nextInt();
sc.close();
System.out.println(" The total amount of repayment :"+Payment+" Annual interest rate of repayment :"+interest+"% "+" Repayment period :"+years+" year ");
mortgageCalcute(Payment,interest/100,years,PaymentType);
}
}
版权声明
本文为[sunfulv]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云