Hey there!
I’ve done the Mortgage Calculator from the Object-Oriented programming in the java course of code with Mosh.
As I did it from scratch and didn’t copy his solution, just arranged my result with his tips, I’d like you to tell me whether there’re flaws or any way to improve the following code.
Thank you!
package me.oriol;
public class Main {
public static void main(String[] args) {
var report = new MortgageReport();
report.printMortgage();
report.printPaymentSchedule();
}
}
package me.oriol;
import java.util.Scanner;
public class Console {
public int principal = (int) readNumber("Principal", 1000, 1_000_000);
public float annualInterest = readNumber("Annual Interest Rate", 0.1F, 30);
public byte years = (byte) readNumber("Period (Years)", 1, 30);
public float readNumber(String prompt, float min, int max) {
Scanner scanner = new Scanner(System.in);
float value;
while (true) {
System.out.print(prompt + ": ");
value = scanner.nextFloat();
if (value >= min && value <= max)
break;
System.out.println("Enter a number between " + (short) min + " and " + max + ".");
}
return value;
}
}
package me.oriol;
public class MortgageCalculator {
private final Console console = new Console();
private final byte MONTHS_IN_YEAR = 12;
private final int principal = console.principal;
private final float annualInterest = console.annualInterest;
private final byte years = console.years;
private final float monthlyInterest = getMonthlyInterest();
private final short numberOfPayments = getNumberOfPayments();
private float getMonthlyInterest() {
byte PERCENT = 100;
return annualInterest / MONTHS_IN_YEAR / PERCENT;
}
private short getNumberOfPayments() {
return (short) (years * MONTHS_IN_YEAR);
}
public double calculateMortgage() {
return principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments)
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
}
private double calculateBalance(short numberOfPaymentsMade) {
return principal
* (Math.pow(1 + monthlyInterest, numberOfPayments) - Math.pow(1 + monthlyInterest, numberOfPaymentsMade))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
}
public double[] getRemainingBalances() {
var balances = new double[numberOfPayments];
for (short month = 1; month <= balances.length; month++)
balances[month -1] = calculateBalance(month);
return balances;
}
}
package me.oriol;
import java.text.NumberFormat;
public class MortgageReport {
private final MortgageCalculator calculator = new MortgageCalculator();
private final NumberFormat currency = NumberFormat.getCurrencyInstance();
public void printMortgage() {
double mortgage = calculator.calculateMortgage();
String mortgageCurrency = currency.format(mortgage);
System.out.println("\nMORTGAGE" + "\n--------" + "\nMonthly Payments: " + mortgageCurrency + "\n");
}
public void printPaymentSchedule() {
System.out.println("PAYMENT SCHEDULE" + "\n----------------");
for (double balance : calculator.getRemainingBalances())
System.out.println(currency.format(balance));
}
}