From d387d706bb0fd19ef5f80bab4555e68be7ff8683 Mon Sep 17 00:00:00 2001 From: Tony Grosinger Date: Thu, 15 Sep 2011 15:51:45 -0700 Subject: [PATCH] -Implemented Inventory -Created FullInventoryException Signed-off-by: Tony Grosinger --- .../exceptions/FullInventoryException.class | Bin 0 -> 423 bytes src/net/grosinger/nomads/Inventory.java | 92 ++++++++++++++++++ .../exceptions/FullInventoryException.java | 8 ++ 3 files changed, 100 insertions(+) create mode 100644 bin/net/grosinger/nomads/exceptions/FullInventoryException.class create mode 100644 src/net/grosinger/nomads/Inventory.java create mode 100644 src/net/grosinger/nomads/exceptions/FullInventoryException.java diff --git a/bin/net/grosinger/nomads/exceptions/FullInventoryException.class b/bin/net/grosinger/nomads/exceptions/FullInventoryException.class new file mode 100644 index 0000000000000000000000000000000000000000..edea67b1ef61f91bbfb2b88ca8cc38888c46305e GIT binary patch literal 423 zcmbVI!AiqG5Ph4*rp9PB#gho}Ry`OHDniAJSP+7jmfp9?nyzejWwR;vvpfkN`~W{n zoS@Z`RIuT3F3*km}I%ta#CQi-e*uB_n2 zD!Hc}AzIjquz`qSzn}d|kDVoN!$zx{NiFW`#Z1@ z|GRC-5ROf4b1{<5j)T8naN1bJ5$I6*Cnz#<-zE%*4d77*1SfIl1>rNL9<~WXilDJJ TjqOkteT|`uU1AJ-gdXBgr{r$a literal 0 HcmV?d00001 diff --git a/src/net/grosinger/nomads/Inventory.java b/src/net/grosinger/nomads/Inventory.java new file mode 100644 index 0000000..4608521 --- /dev/null +++ b/src/net/grosinger/nomads/Inventory.java @@ -0,0 +1,92 @@ +package net.grosinger.nomads; + +import java.util.ArrayList; + +import net.grosinger.nomads.exceptions.FullInventoryException; + +public class Inventory { + private DroneListItem droneItem; + private ArrayList allItems; + private int maxSize; + + public Inventory(DroneListItem drone) { + droneItem = drone; + allItems = new ArrayList(); + updateMaxSize(); + } + + /** + * Remove a specific GameObject from the inventory + * + * @param obj + * Object to be removed + */ + public void remove(GameObject obj) { + allItems.remove(obj); + } + + /** + * Remove the GameObject at the specified index from the inventory + * + * @param index + * Index to be removed + */ + public void remove(int index) { + allItems.remove(index); + } + + /** + * Get the GameObject that is at the specified index and return + * + * @param index + * Index to be found + * @return GameObject + */ + public GameObject getItem(int index) { + return allItems.get(index); + } + + /** + * Add an item to the inventory + * + * @param item + * Item to be added + * @throws FullInventoryException + */ + public void addItem(GameObject item) throws FullInventoryException { + updateMaxSize(); + + if (allItems.size() >= maxSize) { + throw new FullInventoryException(); + } else { + allItems.add(item); + } + } + + /** + * Determine if the inventory is completely empty of all GameObjects + * + * @return boolean + */ + public boolean isEmpty() { + return allItems.isEmpty(); + } + + /** + * Determine if the inventory is full or not and return + * + * @return boolean + */ + public boolean isFull() { + updateMaxSize(); + return allItems.size() >= maxSize; + } + + public int size() { + return allItems.size(); + } + + private void updateMaxSize() { + maxSize = droneItem.getCargoSpace(); + } +} diff --git a/src/net/grosinger/nomads/exceptions/FullInventoryException.java b/src/net/grosinger/nomads/exceptions/FullInventoryException.java new file mode 100644 index 0000000..7d01110 --- /dev/null +++ b/src/net/grosinger/nomads/exceptions/FullInventoryException.java @@ -0,0 +1,8 @@ +package net.grosinger.nomads.exceptions; + +public class FullInventoryException extends Exception { + + public FullInventoryException() { + super("The inventory is already full"); + } +}