Hello. I am a total newbie to Java (and serious programming). I thought I had written this program correctly, but I am getting different results than Mosh was getting with his sample input (Principal: 1,000,000; Interest rate: 3.92; Period: 30 years; Mortgage (calculated): $4728.13). When I enter the same values, I get $3265.67 for the monthly payments. Also, my “calculateBalance” function keeps returning $0, so it seems obvious that it’s a calculation error, but I cannot find it.
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
private final static byte MONTHS = 12;
private final static byte PERCENT = 100;
public static void main(String[] args) {
GetMortgage();
}
private static void GetMortgage() {
/*
Principal: 1,000,000
Interest rate: 3.92
Period: 30 years
Mortgage: $4728.13
*/
final double PRINCIPAL_MIN = 1_000;
final double PRINCIPAL_MAX = 1_000_000;
final double RATE_MIN = .01;
final double RATE_MAX = 100.00;
final byte YEARS_MIN = 1;
final byte YEARS_MAX = 30;
Scanner scan = new Scanner(System.in);
double principal = getInput("Principal ($1K - $1M)", "Invalid entry. Please enter a value between $1K and $1M", scan, PRINCIPAL_MIN, PRINCIPAL_MAX);
double rate = getInput("Interest rate (.01% - 100%)", "Invalid entry. Please enter a value between .01% and 100%", scan, RATE_MIN, RATE_MAX);
int period = (int)(getInput("Period (years)", "Invalid entry. Please enter a value between 1 and 30", scan, YEARS_MIN, YEARS_MAX));
scan.close();
double mortgage = calculateMortgage(principal, rate, period);
NumberFormat nf = NumberFormat.getCurrencyInstance();
printMortgage(mortgage, nf);
printPaymentSchedule(principal, rate, period, nf);
}
private static double getInput(String prompt, String failMessage, Scanner sc, double min, double max) {
System.out.print(prompt + ": ");
while (true) {
if (!sc.hasNextDouble()) {
System.out.println(failMessage);
} else {
double input = sc.nextDouble();
if (input >= min && input <= max) {
return input;
}
System.out.println(failMessage);
}
sc.nextLine();
}
}
private static double calculateMortgage(double principal, double rate, int period) {
rate = rate / PERCENT / MONTHS;
period *= MONTHS;
return principal
* (rate * Math.pow((1 + rate), period))
/ Math.pow(1 + rate, period) - 1;
}
private static double calculateBalance(double principal, double rate, int period, short numberOfPaymentsMade) {
rate = rate / PERCENT / MONTHS;
period *= MONTHS;
double balance = principal
* (Math.pow(1 + rate, numberOfPaymentsMade) - Math.pow(1 + rate, numberOfPaymentsMade))
/ (Math.pow(1 + rate, numberOfPaymentsMade) - 1);
return balance;
}
private static void printMortgage(double mortgage, NumberFormat nf) {
String payments = nf.format(mortgage);
System.out.println();
System.out.println("MORTGAGE");
printRepeatedChar("-", 25);
System.out.println("Monthly payments: " + payments);
}
private static void printPaymentSchedule(double principal, double rate, int period, NumberFormat nf) {
System.out.println();
System.out.println("PAYMENT SCHEDULE");
printRepeatedChar("-", 50);
for(short month = 1; month < period * MONTHS; month++) {
double balance = calculateBalance(principal, rate, period, month);
String formattedBalance = nf.format(balance);
System.out.println(formattedBalance);
}
}
private static void printRepeatedChar(String s, int repeatCount) {
System.out.println(s.repeat(repeatCount));
}
}