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.
First Off, Mortgage Calculator?
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.
Initial Set Up
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.
Let’s Start Building
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.
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
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.
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
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.
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
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.
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. ⌨️