Write a program that lets the user input a loan amount and starting annual interest rate
(not monthly interest rate), then display the monthly and total payments. Print a table that
displays the monthly and total payments for 4 interest rates, with an increment of ½
starting from the given starting interest rate for the years from 1 to 10 (if pay off the
mortgage in that number of years.)
The formula to compute the monthly payment is as follows:
monthlyPayment = (loanAmount * monthlyInterestRateAdd)/(1-Math.pow(1+monthlyInterestRateAdd,-12));
the output is to look like this:
Enter the loan amount: 10000
Enter the starting interest rate: 5.0
Years Interest Rate Monthly Payment Total Payment
1 5% 856.07 10272.89
5.5% 858.36 10300.41
6.0% 860.66 10327.97
6.5% 862.96 10355.57
2 5% 438.71 10529.13
5.5% 440.95 10582.95
6.0% 443.2 10636.94
6.5% 445.46 10691.1
… …
10 5% 106.06 12727.86
5.5% 108.52 13023.15
6.0% 111.02 13322.46
6.5% 113.54 13625.75
My code looks like this:
import java.util.Scanner;
import java.text.NumberFormat;
public class MortgageCalculator {
public static void main(String[] args) {
double loanAmount;
double annualInterestRate;
int loanDuration = 120;
double monthlyPayment, monthlyInterestRate, monthlyInterestRateAdd;
double annualInterestRateAdd;
double principle;
int year = 1;
Scanner keyboard = new Scanner(System.in);
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMaximumFractionDigits(3);
NumberFormat currency = NumberFormat.getCurrencyInstance();
currency.setMaximumFractionDigits(2);
System.out.println("Please enter the amount of your loan.");
loanAmount = keyboard.nextDouble();
System.out.println("Please enter the annual interest rate as a decimal. Ex. 7.5% = .075");
annualInterestRate = keyboard.nextDouble();
monthlyInterestRate = (annualInterestRate/12);
System.out.println("Year Interest Rate Monthly Payment Total Payment" );
while (year < 11)
{
annualInterestRateAdd = annualInterestRate;
monthlyInterestRateAdd = monthlyInterestRate;
System.out.print(year);
double monthlyPaymentTotal = 0;
for (int i = 1; i < 5; i += 1)
{
System.out.print("\t"+ percent.format(annualInterestRateAdd));
monthlyPayment = (loanAmount * monthlyInterestRateAdd)/(1-Math.pow(1+monthlyInterestRateAdd,-12));
System.out.print("\t\t"+ currency.format(monthlyPayment));
monthlyPaymentTotal = monthlyPayment*12;
System.out.println("\t\t\t" + currency.format(monthlyPaymentTotal));
annualInterestRateAdd += .005;
monthlyInterestRateAdd += monthlyInterestRate/12;
}
loanAmount = (loanAmount + (1+ monthlyInterestRate))- monthlyPaymentTotal ;
year += 1;
}
}
}
I canot get the subsequent years to cary the Totals and reduce the monthly payments and increase the total amount paid on the far right column any help is appreciated.