Files
School/Anul 2/Semestrul 1/Metode avansate de programare/Interpretor/Views/TextMenu.java
T
2024-08-31 12:07:21 +03:00

39 lines
1.1 KiB
Java

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();
}
}
}