document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *class ini akan membaca yang diinput user dan menerjemahkannya sebagai
  3.  * "Adventure" command.
  4.  *
  5.  *
  6.  * @author (Aristya Vika)
  7.  * @version (17.11)
  8.  */
  9.  
  10. import java.util.Scanner;
  11.  
  12. public class Parser
  13. {
  14.    private CommandWords commands;  // holds all valid command word
  15.    private Scanner reader;         // source of command input
  16.  
  17.     /**
  18.      * buat parser untuk baca terminal
  19.      */
  20.  
  21.     public Parser()
  22.     {
  23.         commands = new CommandWords();
  24.         reader = new Scanner(System.in);
  25.     }
  26.  
  27.     /**
  28.      * @return command selanjutnya dari user
  29.      */
  30.  
  31.     public Command getCommand()
  32.     {
  33.  
  34.         String inputLine;   // will hold the full input line
  35.         String word1 = null;
  36.         String word2 = null;
  37.        
  38.         System.out.print("> ");     // print prompt
  39.  
  40.         inputLine = reader.nextLine();
  41.  
  42.         // Find up to two words on the line
  43.         Scanner tokenizer = new Scanner(inputLine);
  44.  
  45.         if(tokenizer.hasNext()) {
  46.             word1 = tokenizer.next();      // get first word
  47.             if(tokenizer.hasNext()) {
  48.                 word2 = tokenizer.next();      // get second word
  49.                 // note: we just ignore the rest of the input line.
  50.             }
  51.         }
  52.  
  53.         // Now check whether this word is known. If so, create a command
  54.         // with it. If not, create a "null" command (for unknown command).
  55.  
  56.         if(commands.isCommand(word1))
  57.         {
  58.             return new Command(word1, word2);
  59.         }
  60.  
  61.         else
  62.         {
  63.             return new Command(null, word2);
  64.         }
  65.     }
  66. }
');