Project: Mortgage Calculator Pt 1
When you run the program it should ask a few question.
- What is the principal (Loan amount) e.g.
100000
($100,000.00) - Annual Interest Rate e.g.
3.92
(3.92%) - Period (Years): e.g.
30
The program should calculate monthly
payments and display it as a currency e.g. $472.81
Formula for calculating mortgage payments
Section titled “Formula for calculating mortgage payments”m = Mortgage p = Principal r = Monthly interest rate (r / 100)/12 n = Number of payments (as it’s monthly over 30 years number of months in 30 years.) [30 * 12]
$M = P \times \frac{r(1 + r)^n}{(1 + r)^n - 1}$
Math has the pow method Math.pow(a, b)
where it takes the value of one and provides a power of to the second one.
Code I wrote for the project
Section titled “Code I wrote for the project”import java.text.NumberFormat;import java.util.Scanner;
public class MortgageCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("What is your principal loan amount: "); int principal = scanner.nextInt();
System.out.print("What is your annual interest rate: "); double annualInterestRate = scanner.nextFloat();
System.out.print("How many years is the loan: "); int yearsLoaned = scanner.nextInt();
double monthlyInterestRate = (annualInterestRate / 100)/12; int numberofPayments = yearsLoaned * 12;
final int P = principal; final double R = monthlyInterestRate; final int N = numberofPayments;
double m = P * (R*Math.pow((1+R),N))/(Math.pow((1+R),N)-1);
NumberFormat currency = NumberFormat.getCurrencyInstance(); final String RESULT = currency.format(m);
System.out.println(RESULT); }}
Code Review and Refactoring
Section titled “Code Review and Refactoring”Reviewing the code there were some recommendations that seemed sound, one of the key takeaways.
” Avoid magic numbers, use constants to describe number’s that may not be obvious to someone else reviewing the code. As an example instead of diving by 12 to represent Months use a final constant named variable to make your code easier to read. ”
Post our review here is the updated code to provide better clarity and readability. There were also considerations around Math associated with floats and rounding that made the code return more realistic figures when it came to dollars and cents.
What did we refactor?
- Floats to doubles to take less memory as interest rates are small numbers
- Declared constants to represent months in years and percent
- Slightly moved order to make it easier to understand when values were being modified/created based on flow of code.
- Refactored years to byte as it shouldn’t support unrealistic numbers.
- Small camel case error in numberOfYears
- We kept our formula and return the same as it follows the structure of formula and readability and matches my preference in recreating scientific formulas in code.
Our updated code now looks like the below:
import java.text.NumberFormat;import java.util.Scanner;
public class MortgageCalculator { public static void main(String[] args) { final byte MONTHS_IN_YEAR = 12; final byte PERCENT = 100;
Scanner scanner = new Scanner(System.in); System.out.print("What is your principal loan amount: "); int principal = scanner.nextInt();
System.out.print("What is your annual interest rate: "); // Interest rates are usually small numbers so float should be sufficient instead of double float annualInterestRate = scanner.nextFloat(); float monthlyInterestRate = (annualInterestRate / PERCENT) / MONTHS_IN_YEAR;
System.out.print("How many years is the loan: "); byte yearsLoaned = scanner.nextByte(); int numberOfPayments = yearsLoaned * 12;
final int P = principal; final float R = monthlyInterestRate; final int N = numberOfPayments; final double M = P * (R*Math.pow((1+R),N))/(Math.pow((1+R),N)-1);
NumberFormat currency = NumberFormat.getCurrencyInstance(); final String RESULT = currency.format(M); System.out.println(RESULT); }}