0

I am writing a program where the user inputs a choice in the main menu and it takes them to another sub-menu. I want one of the options to be "return to previous menu" i.e. the menu first shown to the user. I can not really figure out how to do this in the way I have set up my code. Here is my code for the PrintMenu method:

public static void PrintMenu(){

    Scanner input = new Scanner (System.in);
    
    int option = 0;
    while(option != 3){
        System.out.println("Menu Please enter an option given bellow: ");
        System.out.println("Option     Operation Completed");
        System.out.println("--------------------------------");
        System.out.println("1       Stack");
        System.out.println("2       Queue");
        System.out.println("3       Quit");
        
        option = input.nextInt();
        
        switch(option){
                    
                    case 1:
                    {
                        //add code for stack
                        System.out.println("You are currently using a stack. \n");
                        System.out.println("Enter the maximum size you want for your stack: ");
                        maxSize = input.nextInt();
                        
                        int option_stack = 0;
                        while(option_stack != 5){
                            System.out.println("Menu Please enter an option given 
                            bellow: ");
                            System.out.println("Option     Operation Completed");
                            System.out.println("--------------------------------");
                            System.out.println("1       Add to Stack");
                            System.out.println("2       Remove from Stack");
                            System.out.println("3       Clear Stack");
                            System.out.println("4       Return to previous menu");
                            System.out.println("5       Quit");

                            option_stack = input.nextInt();

                            switch(option_stack){

                                        case 1:
                                        {
                                            //add code for Add to Stack
                                            break;
                                        }   
                                        case 2:
                                        {
                                            //add code for Remove from Stack
                                            break;
                                        }
                                        case 3:
                                        {
                                            //add code for Clear Stack
                                            break;
                                        }
                                        case 4:
                                        {
                                            //add code for Return to previous menu
                                            break;
                                        }
                                        case 5:
                                            System.exit(0);
                                            break;

                                        //Error message if user inputs anything other 
                                        than 1-5 
                                        default:
                                            System.out.println(option + " is not a 
                                            correct choice.\n"
                                                               + "please enter 
                                            another option. \n");
                                            break;
                            }
                        }
                        break;
                    }   
                    case 2:
                    {
                        //add sub-menu and corresponding code for queue in the same 
                        //way as stack
                    }
                        
    }
}

What can I put in case 4 that would take the user to the main menu?

Jens
  • 67,715
  • 15
  • 98
  • 113
joker5309
  • 11
  • 3

1 Answers1

0

A good point to start is this skeleton below.

Let me explain some points:

  1. Don't use static methods if you have no good reason to do! Please have a look to this discussion

  2. Keep your switch statements as short as possible and avoid nesting! The longer the switch statement the more difficult is it to keep track of it. The deeper the nesting the higher the complexity. A good way to avoid both is to put the code of each case in a seperate method or to encapsulate in it's own class.

  3. Don't use curly brackets for case blocks. They are avoidable noise (especially if you follow bullet point 2).

  4. In the Main class create a instance of it self to get out of the 'static trap'.

The startable Main class containing the main menu:

package joker5309;

import java.util.Scanner;

public class Menu {

    public void mainMenu() {

        Scanner input = new Scanner(System.in);

        int option = 0;

        while (option != 3) {
            System.out.println("do stuff for main menu ... ");

            System.out.println("Menu Please enter an option given bellow: ");
            System.out.println("Option     Operation Completed");
            System.out.println("--------------------------------");
            System.out.println("1       Stack");
            System.out.println("2       Queue");
            System.out.println("3       Something else");
            System.out.println("4       Quit");

            option = input.nextInt();

            switch (option) {

            case 1:
                StackHandler stackHandler = new StackHandler();
                stackHandler.doIt(input);
                break;
            
            case 2:
                // add sub-menu and corresponding code for queue in the same
                // way as stack
                QueueHandler queueHandler = new QueueHandler();
                queueHandler.doIt(input);
                break;
                    
            case 3:
                // add any other submenue ...
                SomethingElseHandler seHandler = new SomethingElseHandler();
                seHandler.doIt(input);
                break;
                
            case 4:
                System.exit(0);
                
            } // hctiws
            
        } // elihw
        
    } // mainMenu()

    
    public static void main(String[] args) {

        // create an instance of Menu class to leave the 'static trap'
        Menu mySelf = new Menu();
        mySelf.mainMenu();

    } // main()
    

} // sslac

A seperate handler class for each meun item:

package joker5309;

import java.util.Scanner;

public class StackHandler implements Handler {

    @Override
    public void doIt(Scanner input) {
        // add code for stack
        System.out.println("You are currently using a stack. \n");
        System.out.println("Enter the maximum size you want for your stack: ");
        input.nextInt();

        int optionStack = 0;
        
        while (true) {
            System.out.println("do stuff for stack ... ");

            System.out.println("Menu Please enter an option given bellow: ");
            System.out.println("Option     Operation Completed");
            System.out.println("--------------------------------");
            System.out.println("1       Add to Stack");
            System.out.println("2       Remove from Stack");
            System.out.println("3       Clear Stack");
            System.out.println("4       Return to previous menu");
            System.out.println("5       Quit");

            optionStack = input.nextInt();

            switch (optionStack) {

            case 1: 
                // add code for Add to Stack
                stackAdd();
                break;

            case 2:
                // add code for Remove from Stack
                stackRemove();
                break;

            case 3:
                // add code for Clear Stack
                stackClear();
                break;

            case 4:
                // add code for Return to previous menu
                return;

            case 5:
                System.exit(0);
                break;

            // Error message if user inputs anything other than 1-5
            default:
                System.out.println(optionStack + " is not a  correct choice.\n" + "please enter  another option. \n");
                break;
                
            } // hctiws
            
        } // elihw
        
    } // stackParameter()
    

    private void stackAdd() {
        System.out.println("stackAdd()");
    }


    private void stackRemove() {
        System.out.println("stackRemove()");
    }


    private void stackClear() {
        System.out.println("stackClear()");
    }

    
}

package joker5309;

import java.util.Scanner;

public class QueueHandler implements Handler {

    @Override
    public void doIt(Scanner input) {
        
        int optionQueue = 0;
        
        while (true) {
            System.out.println("do stuff for queue ... ");

            System.out.println("Menu Please enter an option given bellow: ");
            System.out.println("Option     Operation Completed");
            System.out.println("--------------------------------");
            System.out.println("1       ...");
            System.out.println("2       Return to previous menu");
            System.out.println("3       Quit");

            optionQueue = input.nextInt();

            switch (optionQueue) {

            case 1: 
                // add code for ...
                break;

            case 2:
                // add code for Return to previous menu
                return;

            case 3:
                System.exit(0);
                break;

            // Error message if user inputs anything other than 1-5
            default:
                System.out.println(optionQueue + " is not a  correct choice.\n" + "please enter  another option. \n");
                break;
                
            } // hctiws
            
        } // elihw
        
    } // queueParameter()

}

package joker5309;

import java.util.Scanner;

public class SomethingElseHandler implements Handler {

    @Override
    public void doIt(Scanner input) {
        
        int optionStack = 0;
        
        while (true) {
            System.out.println("do stuff for something else ... ");

            System.out.println("Menu Please enter an option given bellow: ");
            System.out.println("Option     Operation Completed");
            System.out.println("--------------------------------");
            System.out.println("1       ...");
            System.out.println("2       Return to previous menu");
            System.out.println("3       Quit");

            optionStack = input.nextInt();

            switch (optionStack) {

            case 1: 
                // add code for ...
                break;

            case 2:
                // add code for Return to previous menu
                return;

            case 3:
                System.exit(0);
                break;

            // Error message if user inputs anything other than 1-5
            default:
                System.out.println(optionStack + " is not a  correct choice.\n" + "please enter  another option. \n");
                break;
                
            } // hctiws
            
        } // elihw
        
    } // queueParameter()
    

}

Stefan D.
  • 299
  • 3
  • 8