Results 1 to 3 of 3
  1. #1
    Link is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Menu-driven program

    Hi, I'm supposed to write a loan calculator menu-driven program.
    The user inputs the payment amount, annual interest rate (which needs to be converted to months), and the loan period in months.

    Below is what I've written so far but my program outputs all the wrong numbers.
    When I try to calculate the PAYMENT it always spits out -$0.00, the INTEREST just returns what I input as the interest rate, and the TOTAL is always $0.00.

    Java Code:
    import java.util.*;
    import java.text.DecimalFormat;
    
    public class Menu {
    	public static void main(String[] args) {
    
    		// initialize money format
    		DecimalFormat money = new DecimalFormat("$0.00");
    		
    		// variables
    		String input;
    	    char inputCode = ' ' ;
    	    double PRINCIPLE = 0;
    		double RATE = 0;
    		int TERM = 0;
    
    		// equations
    		double PAYMENT = PRINCIPLE * ((RATE / 1200) / 1 - Math.pow(1 + (RATE / 1200), -TERM));
    		double INTEREST = TERM * PAYMENT - PRINCIPLE;
    		double TOTAL = PRINCIPLE + INTEREST;
    	    
    		/* menu */
    		System.out.println("Enter [p] for payment amount");
    		System.out.println("Enter [i] for interest rate");
    		System.out.println("Enter [t] for total");
    		System.out.println("Enter [q] to terminate program");
    		System.out.print("\nPlease enter your choice: ");
    	
    		// create Scanner object for keyboard input
    		Scanner sc = new Scanner(System.in);
    	    input = sc.nextLine();
    	    inputCode = input.charAt(0);
    	    
    	    while ((inputCode != 'p' && inputCode != 'i' && inputCode != 't' && inputCode !='q') &&
    	    	   (inputCode != 'P' && inputCode != 'I' && inputCode != 'T' && inputCode !='Q')) {
    	      System.err.println("Invalid choice! Please select p, i, t, or q: ");
    	      input = sc.nextLine();
    	      inputCode = input.charAt(0);
    	    }
    	   
    	    /* display results */
    	    switch (inputCode) {
    	    	case 'p':
    	    	case 'P':
    	    		System.out.print("\nEnter principle (loan amount), e.g. 120000.95: ");
    	    		PRINCIPLE = sc.nextDouble();
    	    		System.out.print("Enter annual interest rate (between 1-100), e.g. 5.25: ");
    	    		RATE = sc.nextDouble();
    	    		if (RATE < 1 || RATE > 100) {
    	    	    	System.err.println("Invalid input!  Please re-enter annual interest rate: ");
    	    	    	RATE = sc.nextDouble();
    	    		}
    	    		System.out.print("Enter term (loan period) in months as an integer, e.g. 8: ");
    	    		TERM = sc.nextInt();
    	    		System.out.println("\nThe payment amount is " + money.format(PAYMENT));
    	    		break;
    	    	case 'i':
    	    	case 'I':
    	    		System.out.print("\nEnter (principle) loan amount, e.g. 125000.00: ");
    	    		PRINCIPLE = sc.nextDouble();
    	    		System.out.print("Enter annual interest rate (between 1-100), e.g. 5.25: ");
    	    		RATE = sc.nextDouble();
    	    		if (RATE < 1 || RATE > 100) {
    	    	    	System.err.println("Invalid input!  Please re-enter annual interest rate: ");
    	    	    	RATE = sc.nextDouble();
    	    		}
    	    		System.out.print("Enter term (loan period) in months as an integer , e.g. 8: ");
    	    		TERM = sc.nextInt();
    	    		System.out.println("\nThe interest rate is " + RATE);
    	    		break;
    	    	case 't':
    	    	case 'T':
    	    		System.out.print("\nEnter (principle) loan amount, e.g. 125000.00: ");
    	    		PRINCIPLE = sc.nextDouble();
    	    		System.out.print("Enter annual interest rate (between 1-100), e.g. 5.25: ");
    	    		RATE = sc.nextDouble();
    	    		if (RATE < 1 || RATE > 100) {
    	    	    	System.err.println("Invalid input!  Please re-enter annual interest rate: ");
    	    	    	RATE = sc.nextDouble();
    	    		}
    	    		System.out.print("Enter term (loan period) in months as an integer, e.g. 8: ");
    	    		TERM = sc.nextInt();
    	    		System.out.println("\nThe total amount is " + money.format(TOTAL));
    	    		break;
    	    	case 'q':
    	    	case 'Q':
    	    		System.out.println("Program terminated");
    	    }
    	    
    	}
    }
    Can anyone help me find where I went wrong?

    I know it's terrible that I'm struggling with basic concepts but I've never taken a programming course before and this class also has a prereq that I didn't take. I best learn through example so any help or advice to make my code more efficient will be appreciated!
    Last edited by Link; 01-24-2012 at 11:01 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Menu-driven program

    You're doing all your calculations before you have got any data from the user, so all the sums are done based on the default values you gave the variables (usually 0).

  3. #3
    Link is offline Member
    Join Date
    Jan 2012
    Posts
    3
    Rep Power
    0

    Default Re: Menu-driven program

    Awesome, I'm finally getting real numbers!
    I took out the zeros behind my variables but I'm still not getting the right answers.
    Thanks for getting me started though.

Similar Threads

  1. Problem with menu-driven program
    By t0nydanzuh in forum New To Java
    Replies: 14
    Last Post: 09-30-2010, 09:43 PM
  2. Menu-driven console help please
    By floppyspace in forum New To Java
    Replies: 4
    Last Post: 03-12-2010, 04:12 PM
  3. how to create menu-driven programs?
    By princess.blue in forum Eclipse
    Replies: 0
    Last Post: 12-07-2009, 08:01 AM
  4. Menu driven system - atm
    By thelinuxguy in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2009, 11:14 PM
  5. Replies: 1
    Last Post: 07-13-2008, 03:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •