5 Replies - 611 Views - Last Post: 13 January 2014 - 06:57 AM Rate Topic: -----

#1 sanchez67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-December 13

Missing the code that allows me to go back to my program menu ?

Posted 13 January 2014 - 06:36 AM

This has been my code so far for my Java Program. I am writing a Bank Interest Program for my project and when I ran my program, it shows me the menu of my program, which is this:

1) Calculate interest payable
2) Calculate principle require to earn interest
3) Calculate time required to earn interest
4) Exit program
Please enter your choice <1-4>:


This output is correct.

But the problems are in my next few statements. To make it short, I have two problems with my java program. I am troubled as on how do I write the code that allows me to go back to my program menu after I had made a selection [ Problem 1] ! When I chose option 1 ( which is this ),
1) Calculate interest payable
2) Calculate principle require to earn interest
3) Calculate time required to earn interest
4) Exit program
Please enter your choice <1-4>:
1
Enter principle amount in $:
20000
Enter interest rate in %:
5
Enter period (time) in days:
10
Interest earned is $27.397260273972602



it stops and I was not directed back to my program's menu for another selection. Did I miss out on any code for my program?

On line 34 of my output, which is the formula for calculating the bank interest incurred on any amount, I was confused.

1) Calculate interest payable
2) Calculate principle require to earn interest
3) Calculate time required to earn interest
4) Exit program
Please enter your choice <1-4>:
2
 Enter interest amount to be earned:
22000
Enter interest rate in %:
500
 Enter period(time) in days:
50
You need a principle of $0.0



How come the calculation is $0 of principle when at line 34, it clearly states the formula that is being used to calculate the principle needed, which is
Formula:
interestEarned=principle*(rate/100)*(days/365);
(Problem 2)
For choice 1 of my menu, I have no problems getting the figure. But, for choice 2 and 3 of my program, the figure is $0.
Are there any errors with my program ? Below are my full codes for the program to help you guys understand better.

import java.util.*;
public class BankInterest 
{
   static Scanner input = new Scanner(System.in); 
   Scanner scanner = new Scanner(System.in); 

static double principle, rate, days, interestEarned, choice; 

public static void main(String[] args) 
{
	  display_menu();
       }//end of main
static void display_menu()
{  
	   System.out.println("1) Calculate interest payable");
	   System.out.println("2) Calculate principle require to earn interest");
	   System.out.println("3) Calculate time required to earn interest");
	   System.out.println("4) Exit program");
	    //Prompt for choice 
	   System.out.println("Please enter your choice <1-4>:");
	   
Scanner q = new Scanner(System.in);
   
switch (q.nextInt()) 
{
case 1: 
	System.out.println("Enter principle amount in $:");
	principle=input.nextDouble();
	System.out.println("Enter interest rate in %:");
	rate=input.nextDouble();
	System.out.println("Enter period (time) in days:");
	days=input.nextDouble();
	//Calculation
	interestEarned=principle*(rate/100)*(days/365);
	System.out.println("Interest earned is $"+ interestEarned);  
	break;

case 2: 
	System.out.println(" Enter interest amount to be earned:");
	interestEarned=input.nextDouble();
	System.out.println("Enter interest rate in %:");
	rate=input.nextDouble();
	System.out.println(" Enter period(time) in days:");
	days=input.nextDouble();
	//Calculation
	interestEarned=principle*(rate/100)*(days/365);
	System.out.println("You need a principle of $"+ interestEarned); 
	break;


case 3: 
	System.out.println("Enter interest amount to be earned:");
	interestEarned=input.nextDouble();
	System.out.println("Enter interest rate in %:");
	rate=input.nextDouble();
	System.out.println("Enter principle amount in $:");
	principle=input.nextDouble();
	//Calculation
	interestEarned=principle*(rate/100)*(days/365);
	System.out.println("Days required to earn interest is "+days);
	days=input.nextDouble();
	break;
	
case 4: 
System.out.println("Thank you for using the Bank Interest Program, we hope to see you again.");
break;
        
default:
System.err.println ( "Invalid choice.  Please enter it again:" );
choice=input.nextInt();
break;
}
}

public BankInterest()
{
Scanner in = new Scanner(System.in);
    display_menu();

switch (in.nextInt()) 
{
    case 1:
    System.out.println ( "You picked option 1" );
    display_menu();
    break;

    case 2:
    System.out.println ( "You picked option 2" );
    display_menu();
    break;

    case 3:
    System.out.println ( "You picked option 3" );
    display_menu();
    break;
    
    default:
    System.err.println ( "Unrecognized option" );
    break;
}
}
public static void main1 (String[]args) 
{
 display_menu();
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Missing the code that allows me to go back to my program menu ?

#2 yashwanth.c.b  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Missing the code that allows me to go back to my program menu ?

Posted 13 January 2014 - 06:43 AM

why have you made most of the fields static..i don't get you..
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7070
  • View blog
  • Posts: 14,749
  • Joined: 16-October 07

Re: Missing the code that allows me to go back to my program menu ?

Posted 13 January 2014 - 06:47 AM

You seem to have two programs squished into one, here. Get rid of all the static. Make an instance of your class. Make methods for each of you menu options. Stop making all those scanners! You only need one.

And don't do all that work in a constructor! Not that you ever call it...
Was This Post Helpful? 0
  • +
  • -

#4 yashwanth.c.b  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Missing the code that allows me to go back to my program menu ?

Posted 13 January 2014 - 06:50 AM

sorrry,i meant to give you a +1
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6505
  • View blog
  • Posts: 26,306
  • Joined: 12-December 12

Re: Missing the code that allows me to go back to my program menu ?

Posted 13 January 2014 - 06:54 AM

The specific issue with problem 2 is that you never ask for the principle, so it still has its default 0 value.

For problem 1, after all the above advice is followed ;), you don't have any kind of loop, which you will need if you want to repeatedly ask for an option.

View Postyashwanth.c.b, on 13 January 2014 - 01:50 PM, said:

sorrry,i meant to give you a +1

I've upped ;)
Was This Post Helpful? 0
  • +
  • -

#6 yashwanth.c.b  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Missing the code that allows me to go back to my program menu ?

Posted 13 January 2014 - 06:57 AM

Quote

I've upped ;)

thank you and sorry.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1