Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. package util;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6. import java.util.Map.Entry;
  7.  
  8. public class ConsoleUserInterface {
  9.  
  10.     // The map of the commands
  11.     private final Map<String, CuiCommand> commandMap;
  12.    
  13.     private boolean exit;
  14.  
  15.     // The name of the cui
  16.     private final String name;
  17.  
  18.     /**
  19.      * @param name The name of the cui
  20.      */
  21.     public ConsoleUserInterface(String name) {
  22.         this.name = name;
  23.         this.commandMap = new HashMap<String, CuiCommand>();
  24.        
  25.         addCommand("help", new HelpCommand());
  26.         addCommand("exit", new ExitCommand());
  27.     }
  28.  
  29.     /**
  30.      * Exits the main loop.
  31.      */
  32.     public void exit() {
  33.         this.exit = true;
  34.     }
  35.  
  36.     /**
  37.      * Adds a command to this cui
  38.      *
  39.      * @param key
  40.      *            The key of the command, i.e. "help" for a HelpCommand.
  41.      * @param cuiCommand
  42.      *            That's the actual CuiCommand.
  43.      */
  44.     public void addCommand(String key, CuiCommand cuiCommand) {
  45.         commandMap.put(key.toLowerCase(), cuiCommand); // Puts the new command in the map
  46.     }
  47.  
  48.     public void start() {
  49.         String input;
  50.  
  51.         // These are the the different messages.
  52.         String error = "There is no such command like \"[INPUT]\"";
  53.         String helpMessage = "Type in \"help\" for help :P";
  54.  
  55.         System.out.println("-----------" + this.name + "-----------");
  56.         System.out.println(helpMessage);
  57.  
  58.         // Now the programm enters the main loop.
  59.         // You have to invoke exit() in order to exit this loop again.
  60.         while (!exit) {
  61.             input = Input.readString("Next action: ").toLowerCase(); // Reads the input
  62.             System.out.println();
  63.             // checks if there is a key of a command, which the user wants
  64.             if (this.commandMap.containsKey(input)) {
  65.                 // if true, then execute the command!
  66.                 this.commandMap.get(input).execute();
  67.             } else {
  68.                 // Print error message
  69.                 System.out.println(error.replaceAll("\\[INPUT\\]", input));
  70.                 System.out.println(helpMessage);
  71.             }
  72.         }
  73.         System.exit(0);
  74.     }
  75.    
  76.     private class ExitCommand implements CuiCommand{
  77.         @Override
  78.         public void execute() {
  79.             exit();
  80.             System.out.println("Exit the programm.");
  81.         }
  82.  
  83.         @Override
  84.         public String getDescription() {
  85.             return "Exits the program.";
  86.         }
  87.     }
  88.    
  89.     private class HelpCommand implements CuiCommand{
  90.         @Override
  91.         public void execute() {
  92.             // We use an iterator here.
  93.             Iterator<Entry<String, CuiCommand>> commands = commandMap.entrySet().iterator();
  94.            
  95.             System.out.println("----Help----");
  96.             while (commands.hasNext()) {
  97.                 // For each command entry print the key and the description!
  98.                 Map.Entry<String, CuiCommand> entry = commands.next();
  99.                
  100.                 // Finally prints the information
  101.                 System.out.println(entry.getKey() + ": "
  102.                         + entry.getValue().getDescription());
  103.             }
  104.             System.out.println();
  105.         }
  106.  
  107.         @Override
  108.         public String getDescription() {
  109.             return "Shows all commands.";
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement