Results 1 to 3 of 3
Thread: Menu-driven program
- 01-24-2012, 10:35 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
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.
Can anyone help me find where I went wrong?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"); } } }
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.
- 01-24-2012, 10:40 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,451
- Rep Power
- 16
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).
- 01-24-2012, 10:44 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Problem with menu-driven program
By t0nydanzuh in forum New To JavaReplies: 14Last Post: 09-30-2010, 09:43 PM -
Menu-driven console help please
By floppyspace in forum New To JavaReplies: 4Last Post: 03-12-2010, 04:12 PM -
how to create menu-driven programs?
By princess.blue in forum EclipseReplies: 0Last Post: 12-07-2009, 08:01 AM -
Menu driven system - atm
By thelinuxguy in forum Advanced JavaReplies: 1Last Post: 02-18-2009, 11:14 PM -
want menu driven prog. who to take user i/p in the middle of the prog.
By Shyam Singh in forum New To JavaReplies: 1Last Post: 07-13-2008, 03:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks