Java Mortgage Calculator

Image for post
Image for post
Let’s have Java do the math for us!

Hello, fellow coders! If you have recently got into Java just like I have this will be a great read for you. I am going to talk about how I built a mortgage calculator with bunched up code and then organized them with methods. From the end of all this, you should be able to realize that Java is really great with organizing your code blocks.

If you aren’t familiar with what a mortgage is, you should be familiar with what a calculator is. It helps compute simple or complicated math problems. A mortgage is a debt instrument, secured by the collateral of specified real estate property, that the borrower is obliged to pay back with a predetermined set of payments. However we’re not going to go into real estate, we’re going to create numbers and calculate the result. To start off we need to know the formula to calculate the mortgage.

Image for post
Image for post
Figure 1.0 The mortgage rate per month formula

Before we start coding, we need to make this as realistic as possible and understand what values we need. For the values we need: principal that stands for the loan amount, rate that stands for how much interest we are applying, and months that stands for how long you will take to pay off the loan. To make this look like a real mortgage calculator, the principal must be between the amount of 1,000 and 1,000,000, the annual interest and years have to be between 1 and 30. I am saying years because we can calculate the years into month with simple math.

Image for post
Image for post
Figure 1.1 These are the edge cases to make our mortgage calculator more realistic. The readNumbers is a method that reduces the amount of while loops used (left gif).
Image for post
Image for post
Image for post
Image for post
Figure 1.2 This is our end result. The methods we create will take 3 inputs and calculate the monthly payments and will show the recurring payments which we see on the left.

Now that we have a solid understanding of what we are trying to do, let’s get to it! Usually on any Java project, you have one main method. If we take a look at figure 1.3, you can see that our main method calls out to 5 methods.

Image for post
Image for post
Figure 1.3 Looks very beautiful. It only contains 6 lines of code.

The first 3 method calls are calling the same method and the purpose is to reduce the amount of while loops. The method readNumbers takes 3 arguments, prompt, min, and

Image for post
Image for post
Figure 1.4 We compare the values to the min and max with a while loop. The while loop will never end and will keep asking to input a value between the min and the max.

max. The prompt is meant for the principal, monthly interest, and years. The min and max are meant for the edge cases which is crucial for our calculator to be realistic. Please note that each argument contains a data type next to it and it helps organize the flow of our logic. On line 40 we are utilizing one of Java’s classes that allows us to input some value. Once we get that input, we have to check to see if the inputs are within the edge cases we have put with the prompt. We create a while loop to check the inputs and to force the user to input the correct values.

Image for post
Image for post
Figure 1.5 This method is going to calculate the mortgage and it is going to take the mortgage value and abstracting the number values by changing it into a currency value.

Once we have the correct inputs, we are now able to calculate the mortgage. On figure 1.5, we are calling on the method calculateMortgage which will do all the heavy computations. In

Image for post
Image for post
Figure 1.6 Deconstructed formula of Figure 1.0. The variable monthInYears and percent are global variables I used that are outside the method scoop. I did it this way so that I don’t have to excessively type the same values.

order for us to even start computing, we have to save the values of our prompt from readNumbers method. If you refer back to figure 1.3 and take a look at lines 13 to 15, we created variables that will hold the values and use them for the method call printMortgage. The method printMortgage is not necessary to have, but it makes our code more organized and much easier to read. Now let’s dive into what our calculateMortgage method is doing. In figure 1.6, we are deconstructing the formula from figure 1.0. On line 58, we are converting the years into months for our formula and save its to a short variable called num_Of_Payments. On line 59, we have a float variable called actualRate and it calculates the rate. On line 61, we use ratePlusOne and assign it to (1 + actualRate)^num_Of_Payments. We are casting the value as a float to keep it consistent. Lastly on line 62, we are calculating the mortgage from the two pervious variables: ratePlusOne and actualRate and the principal. It basically replicates the mortgage formula and returns the value.

Image for post
Image for post
Figure 1.7 It requires the same inputs from readNumbers and we are iterating the amount of month it takes with a for-loop to calculate the remaining balance.

The purpose of these next two methods is to show the monthly payments and amount left for each month that passes by. On figure 1.7, we create a for-loop that will call the method

Image for post
Image for post
Figure 1.8 The formula for the remaining balance from the mortgage.
Image for post
Image for post
Figure 1.9 Follows the same structure as figure 1.6.

calculateBalance. The for-loop will keep calling on that method until it reaches the last month of payments. On figure 1.8, it utilizes a different formula that will calculate the remaining balance. It follows a similar structure to the earlier method when we calculated the mortgage.

Image for post
Image for post
Image for post
Image for post
More than 70 lines of code on a single method (left). The best practice is having each method contain about 10 to 20 lines of code (right). Sorry if you can’t read it, I used Giphy Capture to show the visual difference between using one methods and multiple methods for our calculator. I hope that you can see the visual difference and see how the code structure on the right looks more beautiful compared to the left.

There you have it, a simple mortgage calculator built from multiple methods to make our code structure more organized. Please note that reference variables must be consistent with one another and you can change the variable data type by casting. Hope you are able to take something from this blog and have a great day! Happy coding. ⌨️

Interested in the structure of codes and chasing the Dopamine rush as I start to understand their structures.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store