Below i have my Main class and a MortgageCalculation class I am trying to modify my app to do the following:
"Modify the mortgage program to display 3 mortgage loans: 7 year at 4.35%, 15 year at 4.5 %, and 30 year at 4.75%. Use an array for the different loans. Display the mortgage payment amount for each loan. Then, list the loan balance and interest paid for each payment over the term of the loan. Pause the thread to prevent lists from scrolling off the screen. Do not use a graphical user interface. Insert comments in the program to document the program. Create at least one method and move some of your most commonly used code into that method"
My mind is just over complicating this. Please any assistance would be appreciated thanks for your time!
package com.mortgagecalc;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) throws InterruptedException {
//creating a new instance of my Calc class with a custom constructor
MortgageCalculation calc = new MortgageCalculation(30, 0.0575, 200000.00);
//Method call to calc class
double monthlyPayment = calc.CalculatePayment();
//format currency correctly
DecimalFormat df = new java.text.DecimalFormat("$,###.00");
//out put
System.out.println("__________________________________________________________________________\n");
System.out.println("\nPrincipal Amount: " + df.format (calc.GetPrincipal()));
System.out.println("Interest rate: " + calc.GetInterestRate());
System.out.println("Term of loan(number of years): " + calc.GetTerm());
System.out.println("Monthly payment is: " + df.format(monthlyPayment));
System.out.println("\n__________________________________________________________________________\n");
System.out.println ("Month \t Payment \0\0\0\0Interest Paid \0\0\0Principal Paid \0\0\0Remaining Balance");
System.out.println("__________________________________________________________________________");
// two dimensional array for months.
double[][] amorization = calc.CalculateAmorization();
for (int month = 0; month < amorization.length; month++)
{
System.out.println ("\0\0"+ (int)amorization[month][0] + "\t" + df.format(amorization[month][1])+"\t" + df.format(amorization[month][2])+"\t\t"+ df.format(amorization[month][3])+ "\t\t\0" +df.format(amorization[month][4]));
// Display 15 lines at a time
if (month % 15 == 0) {
Thread.sleep(600);
}
}
}
}
package com.mortgagecalc;
public class MortgageCalculation {
private int term; //how long the loan is in years
private double interestRate; // loan interest rate
private double principal; // loan amount
private int period = 12; // 12 months in a year
// constructor
public MortgageCalculation(int term, double interestRate, double principal) {
this.term = term;
this.interestRate = interestRate;
this.principal = principal;
}
public int GetTerm() {
return this.term;
}
public double GetInterestRate() {
return this.interestRate;
}
public double GetPrincipal() {
return this.principal;
}
//Method to calculate the payment.
public double CalculatePayment() throws InterruptedException
{
//Payment calculation
return (double) (principal * Math.pow(1 + interestRate / period, term * period) * (interestRate / period) / (Math.pow(1 + interestRate / period, term * period) - 1));
}
//Method to calculate Amortization
public double[][] CalculateAmorization() throws InterruptedException
{
int totalMonths = this.term * 12;
double[][] amorization = new double[totalMonths][5];
double balance = this.principal;
double monthlyInterest = (1 + this.interestRate / 12);
double monthlyPayment = balance * ( monthlyInterest - 1 ) / ( 1 - Math.pow(monthlyInterest,-totalMonths));
double interest, principalPayment;
for (int month = 0; month < totalMonths; month++)
{
interest = balance * ( this.interestRate / 12 );
principalPayment = monthlyPayment - interest ;
balance = balance - principalPayment;
amorization[month][0] = month + 1;
amorization[month][1] = monthlyPayment;
amorization[month][2] = interest;
amorization[month][3] = principalPayment;
amorization[month][4] = balance;
}
return amorization;
}
}