-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.java
More file actions
99 lines (85 loc) · 2.04 KB
/
Inventory.java
File metadata and controls
99 lines (85 loc) · 2.04 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.ArrayList;
import java.util.Iterator;
/**
* A class to hold a characters inventory.
*
* @author Thomas Vakili
* @version 2013.12.14
*/
public class Inventory implements Iterable<Item> {
private ArrayList<Item> items;
private int size;
public Inventory() {
items = new ArrayList<Item>();
size = 10; // This should be passed as a parameter??
}
/**
* Method to make it possible to iterate over
* the inventory.
*
* @return iterator for the list of items
*/
public Iterator<Item> iterator() {
return items.iterator();
}
/**
* List the inventory.
*
* @return a string listing all the items
*/
@Override
public String toString() {
String string = "";
if (items.size() > 0) {
for (Item item: items) {
string += item.getName() + " ";
}
// Skip the last space
string = string.substring(0, string.length()-1);
}
return string;
}
/**
* Adds an item to the inventory.
*
* @param item The Item to add
* @return true if there is room, false if the inventory is full
*/
public boolean addItem(Item item) {
if (items.size() < size) {
items.add(item);
return true;
} else {
return false;
}
}
/**
* Remove an item from the inventory.
*
* @param item the item to remove
*/
public void removeItem(Item item) {
items.remove(items.indexOf(item));
}
/**
* Gets the number of items in the inventory
*
* @return The number of items
*/
public int numberOfItems() {
return items.size();
}
public void setSize(int newSize) {
size = newSize;
}
public ArrayList<Item> getItems() {
return items;
}
public int getWeight() {
int total = 0;
for (Item item: items) {
total += item.getWeight();
}
return total;
}
}