The Amortization Class: Calculations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CASE STUDY

The Amortization Class

The loan officer at one of the Central Mountain Credit Unions branch offices has asked you to write a loan amortization application to run on her desktop PC. The application should allow the user to enter the amount of a loan, the number of years of the loan, and the annual interest rate. An amortization report should then be saved to a text file.

Calculations
The credit union uses the following formula to calculate the monthly payment of a loan:

Rate Loan Term 2 Pa ent = Paym P Term 1


where: Loan 5 the amount of the loan, Rate 5 the annual interest rate, and Term 5 (11Rate/12)Years 3 12

Report Requirements
The report produced by the program should show the monthly payment amount and the following information for each month in the loan period: amount applied to interest, amount applied to principal, and the balance. The following report may be used as a model. It shows all the required information on a one-year $5000 loan at 5.9 percent annual interest.

CS1-1

CS1-2

Case Study 1

The Amortization Class Monthly Payment: $430.10 Month Interest Principal Balance ------------------------------------------------1 24.58 405.52 4,594.48 2 22.59 407.51 4,186.97 3 20.59 409.52 3,777.45 4 18.57 411.53 3,365.92 5 16.55 413.55 2,952.37 6 14.52 415.59 2,536.78 7 12.47 417.63 2,119.15 8 10.42 419.68 1,699.47 9 8.36 421.75 1,277.72 10 6.28 423.82 853.90 4.20 425.90 428.00 11 12 2.10 428.00 0.00

The core of the program will be a class, Amortization, that holds the primary data, performs the mathematical calculations, and displays the report. Figure CS1-1 shows a UML diagram for the class. Table CS1-1 lists and describes the classs fields.

Figure CS1-1 UML diagram for the Amortization class

Case Study 1 The Amortization Class

CS1-3

Table CS1-1 Amortization class fields Field


loanAmount interestRate loanBalance term payment loanYears

Description A double variable to hold the amount of the loan. A double variable to hold the annual interest rate. A double variable to hold the loan balance. A double variable used in the calculation of the monthly payment. A double variable to hold the amount of the monthly payment. An int variable to hold the number of years of the loan.

Table CS1-2 lists and describes the classs methods. Table CS1-2 Amortization class methods Method
Constructor

Description The constructor accepts three arguments: the loan amount, the annual interest rate, and the number of years of the loan. These values are stored in their corresponding fields. The private method calcPayment is then called. A private method that is used to calculate the monthly payment amount. The result is stored in the payment field. Returns as an int the number of loan payments. Saves the amortization report to a text file. Returns as a double the amount of the loan. Returns as a double the annual interest rate. Returns as an int the number of years of the loan.

calcPayment getNumberOfPayments saveReport getLoanAmount getInterestRate getLoanYears

Code Listing CS1-1 shows the code for the class.

Code Listing CS1-1

(Amortization.java)

1 import java.io.*; 2 import java.text.DecimalFormat; 3 4 /** 5 * A class for storing loan information and printing an 6 * amortization report. 7 */ 8

CS1-4

Case Study 1

The Amortization Class

9 public class Amortization 10 { 11 private double loanAmount; // Loan Amount 12 private double interestRate; // Annual Interest Rate 13 private double loanBalance; // Monthly Balance 14 private double term; // Payment Term 15 private double payment; // Monthly Payment 16 private int loanYears; // Years of Loan 17 18 /** 19 * The constructor uses three parameters: loan, 20 * which is the loan amount, rate, which is the 21 * annual interest rate, and years, which is the 22 * number of years of the loan. These values are 23 * stored in the corresponding fields. The private 24 * calcPayment method is then called. 25 */ 26 27 public Amortization(double loan, double rate, int years) 28 { 29 loanAmount = loan; 30 loanBalance = loan; 31 interestRate = rate; 32 loanYears = years; 33 calcPayment(); 34 } 35 36 /** 37 * The calcPayment method is used to calculate the 38 * monthly payment amount. The result is stored in the 39 * payment field. 40 */ 41 42 private void calcPayment() 43 { 44 // Calculate value of Term 45 term = 46 Math.pow((1+interestRate/12.0), 12.0 * loanYears); 47 48 // Calculate monthly payment 49 payment = 50 (loanAmount * interestRate/12.0 * term) / (term - 1); 51 } 52 53 /** 54 * The getNumberOfPayments method returns the number of 55 * loan payments. 56 */ 57

Case Study 1 The Amortization Class 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 public int getNumberOfPayments() { return 12 * loanYears; } /** * The saveReport method saves the amortization report to * the file named by the filename argument. */ public void saveReport(String filename) throws IOException { double monthlyInterest; // The monthly interest rate double principal; // The amount of principal // Create a DecimalFormat object for output formatting. DecimalFormat dollar = new DecimalFormat("#,##0.00"); // Create objects necessary for file output. FileWriter fwriter = new FileWriter(filename); PrintWriter outputFile = new PrintWriter(fwriter); // Print monthly payment amount. outputFile.println("Monthly Payment: $" + dollar.format(payment)); // Print the report header. outputFile.println("Month\tInterest\tPrincipal\tBalance"); outputFile.println("-----------------------------------" + "--------------"); // Display the amortization table. for (int month = 1; month <= getNumberOfPayments(); month++) { // Calculate monthly interest. monthlyInterest = interestRate / 12.0 * loanBalance; if (month != getNumberOfPayments()) { // Calculate payment applied to principal principal = payment - monthlyInterest; } else // This is the last month. { principal = loanBalance; payment = loanBalance + monthlyInterest; }

CS1-5

CS1-6

Case Study 1 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 }

The Amortization Class // Calculate the new loan balance. loanBalance -= principal; // Display a line of data. outputFile.println(month + "\t" + dollar.format(monthlyInterest) + "\t\t" + dollar.format(principal) + "\t\t" + dollar.format(loanBalance)); } // Close the file. outputFile.close();

} /** * The getLoanAmount method returns the loan amount. */ public double getLoanAmount() { return loanAmount; } /** * The getInterestRate method returns the interest rate. */ public double getInterestRate() { return interestRate; } /** * The getLoanYears method returns the years of the loan. */ public int getLoanYears() { return loanYears; }

