School Commit Init
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package Views;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
import Views.Commands.Command;
|
||||
|
||||
public class TextMenu {
|
||||
private Map<String, Command> commands;
|
||||
public TextMenu() {
|
||||
this.commands = new HashMap<String, Command>();
|
||||
}
|
||||
public void addCommand(Command command) {
|
||||
this.commands.put(command.getKey(), command);
|
||||
}
|
||||
private void printMenu() {
|
||||
System.out.println("Menu:");
|
||||
for (Command command : this.commands.values()) {
|
||||
String line = String.format("%4s: %s", command.getKey(), command.getDescription());
|
||||
System.out.println(line);
|
||||
}
|
||||
}
|
||||
public void show() {
|
||||
var scanner = new Scanner(System.in);
|
||||
while (true) {
|
||||
this.printMenu();
|
||||
System.out.println("Input the option: ");
|
||||
String key = scanner.nextLine();
|
||||
Command command = this.commands.get(key);
|
||||
if (command == null) {
|
||||
System.out.println("Invalid option");
|
||||
continue;
|
||||
}
|
||||
command.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user