#Interactive Menu Best practices

1 messages · Page 1 of 1 (latest)

snow echo
#

I currently have an interactive menu class that I setup to allow a user to specify a specific operation but I'm wondering the best way to test it. I have this method for example:

  public void execute() {
    while (true) {
      System.out.println(
          """
              Options:
                1. Modify Table
                2. Modify Seed
                3. Modify Dependency
                4. Print Configurations
                5. Exit Modify Entity Menu""");
      System.out.print("Option: ");
      if (!scanner.hasNextInt()) {
        System.out.println("Invalid Option number, please try it again");
        scanner.nextLine();
        continue;
      }
      int optionSelected = scanner.nextInt();
      scanner.nextLine();
      CommandInvoker commandInvoker = new CommandInvoker();
      switch (optionSelected) {
        case 1 ->
            commandInvoker.setInteractiveCommand(
                new ModifyTableSubcommand(interactiveEntityUtility));
        case 2 ->
            commandInvoker.setInteractiveCommand(
                new ModifySeedSubcommand(interactiveEntityUtility));
        case 3 ->
            commandInvoker.setInteractiveCommand(
                new ModifyDependencySubcommand(interactiveEntityUtility));
        case 4 -> commandInvoker.setInteractiveCommand(new PrintContextCommand(interactiveEntityUtility));
        case 5 -> {
          continue;
        }
      }
      commandInvoker.executeCommand();
      break;
    }
  }

which works pretty well since it doesn't require me to change how the command works in this class. I just pass in the correct class using the command pattern and let it take over. My question is, if I want to add an option 5 and push back the exit option, I will break all of my tests so how should I handle this?

woeful waveBOT
#

<@&987246399047479336> please have a look, thanks.