|
| 1 | +package org.ecn.medev.controller; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import lombok.Setter; |
| 5 | +import org.ecn.medev.controller.util.CallbackToDo; |
| 6 | +import org.ecn.medev.controller.util.UnsupportedActionException; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +@Getter |
| 12 | +@Setter |
| 13 | +public abstract class MenuController { |
| 14 | + |
| 15 | + @Getter |
| 16 | + private enum MenuOption implements OptionKey { |
| 17 | + SHUTDOWN("*"), |
| 18 | + GO_PARENT("*"); |
| 19 | + |
| 20 | + private final String inputKey; |
| 21 | + |
| 22 | + MenuOption(String s) { |
| 23 | + this.inputKey = s; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private final Map<MenuOption, InputOption> COMMON_OPTIONS = Collections.unmodifiableMap(new HashMap<MenuOption, InputOption>() {{ |
| 28 | + put(MenuOption.SHUTDOWN, new InputOption(MenuOption.SHUTDOWN, "Terminer", "Terminer le jeu.")); |
| 29 | + put(MenuOption.GO_PARENT, new InputOption(MenuOption.GO_PARENT, "Menu precedant", "Revenir au menu precedant")); |
| 30 | + }}); |
| 31 | + private final UiMenuType MENU_TYPE; |
| 32 | + |
| 33 | + private MenuController parentMenu; |
| 34 | + |
| 35 | + private Map<String, InputOption> options; |
| 36 | + |
| 37 | + private GameSession gameSession; |
| 38 | + |
| 39 | + public MenuController(UiMenuType MENU_TYPE, List<InputOption> options, GameSession gameSession, MenuController parentMenu) { |
| 40 | + this.MENU_TYPE = MENU_TYPE; |
| 41 | + if (parentMenu != null) { |
| 42 | + this.parentMenu = parentMenu; |
| 43 | + options.add(COMMON_OPTIONS.get(MenuOption.GO_PARENT)); |
| 44 | + } else { |
| 45 | + options.add(COMMON_OPTIONS.get(MenuOption.SHUTDOWN)); |
| 46 | + } |
| 47 | + this.options = options.stream().collect(Collectors.toMap(o -> o.getOptionKey().getInputKey(), o -> o)); |
| 48 | + this.gameSession = gameSession; |
| 49 | + } |
| 50 | + |
| 51 | + public void displayLongMenu() { |
| 52 | + System.out.println("Choisir une option de la menu:"); |
| 53 | + options.values().stream().forEach(o -> System.out.println(o.getOptionKey().getInputKey() + " - " + o.getLongDescription())); |
| 54 | + } |
| 55 | + |
| 56 | + public void displayShortMenu() { |
| 57 | + System.out.print("Options: "); |
| 58 | + options.values().stream().forEach(o -> System.out.print(" (" + o.getOptionKey().getInputKey() + ")" + o.getShortDescription())); |
| 59 | + System.out.print("\nYour choice: "); |
| 60 | + } |
| 61 | + |
| 62 | + public boolean conditionToStart() { |
| 63 | + return true; |
| 64 | + } |
| 65 | + public void start() { |
| 66 | + if (!conditionToStart()) { |
| 67 | + System.out.println("Condition to start menu are not met"); |
| 68 | + if (parentMenu != null) |
| 69 | + parentMenu.start(); |
| 70 | + return; |
| 71 | + } |
| 72 | + displayLongMenu(); |
| 73 | + try { |
| 74 | + doTheChoice(getTheChoice()); |
| 75 | + } catch (UnsupportedActionException e) { |
| 76 | + e.printStackTrace(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public InputOption getTheChoice() { |
| 81 | + String choice = "", st; |
| 82 | + System.out.println("---------------------------------"); |
| 83 | + int i; |
| 84 | + boolean isValidInput = false; |
| 85 | + Scanner in = new Scanner(System.in); |
| 86 | + do { |
| 87 | + choice = in.next(); |
| 88 | + if (choice.equalsIgnoreCase("help")) { |
| 89 | + displayLongMenu(); |
| 90 | + } |
| 91 | + if (choice.equalsIgnoreCase("*")) { |
| 92 | + if (parentMenu == null) { |
| 93 | + return new InputOption(MenuOption.SHUTDOWN); |
| 94 | + } else { |
| 95 | + return new InputOption(MenuOption.GO_PARENT); |
| 96 | + } |
| 97 | + } |
| 98 | + isValidInput = options.containsKey(choice); |
| 99 | + if (!isValidInput) { |
| 100 | + displayShortMenu(); |
| 101 | + } |
| 102 | + } while (!isValidInput); |
| 103 | + return options.get(choice); |
| 104 | + } |
| 105 | + |
| 106 | + public static Integer getIndexChoiceFromList(String header, List<String> options) { |
| 107 | + if (options == null || options.size() == 0) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + CallbackToDo callbackToDo = () -> { |
| 111 | + System.out.println(header); |
| 112 | + int i = 1; |
| 113 | + for (String option : options) { |
| 114 | + System.out.println(i + " - " + option); |
| 115 | + i++; |
| 116 | + } |
| 117 | + System.out.println("---------------------------------"); |
| 118 | + }; |
| 119 | + callbackToDo.call(); |
| 120 | + |
| 121 | + return getIndexChoice(options.size(), callbackToDo); |
| 122 | + } |
| 123 | + |
| 124 | + public static Integer getIndexChoice(int arraySize, CallbackToDo helpFunction) { |
| 125 | + return getIndexChoice(arraySize, "Your choice", helpFunction); |
| 126 | + } |
| 127 | + |
| 128 | + public static Integer getIndexChoice(int arraySize, String indexMeaning, CallbackToDo helpFunction) { |
| 129 | + if(arraySize <= 0){ |
| 130 | + return null; |
| 131 | + } |
| 132 | + Scanner in = new Scanner(System.in); |
| 133 | + boolean isValidChoice = false; |
| 134 | + String choice = "", st; |
| 135 | + Integer indexChoice = null, i = 1; |
| 136 | + do { |
| 137 | + System.out.print(indexMeaning + " [1-" + arraySize + "]: "); |
| 138 | + choice = in.next(); |
| 139 | + if (choice.equalsIgnoreCase("help") && helpFunction != null) { |
| 140 | + helpFunction.call(); |
| 141 | + } |
| 142 | + for (i = 1; i <= arraySize; i++) { |
| 143 | + st = "" + i; |
| 144 | + if (choice.equals(st)) { |
| 145 | + isValidChoice = true; |
| 146 | + indexChoice = i - 1; |
| 147 | + } |
| 148 | + } |
| 149 | + } while (!isValidChoice); |
| 150 | + return indexChoice; |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + public void doTheChoice(InputOption inputOption) throws UnsupportedActionException { |
| 155 | + if (inputOption.getOptionKey() instanceof MenuOption) { |
| 156 | + switch ((MenuOption) inputOption.getOptionKey()) { |
| 157 | + case SHUTDOWN: |
| 158 | + System.out.println("Shutting down..."); |
| 159 | + return; |
| 160 | + case GO_PARENT: |
| 161 | + System.out.println("Going back."); |
| 162 | + parentMenu.start(); |
| 163 | + return; |
| 164 | + } |
| 165 | + } |
| 166 | + doInnerChoice(inputOption); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Use {@link #doTheChoice(InputOption)} instead to get functionalities such as going back and exiting application. |
| 171 | + * <p> |
| 172 | + * This function have recursive behavior that do action based on |
| 173 | + * parameter provided. Then it call again itself by {@link #doInnerChoice(InputOption)} {@link #getTheChoice()} |
| 174 | + * unless the program end, or it starts another menu controller. |
| 175 | + * |
| 176 | + * @param inputOption |
| 177 | + */ |
| 178 | + protected abstract void doInnerChoice(InputOption inputOption) throws UnsupportedActionException; |
| 179 | +} |
0 commit comments