summaryrefslogtreecommitdiff
path: root/src/com/delwink/icebox/Inventory.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/delwink/icebox/Inventory.java')
-rw-r--r--src/com/delwink/icebox/Inventory.java38
1 files changed, 26 insertions, 12 deletions
diff --git a/src/com/delwink/icebox/Inventory.java b/src/com/delwink/icebox/Inventory.java
index f4472c9..bd6a574 100644
--- a/src/com/delwink/icebox/Inventory.java
+++ b/src/com/delwink/icebox/Inventory.java
@@ -22,10 +22,12 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Date;
import java.util.List;
+import java.util.Map;
import java.util.Set;
-import java.util.TreeSet;
+import java.util.TreeMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
@@ -41,7 +43,7 @@ import org.xml.sax.SAXException;
public class Inventory {
protected final List<Order> ORDERS;
protected final List<QuantityUpdate> UPDATES;
- protected final Set<InventoryItem> ITEMS;
+ protected final Map<Integer, InventoryItem> ITEMS;
/**
* Creates a new empty inventory.
@@ -49,7 +51,7 @@ public class Inventory {
public Inventory() {
ORDERS = new ArrayList<>();
UPDATES = new ArrayList<>();
- ITEMS = new TreeSet<>();
+ ITEMS = new TreeMap<>();
}
/**
@@ -138,8 +140,8 @@ public class Inventory {
writer.println("<!-- Generated by IceBox. DO NOT EDIT! -->");
writer.println("<inventory>");
- for (InventoryItem item : ITEMS)
- writer.println(" " + item);
+ for (Integer id : ITEMS.keySet())
+ writer.println(" " + ITEMS.get(id));
writer.println();
@@ -196,18 +198,30 @@ public class Inventory {
}
public final void addNewItem(InventoryItem item) {
- ITEMS.add(item);
+ ITEMS.put(item.getID(), item);
}
- public Set<InventoryItem> getItems() {
- return ITEMS;
+ public Collection<InventoryItem> getItems() {
+ return ITEMS.values();
}
public InventoryItem getItemByID(int id) {
- for (InventoryItem item : ITEMS)
- if (item.getID() == id)
- return item;
+ return ITEMS.get(id);
+ }
+
+ public int getNextID() {
+ Set<Integer> keys = ITEMS.keySet();
+ for (int i = 0; i <= keys.size(); ++i)
+ if (!ITEMS.containsKey(i))
+ return i;
+
+ if (ITEMS.isEmpty())
+ return 0;
+
+// for (Integer id : ITEMS.keySet())
+// if (!ITEMS.containsKey(id + 1))
+// return id + 1;
- return null;
+ throw new IllegalStateException("Could not find next item ID");
}
}