Home Programming Java Loan Calculator Project

Search

Java Loan Calculator Project Print
Written by Chris Gountanis   

This program asks user for input amount of the loan, the interest rate and the number of years. The program validates the input checking for valid numeric data before continuing. If the interest rate was entered in percentage form it converts to decimal before calculation process. The program then calculates the monthly payment. The monthly payment total and the input values are displayed rounded to the second decimal place.

 

 

Input

The inputs for this program are the loan amount, annual interest rate and the number of years for the loan. The interest rate can be entered as a decimal or percentage value. For example .065 or 6.5 will be accepted.

 

Output

The output for the program is the user defined input values as well as the calculated monthly payment.

 

Java Loan Calculator Project
Process

All the input values are accepted from the keyboard.  Error handling is in place to validate the input for numeric values. The program will not continue until a valid input is entered. The input values will be converted from string to a numeric data type. If the interest rate was entered in percentage form it converts to decimal before calculation process. The program uses the following formula for the calculation:

 

Java Loan Calculator Project

 

 

 

The final output can now be displayed as all the input and calculations are complete. Numeric values with more than 2 decimal places will be rounded to the second decimal place.

 

Design

Java Loan Calculator Project


Code

/*

Programmer: Chris Gountanis

Date: 02/08/2008

Program Name: LoanCalculator.java for Week 3 IA

Description: Program asks user for input amount of the loan,

the interest rate and the number of years. The program

validates the input checking for valid numeric data before

continuing. If the interest rate was entered in percentage

form it converts to decimal before calculation process. The

program then calculates the monthly payment. Input values and

payment total are displayed rounded to the second decimal place.

*/

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class LoanCalculator {

public static void main(String[] args) throws IOException {

//declarations

BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

double payment=0, interestrate=0, loanamount=0, years=0;

String inputvalue="";

 

//get input for loan amount

while (!isNumeric(inputvalue)) {

System.out.println("Loan Amount? ");

inputvalue = dataIn.readLine();

}

loanamount = Float.parseFloat(inputvalue);

 

//get input for interest rate

inputvalue="";

while (!isNumeric(inputvalue)) {

System.out.println("What is the Annual Interest Rate? ");

inputvalue = dataIn.readLine();

}

interestrate = Float.parseFloat(inputvalue);

 

//get input for the number of years

inputvalue="";

while (!isNumeric(inputvalue)) {

System.out.println("How many Years? ");

inputvalue = dataIn.readLine();

}

years = Float.parseFloat(inputvalue);

 

//if the interest rate was entered in as percentage convert to decimal

if (interestrate > 1) {

interestrate = interestrate / 100;

}

 

//calculate the monthly payment total

payment = (interestrate * loanamount / 12) /

(1.0 - Math.pow(((interestrate / 12) + 1.0), (-(12 * years))));

 

//output the total and input values

System.out.print("\nThe Loan Amount:\t\t$" + loanamount);

System.out.print("\nThe Interest Rate:\t\t" + Round(interestrate * 100, 2) + "%");

System.out.print("\nThe Number of Years:\t\t" + years + " (Months: " + years * 12 + ")");

System.out.print("\n\nThe Monthly Payment:\t\t$" + Round(payment, 2));

}

 

//used to validate for numeric data

private static boolean isNumeric(String str){

try {

Float.parseFloat(str);

return true;

} catch (NumberFormatException nfe){

return false;

}

}

 

//used to round to a certain number of decimal places

public static float Round(double Rval, int Rpl) {

float p = (float)Math.pow(10, Rpl);

Rval = Rval * p;

float tmp = Math.round(Rval);

return (float)tmp/p;

}

}

 

Downloads

LoanCalculator.java

Last Updated on Saturday, 09 February 2008 07:21