-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropCommand.java
41 lines (36 loc) · 1.26 KB
/
DropCommand.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
package bork;
/**
* A subclass of Command that executes commands from the user to drop a certain
* item in their inventory.
* @author Dr. Zeitz
*/
class DropCommand extends Command {
private String itemName;
/**
* Constructor for DropCommand objects that take in an item name as a parameter.
* @param itemName the name of the item to be dropped from inventory
*/
DropCommand(String itemName) {
this.itemName = itemName;
}
/**
* Executes a command to drop an item from the user's inventory, updates the
* GameState, and returns a String to the user.
* @return a String that either confirms the action or explains why it was
* not possible
*/
public String execute() {
if (itemName == null || itemName.trim().length() == 0) {
return "Drop what?\n";
}
try {
Item theItem = GameState.instance().getItemFromInventoryNamed(
itemName);
GameState.instance().removeFromInventory(theItem);
GameState.instance().getAdventurersCurrentRoom().add(theItem);
return itemName + " dropped.\n";
} catch (Item.NoItemException e) {
return "You don't have a " + itemName + ".\n";
}
}
}