Case Study 1 The Amortization Class

CS1-7

The Main Program


The main program code is shown in Code Listing CS1-2. First, it gets the amount of the loan, the annual interest rate, and the years of the loan as input from the user. It then creates an instance of the Amortization class and passes this data to the classs constructor. The program then saves the amortization report in the file LoanAmortization.txt. It asks the user if he or she wants to run another report. If so, the program repeats these steps. Code Listing CS1-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

(LoanReport.java)

import java.util.Scanner; import java.io.*; /** * This program displays a loan amortization report. */ public class LoanReport { public static void main(String [] args) throws IOException { double loan; // Loan amount double interestRate; // Annual interest rate int years; // Years of the loan String again; // Should the loop repeat? // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); do { // Get the loan amount. System.out.print("Loan amount: "); loan = keyboard.nextDouble(); // Validate the loan amount. // (No negative amounts.) while (loan < 0) { System.out.println("Invalid amount."); System.out.print("Loan amount: "); loan = keyboard.nextDouble(); } // Get the annual interest rate. System.out.print("Annual interest rate: "); interestRate = keyboard.nextDouble();

CS1-8

Case Study 1 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

The Amortization Class // Validate the interest rate // (No negative amounts.) while (interestRate < 0) { System.out.println("Invalid amount."); System.out.print("Annual interest rate: "); interestRate = keyboard.nextDouble(); } // Get the years of the loan. System.out.print("Years of Loan: "); years = keyboard.nextInt(); // Validate the number of years. // (No negative amounts.) while (years < 0) { System.out.println("Invalid amount."); System.out.print("Years of loan: "); years= keyboard.nextInt(); } // Create and initialize an Amortization object. Amortization am = new Amortization(loan, interestRate, years); // Save the report. am.saveReport("LoanAmortization.txt"); System.out.println("Report saved to the file " + "LoanAmortization.txt."); // Do another report? System.out.print("Would you like to run another " + "report? Enter Y for yes or " + "N for no: "); keyboard.nextLine(); // Consume the remaining newline. again = keyboard.nextLine(); } while (again.charAt(0) == 'Y' || again.charAt(0) == 'y');

} }

Program Output with Example Input Shown in Bold


Loan amount: 5000 [Enter] Annual interest rate: 0.059 [Enter] Years of Loan: 1 [Enter] Report saved to the file LoanAmortization.txt. Would you like to run another report? Enter Y for yes or N for no: n [Enter]

Case Study 1 The Amortization Class

CS1-9

Contents of the file LoanAmortization.txt:


Monthly Payment: $430.10 Month Interest Principal Balance ------------------------------------------------1 24.58 405.52 4,594.48 2 22.59 407.51 4,186.97 3 20.59 409.52 3,777.45 4 18.57 411.53 3,365.92 5 16.55 413.55 2,952.37 6 14.52 415.59 2,536.78 7 12.47 417.63 2,119.15 8 10.42 419.68 1,699.47 9 8.36 421.75 1,277.72 10 6.28 423.82 853.90 11 4.20 425.90 428.00 12 2.10 428.00 0.00

First, notice that a do-while loop (in lines 21 through 78) controls everything done in this program. Here is a condensed version of the loop:
do {

(Code to get the loan data and display the report appears here).
// Do another report? System.out.print("Would you like to run another " + "report? Enter Y for yes or " + "N for no: "); keyboard.nextLine(); // Consume the remaining newline. again = keyboard.nextLine(); } while (again.charAt(0) == 'Y' || again.charAt(0) == 'y');

During each iteration, this loop first gathers the necessary loan data as input and then writes the amortization report. At the end of the iteration, the user is asked if he or she wants to run another report. If the user enters Y or y for yes, the loop repeats. Otherwise, the loop terminates and the program ends. Inside the loop, the code that gathers input from the user also validates the input. For example, here is the code in lines 23 through 34:
// Get the loan amount. System.out.print("Loan amount: "); loan = keyboard.nextDouble(); // Validate the loan amount. // (No negative amounts.) while (loan < 0) {

CS1-10 Case Study 1

The Amortization Class System.out.println("Invalid amount."); System.out.print("Loan amount: "); loan = keyboard.nextDouble(); }

If the user enters a negative number, the while loop displays an error message and asks the user to enter the loan amount again. Here is an example session with the code where the user enters a negative number.
Loan amount: -5000 [Enter] Invalid amount. Loan amount: 5000 [Enter]

The code that gets the annual interest rate and the years of the loan also performs similar input validation. Once these values have been correctly entered, an instance of the Amortization class is created in lines 63 through 64:
Amortization am = new Amortization(loan, interestRate, years);

In line 67, the saveReport method is called to save the amorization report to the file
LoanAmortization.txt

You might also like