-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathItemContainer.java
executable file
·77 lines (65 loc) · 1.66 KB
/
ItemContainer.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package textgame;
import java.util.*;
public abstract class ItemContainer {
private List<Item> itemList;
private int id;
public ItemContainer(int i){
id = i;
itemList = new ArrayList<Item>();
}
public ItemContainer(int i, List<Item> l) {
id = i;
itemList = l;
}
//add a newly created item in the container
public void addItem(Item it){
itemList.add(it);
}
//TODO: Make this less of an ugly hack
public static String describeItems(List<Item> items) {
int itCount = items.size();
switch (itCount) {
case 0:
return "nothing";
case 1:
return items.get(0).getName();
default:
int i;
String desc = "";
for (i = 0; i < itCount - 1; ++i) {
desc += items.get(i).getName() + ", ";
}
return desc + "and " + items.get(i).getName();
}
}
public String describeItems() {
return describeItems(itemList);
}
//removes the item from the container
public void removeItem(Item it){
itemList.remove(it);
}
public boolean containsItem(Item it){
return itemList.contains(it);
}
public Item itemOfType(String type) {
for (Item it : itemList) {
if (it.getType().equals(type)) {
return it;
}
}
return null;
}
public Item itemWithName(String name) {
name = name.toLowerCase();
for (Item it : itemList) {
if (it.getName().toLowerCase().equals(name)) {
return it;
}
}
return null;
}
public int getId(){
return id;
}
}