trizehn

CommandWords

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