blob: 605b64016469ee0a9c6e01f23665a6896a5e6afd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package design.controller.userinput;
import java.util.*;
public abstract class Menu {
public abstract String getTitle();
public abstract List<MenuOption> getMenuOptions();
public void present() {
var menuOptions = getMenuOptions();
System.out.printf("-- %s --\n", getTitle().toUpperCase());
for (int i = 0; i < menuOptions.size(); i++) {
MenuOption menuOption = menuOptions.get(i);
System.out.printf("%s: %s\n", i, menuOption.getName());
}
Scanner sc = new Scanner(System.in);
var line = sc.nextLine();
var split = line.split(" ");
try {
int i = Integer.parseInt(split[0]);
menuOptions.get(i).onCommand(Arrays.copyOfRange(split, 1, split.length));
} catch (ArrayIndexOutOfBoundsException ex) {
System.err.printf("Invalid option \"%s\"\n", line);
present();
}
}
}
|