Connecting Tech Pros Worldwide Help | Site Map

Display Menu

Newbie
 
Join Date: Jan 2009
Posts: 24
#1: May 22 '09
Hi All

I'm a total newbie to Java.

I'm creating a very simple banking system as a college project. I'm doing ok... logging in the user, the displaying a Main Menu with options to select from. However Im using a If statement for the selection and once I have selected an option (for example = check account balance) I cant go back to the Main Menu (unless I write the whole thing out again, which would be daft).

I have tried to write a method (?) to display the menu, I can then put this at the end of my if statement to return the user to the Main Menu. However I can get it to work... Below is my menu code and below that the code i attempted for the method, if I can resolve this then im pretty much flying at the minute its the only thing holding me back!!!

Expand|Select|Wrap|Line Numbers
  1. //Show menu and get users choice
  2.         accChoice = JOptionPane.showInputDialog(null, "Account Menu \n" +
  3.                                           "What would you like to do? \n" +
  4.                                           " 1 - Check Balance.\n" +
  5.                                           " 2 - Make A Withdrawl.\n" +
  6.                                           " 3 - Make A Deposit.\n"+
  7.                                           " 4 - Amend Overdraft.\n" +
  8.                                           " 5 - Exit.\n" +
  9.                                           "Please enter your choice:");
  10.  
  11.     //direct users choice
  12.         if (accChoice.equals(accChoice1))
  13.         {
  14.             System.out.println("1 - Check Balance.");
  15.         }
  16.         else if (accChoice.equals(accChoice2))
  17.         {
  18.             System.out.println("2 - Make A Withdrawl");
  19.         }
  20.         else if (accChoice.equals(accChoice3))
  21.         {
  22.             System.out.println("3 - Make A Deposit.");
  23.         }
  24.         else if (accChoice.equals(accChoice4))
  25.         {
  26.             System.out.println("4 - Amend Overdraft.");
  27.         }
  28.         else if (accChoice.equals(accChoice5))
  29.         {
  30.             System.out.println("5 - Exit");
  31.         }
below is my method(s) code:

Expand|Select|Wrap|Line Numbers
  1. private String displayMainMenu()
  2.     {
  3.         String accChoice;
  4.         accChoice = JOptionPane.showInputDialog(null, "Account Menu \n" +
  5.                                           "What would you like to do? \n" +
  6.                                           " 1 - Check Balance.\n" +
  7.                                           " 2 - Make A Withdrawl.\n" +
  8.                                           " 3 - Make A Deposit.\n"+
  9.                                           " 4 - Amend Overdraft.\n" +
  10.                                           " 5 - Exit.\n" +
  11.                                           "Please enter your choice:");
  12.         return accChoice;
  13.  
  14.    }
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: May 30 '09

re: Display Menu


If you can wait one more day or so then I'll post a little article about this topic because I've seen people struggle with this item more than often. The solution (which you can reuse over and over again!) will be a bit of code that represents a single menu item as well as an entire menu. You can nest menus at will and put in "back" and "quit" menu items whereever you want.

kind regards,

Jos

edit: done; read the 'insights' section of the Java forum: it contains a little article about this very topic.
Reply