// Dwight Welsh
/Mortgage Calculator
//Principal = The initial amount of the loan
//InterestYearly = The annual interest rate (from 1 to 100 percent)
//InterestMonthly = monthly interest in decimal form = interestYealy / (12 * 100)
//LengthInYears = the length (in years) of the loan
//NumberOfPayments = number of months over which loan is amortized = L * 12
//InterestMonthly = InterestYearly / (12.0*100.0)
//NumberOfPayments = LengthInYears *12
//MonthlyPayment = (Principal * InterestMonthly) / (1.0 - (Math.pow(1.0 + InterestMonthly, - NumberOfPayments)))
//This Imports the various classes
import java.text.NumberFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.Formatter;
class Loan {
public final double principal;
public final double interestYearly;
public final int lengthInYears;
public Loan(double principal, double interestYearly, int lengthInYears) {
this.principal = principal;
this.interestYearly = interestYearly;
this.lengthInYears = lengthInYears;
}
public double ComputeMonthlyInterest() {
return interestYearly / 12.0;
}
public double ComputeMonthlyPayment() {
double interestMonthly = ComputeMonthlyInterest();
double numberOfPayments = lengthInYears * 12.0;
return (principal * interestMonthly)
/ (1.0 - (Math.pow((1.0 + interestMonthly), -numberOfPayments)));
}
}
// File name is Mortgage.java
public class Mortgage {
private Loan loan;
private NumberFormat nf;
// Calculate and Return the Monthly Payment
public Mortgage(double principal, double interestYearly, int lengthInYears) {
this(new Loan(principal, interestYearly, lengthInYears));
}
public Mortgage(Loan loan) {
this.loan = loan;
nf = NumberFormat.getCurrencyInstance();
}
// Displays the Monthly Payment
public void DisplayMonthlyPayment() {
double monthlyPayment = loan.ComputeMonthlyPayment();
double total = loan.principal;
double mrate = loan.ComputeMonthlyInterest();
//tells the number of lines to show on a screen before asking the user to press enter
int linesPerScreen = 20;
//counts the number of lines (months) printed to the console
int monthsCount = 0;
//This will read input from the console
Scanner input = new Scanner(System.in);
System.out.println ("Loan amount " + nf.format(loan.principal)
+ " over " + loan.lengthInYears + " years term at "
+ (loan.interestYearly * 100) + "% interest rate");
System.out.println("Month\t\tPayment Amount\t\tInterest\tPrincipal");
//Start of Loop code
while (monthsCount < loan.lengthInYears * 12){
for( int k = 0 ; k < linesPerScreen ; k++ ){
double interest = total * mrate;
total -= (monthlyPayment - interest);
System.out.printf ("%2d\t\t%s\t\t%s\t\t%s\n",
monthsCount+1, nf.format(monthlyPayment), nf.format(interest), nf.format(total));;
//This will cause the month to increase
monthsCount = monthsCount+1;
}
System.out.println ("Press Enter to continue...");
input.nextLine();
}
}
public static void main(String[] args) {
System.out.println("Dwight Welsh: " + new Date());
Mortgage pgm = new Mortgage(200000, 0.0535, 7);
pgm.DisplayMonthlyPayment();
Mortgage pgm2 = new Mortgage(200000, 0.0550, 15);
pgm2.DisplayMonthlyPayment();
Mortgage pgm3 = new Mortgage(200000, 0.0575, 30);
pgm3.DisplayMonthlyPayment();
}
}