mnaufaldillah

CommandWords Tugas 6

Nov 24th, 2020
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. /**
  2.  * This class holds an enumeration of all command words
  3.  * known to the game.
  4.  * It is used to recognise commands as they are typed
  5.  * in.
  6.  *
  7.  * @author Muhammad Naufaldillah
  8.  * @version 24 November 2020
  9.  */
  10. public class CommandWords
  11. {
  12.     //a constant array that holds all valid command words
  13.     private static final String validCommands[] =
  14.     {
  15.         "go", "quit", "help", "look"
  16.     };
  17.    
  18.     /**
  19.      * Constructor - initialise the command words.
  20.      */
  21.     public CommandWords()
  22.     {
  23.        
  24.     }
  25.    
  26.     /**
  27.      * Check whether a given String is a valid command
  28.      * word.
  29.      * Return true if it is, false if it isn't.
  30.      */
  31.     public boolean isCommand(String aString)
  32.     {
  33.         for(int i = 0; i < validCommands.length; i++)
  34.         {
  35.             if(validCommands[i].equals(aString))
  36.             {
  37.                 return true;
  38.             }
  39.         }
  40.         // if we get here, the string was not found in the commands
  41.         return false;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment