# Mortgage Calculator – Java Code

/**
* @author Arutyun
*/

import java.text.*;
import java.io.*;

class Mortgage {

/**Variables for monthly mortgage payment formula**/
double P = 200000.00; // Principal loan amount
double r []= {0.0044583333, 0.0045833333, 0.0047916666}; // Array for annual interest rate: (rate/1200)
int N []= {84, 180, 360}; // Array for loan term in months.
double MP; // Monthly payment
DecimalFormat df = new DecimalFormat(“\$###,###.00″);

public Mortgage () { // Constructor

for(int i = 0; i < r.length; i++){

/**Formula for monthly mortgage payment**/
MP = (r[i] + r[i] /(Math.pow((1+r[i]),N[i])-1))* P;

System.out.println(“Loan Amount: ” + df.format(P));
System.out.println(“Loan Term: ” + N[i] + ” Months / ” + (N[i]/12) + ” Years”);
System.out.println(“Interest Rate: ” + (r[i]*12*100));
System.out.println(“”);
System.out.printf(“Monthly mortgage payment: \$” + “%.2f %n”,MP);
System.out.println(“”);

/**Variables for loan amortization**/
double IP = P*r[i]; // Monthly interest payment
double PP = MP – IP; // Monthly principal payment
int count = 1; // For iteration
double PB = P-PP; // New Principal balance
double IC = IP;
double P = PB;

System.out.println(“Loan Amortization”);
System.out.println(“”);

while (PB > 0){

System.out.printf(“Payment:”+count+” Principal Balance remaining:” + ” \$” + “%.2f “,PB);
System.out.printf(“Interest Paid: \$” + “%.2f %n”,IP);

IP = P*r[i];
PP = MP – IP;
PB = P-PP;
P = PB;
IC = IC + IP;
count ++;

if (count % 10 == 0){
try {

System.out.println(“”);
System.out.println(“Total Principal Paid: \$200000.00″);
System.out.printf(“Total Interest Paid: \$” + “%.2f %n”,IC);
System.out.printf(“Total of 360 Payments: \$” + “%.2f %n”,IC + 200000.00);
System.out.println(“”);
System.out.println(“Thank You — Click Enter to receive loan information for next term”);

}
catch (IOException e) {}

}
}
}

}//Close public Mortgage

public static void main (String[] args) {

Mortgage calculator = new Mortgage();

}//main

}//close Mortgage