Display Menu

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Newbie23
    New Member
    • Jan 2009
    • 25

    Display Menu

    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!!!

    Code:
    //Show menu and get users choice
            accChoice = JOptionPane.showInputDialog(null, "Account Menu \n" +
                                              "What would you like to do? \n" +
                                              " 1 - Check Balance.\n" +
                                              " 2 - Make A Withdrawl.\n" +
                                              " 3 - Make A Deposit.\n"+
                                              " 4 - Amend Overdraft.\n" +
                                              " 5 - Exit.\n" +
                                              "Please enter your choice:");
    
        //direct users choice
            if (accChoice.equals(accChoice1))
            {
                System.out.println("1 - Check Balance.");
            }
            else if (accChoice.equals(accChoice2))
            {
                System.out.println("2 - Make A Withdrawl");
            }
            else if (accChoice.equals(accChoice3))
            {
                System.out.println("3 - Make A Deposit.");
            }
            else if (accChoice.equals(accChoice4))
            {
                System.out.println("4 - Amend Overdraft.");
            }
            else if (accChoice.equals(accChoice5))
            {
                System.out.println("5 - Exit");
            }
    below is my method(s) code:

    Code:
    private String displayMainMenu()
        {
            String accChoice;
            accChoice = JOptionPane.showInputDialog(null, "Account Menu \n" +
                                              "What would you like to do? \n" +
                                              " 1 - Check Balance.\n" +
                                              " 2 - Make A Withdrawl.\n" +
                                              " 3 - Make A Deposit.\n"+
                                              " 4 - Amend Overdraft.\n" +
                                              " 5 - Exit.\n" +
                                              "Please enter your choice:");
            return accChoice;
            
       }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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.

    Comment

    • luckywin
      New Member
      • Jul 2014
      • 1

      #3
      Hi Josah,

      Can I check where I can find the article on the display menu in java? Thanks!!

      Comment

      Working...