-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandWords.java
58 lines (52 loc) · 1.63 KB
/
CommandWords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.HashMap;
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This class holds an enumeration of all command words known to the game.
* It is used to recognise commands as they are typed in.
*
* @author Fredrik Ljungdahl, Michael Kölling and David J. Barnes
* @version 2013.12.19
*/
public class CommandWords {
// map between command name and the associated enum
private HashMap<String, CommandWord> validCommands;
/**
* Initializes the commands.
*/
public CommandWords() {
validCommands = new HashMap<String, CommandWord>();
for (CommandWord command : CommandWord.values()) {
if (command != CommandWord.UNKNOWN) {
validCommands.put(command.toString(), command);
}
}
}
/**
* Finds the CommandWord associated with a command word.
*/
public CommandWord getCommandWord(String commandWord) {
CommandWord command = validCommands.get(commandWord);
if (command == null) {
return CommandWord.UNKNOWN;
}
return command;
}
/**
* Check whether a given String is a valid command word.
* @return true if it is, false if it isn't.
*/
public boolean isCommand(String aString) {
return validCommands.containsKey(aString);
}
/**
* Print all valid commands to System.out.
*/
public void showAll() {
for (String command : validCommands.keySet()) {
System.out.print(command + " ");
}
System.out.println();
}
}