简体   繁体   English

我如何继续实施子菜单? Java 终端菜单

[英]How do I proceed to implement sub menu's? Java terminal Menu

This is the menu as of now.这是目前的菜单。 I want to create sub menu's to ship and order.我想创建子菜单来发货和订购。 Im really stuck on how to do that.我真的坚持如何做到这一点。 When i choose the ship alternative i want another menu that shows the different ships, 10 of them to be specific.当我选择船舶替代方案时,我想要另一个显示不同船舶的菜单,其中 10 艘是具体的。

What is the best way of doing that?这样做的最佳方法是什么?

Thanks in advance!提前致谢!

import java.util.Scanner;

public class Main {
boolean exit;

public static void main(String[] args) {
    Main main = new Main();
    main.runMenu();


}

public void runMenu() {
    printHeader();
    while(!exit) {
        printMenu();
        int choice = getInput();
        performAction(choice);
    }
}

private void printHeader() {
    System.out.println("+-----------------------------------+");
    System.out.println("|          Welcome operator!        |");
    System.out.println("+-----------------------------------+");
}

private void printMenu() {
    System.out.println("\nMake your selection");
    System.out.println("1) Ship");
    System.out.println("2) Order");
    System.out.println("3) Map");
    System.out.println("4) Status");
    System.out.println("0) Exit");
}

private int getInput() {
    Scanner kb = new Scanner(System.in);
    int choice = -1;
    while(choice < 0 || choice > 4) {
        try {
            System.out.print("\nEnter your choice");
            choice = Integer.parseInt(kb.nextLine());
        }
        catch (NumberFormatException e) {
            System.out.println("Invalid selection. Please try again");
        }
    }
    return choice;
}

Here I want to call the sub menus:这里我想调用子菜单:

private void performAction(int choice) {
    switch(choice) {
        case 0:
            exit = true;
            System.out.println("Thank your for using our simulator");
            break;
        case 1:
            shipMenu();
            break;
        case 2:
            orderMenu();
            break;
        case 3:
            //map();
        case 4:
            //status();
        default:
            System.out.println("Unknown error has occured");
    }
}

Tell me if this isn't quite what you're looking for.告诉我这是否不是您要找的东西。

After the user makes a selection you can clear the console using用户做出选择后,您可以使用清除控制台

new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();

and then print out the next submenu.然后打印出下一个子菜单。

For example (this method can get very lengthy, but it works for simple text menus):例如(此方法可能会变得非常冗长,但它适用于简单的文本菜单):

public static void main(String[] args) {
     while (true) {
     new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
     System.out.println("1. Food");
     System.out.println("2. Drink");
     System.out.println("3. Quit");

     //get user input

      if (userInput == 1) {         //food submenu (submenus can be easily put in functions)
          new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
          System.out.println("1. Apple");
          System.out.println("2. Banana");
          System.out.println("3. Back");

          //get and process input
      } else if (userInput == 2) {         //drink submenu (submenus can be easily put in functions)
          new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
          System.out.println("1. Water");
          System.out.println("2. Juice");
          System.out.println("3. Back");

          //get and process input
      } else break;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM