Write a program that calcualtes the payment anount, interest paid, and loan balance after each payment for a loan of 200000 using an array to step through three interest rates and loan terms.
In the following code I have asked the program to "press any key to continue" after each individual interest rate and loan term is completed so the user will know when the next interest rate and term is going to be displayed. The problem is that the "press any key to contiune" only works the first and third time. What am I missing?
CODE
Author Derek Williamson
Version 4.0
Created on April 18, 2006
To calculate and display a monthly mortgage payment for three different
terms associated with three different interest rates. The mortgage amount is 200,000,
the interest is 5.35%, 5.50%, 5.75% and the terms are 7, 15, and 30 years.
*/
import java.text.DecimalFormat;
public class mortgageCalculator4
{
public static void main(String[] args) throws Exception
{
//Declares variables used to hold the financial and term data.
double mortgageAmount = 200000.00;
double[] interestRate = {.0535, .0550, .0575};
double interest = 0;
int[] termMonths = {84, 180 ,360};
double months = 360;
double temp = 0;
double monthlyPayment = 0;
double interestPaid = 0;
double principalPaid = 0;
double mortgageBalance = 200000;
double lineCount = 3;
DecimalFormat money = new DecimalFormat("$0.00");
int i;
int j;
//steps through the array and displays the payment amount.
for (i = 0; i <= 2; i++){
lineCount = 10;
for (j = 0; j <= termMonths[i]; j++){
mortgageAmount=200000;
mortgageBalance = 200000;
//formula to calculate monthly payment amount
monthlyPayment = (mortgageAmount*(interestRate[i]/12)) / (1 - 1 /Math.pow((1 + interestRate[i]/12), termMonths[i]));
//Prints the mortgage calculator
System.out.println("Mortgage Calculator");
System.out.println("Your payment for a " +(money.format(mortgageAmount)) + " mortgage over a term of");
System.out.println((termMonths[i]/12) + " years with an interest rate of " + (interestRate[i]*100)+ "% will be " +(money.format(monthlyPayment)) + " a month.");
System.out.println();
System.out.println("Press Enter to Continue.");
System.in.read ();
//Starts loop statement, declares formula for loan balance and interest paid.
while(termMonths[i] > 0) {
//reduces the term one month at a time
months = (termMonths[i] --);
//calculates the interest paid and resets the mortgage balance to reflect payment.
temp = (1 - 1 /Math.pow((1 + interestRate[i]/12), months));
interestPaid = (monthlyPayment * temp);
mortgageBalance = (mortgageAmount - monthlyPayment + interestPaid);
//Displays the payment amount, mortgage balance, and interest paid.
System.out.println("After a payment of: " +(money.format(monthlyPayment)));
System.out.println("The current balance of your mortgage is: " +(money.format(mortgageBalance)));
System.out.println("This months interest paid on the mortgage is: " +(money.format(interestPaid)));
System.out.println();
//sets a new mortgage amount reflecting the payment.
mortgageAmount = mortgageBalance;
}
//Pauses screen
if (lineCount > 0) {
lineCount--;
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
}
}
}
//Stops loop statement
//if (termMonths[i] <= 0){
//System.out.println("CONGRATULATIONS! Your mortgage is paid in full!");
//}
} }