Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Another Mortgage Calculator Question

807591Jun 17 2008 — edited Jun 17 2008
I have been working for 5 days on my calcluator program. Below is the problem presented:

Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:

- 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%

Use an array for the different loans. Display the mortgage payment amount for each selected loan, which is picked at the run time of the program.

Below is my program. Everything runs just fine. I started out just doing a menu to make sure it was presented properly, then I created the arrays and when I ran that, I got three different lines (corect results) from calculating each year with it's respective rate.

What is happening now, is that I am gettting the same result no matter whether I pick 1, 2 or 3 from the menu and if I choose 4, it just returns the menu. Everything else seems to be working. Could someone please take a look and tell me if I have left something out.... It would be most appreciated.

Code is below
-------------------------------------------------------------

/**
* File name: Menu_Driven_Mortgage_Calculator.java
*
* Mortgage Calculator for Week 4
*
* Sandra Manchor
* version 4.00 June 16, 2008

*/

//Import Files

import java.math.*; //*loan calculator
import java.text.NumberFormat; //for currency format
import java.util.*; //for Scanner

class Menu_Driven_Mortgage_Calculator
{
public static void main(String[] arguments)
{
double amount = 200000;
int[] term = {7, 15, 30};
double[] rate = {5.35, 5.5, 5.75};


java.text.DecimalFormat dfm = new java.text.DecimalFormat(",###.00");

for (int i = 0; i < term.length; i++)
{
double payment = (amount*(rate/100/12))/(1-(Math.pow(1/(1+(rate[i]/100/12)),(term[i]*12))));
Scanner sc = new Scanner(System.in); //reads value from the keyboard

int choice = 1;

// end if user picks 4 or with invalid entry, continue program otherwise
while ( choice != 4 )
{
do {
if (choice > 4 || choice <= 0)
System.out.println("\nPlease make the correct selection. \n");

ShowMenu();

try {
choice = sc.nextInt(); //read in a numeric value, a decimal or an integer
}
catch (InputMismatchException e){
System.out.println("\nInvalid Entry! You should enter an integer from 1 to 4.");
System.out.println("The program has now ended.\n");
System.exit(1);
}
} while ( choice < 0 || choice > 4 );

if ( choice != 4 )
switch (choice) { // branch to an appropriate selection
case 1:
System.out.print("You have selected #1. \nFor a " + term[i] + " year mortgage at a rate of "+ rate[i]);
System.out.println(", the monthly payment will be $" + dfm.format(payment));
break;
case 2:
System.out.print("You have selected #2. \nFor a " + term[i] + " year mortgage at a rate of "+ rate[i]);
System.out.println(", the monthly payment will be $" + dfm.format(payment));
break;
case 3:
System.out.print("You have selected #3. \nFor a " + term[i] + " year mortgage at a rate of "+ rate[i]);
System.out.println(", the monthly payment will be $" + dfm.format(payment));
break;

}
else //choice 4 was selected
System.out.println("\n\n Thank you for using the Mortgage Calculator Program.\n");

} // end of while

} // end of main
}
private static void ShowMenu()
{
System.out.println(" Please make selection from the following:\n");
System.out.println(" 1 Calculate payment for 7 year term at 5.53%.");
System.out.println(" 2 Calculate payment for 15 year term at 5.5%.");
System.out.println(" 3 Calculate payment for 30 year term at 5.75%.");
System.out.println(" \n 4 End program \n");
System.out.print("\nEnter your choice: ");

} // end function ShowMenu

} // end of class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 15 2008
Added on Jun 17 2008
2 comments
162 views