Class InventoryComponent

java.lang.Object
com.csse3200.game.components.Component
com.csse3200.game.components.player.InventoryComponent

public class InventoryComponent extends Component
A generic inventory component that is used to keep track of stored items for an entity
  • Constructor Details

    • InventoryComponent

      public InventoryComponent(int capacity)
      Creates inventory component
      Parameters:
      capacity - the players inventory size
  • Method Details

    • getCapacity

      public int getCapacity()
      Returns the capacity of this inventory component. I.e. the maximum number of items it can hold.
      Returns:
      - the set capacity of the inventory.
    • getSize

      public int getSize()
      Returns the current number of items in the inventory component.
      Returns:
      - the number of items in the inventory.
    • setCapacity

      public void setCapacity(int newCapacity)
      Sets the size of this item component to the newSize provided.
      Parameters:
      newCapacity - - the size to set this item component's capacity to. Must be a positive, non-zero integer.
      Throws:
      IllegalArgumentException - - if newSize is less than 1.
    • setSelected

      public void setSelected(int index)
      Sets the currently selected item to the index specified.
      Parameters:
      index - - the index to set the currently selected item to
      Throws:
      IllegalArgumentException - - if index is not within 0 and this.getSize()
    • getSelectedIndex

      public int getSelectedIndex()
      Returns the index of the currently selected item.
      Returns:
      - the index of the currently selected item.
    • getSelectedItem

      public ItemComponent getSelectedItem()
      Returns the currently selected item of the inventory
      Returns:
      - the item that is currently selected
    • getItems

      public List<ItemComponent> getItems()
      Returns an ArrayList containing the items stored in this InventoryComponent.
      Returns:
      - an ArrayList containing items currently stored in the inventory.
    • isFull

      public boolean isFull()
      Returns true if Inventory data structure is full.
      Returns:
      - true if the ArrayList is full, false otherwise.
    • isEmpty

      public boolean isEmpty()
      Returns true if the Inventory data structure is empty.
      Returns:
      - true if the ArrayList is empty, true otherwise.
    • getItemAt

      public ItemComponent getItemAt(int index)
      Returns the item at index if it exists in the current inventory.
      Parameters:
      index - - the index being queried.
      Returns:
      the item at index if it exists in the current inventory, null if there is nothing there.
      Throws:
      IllegalArgumentException - - if the given index is negative or out of bounds.
    • getItemFirst

      public ItemComponent getItemFirst()
      Returns the first element of the Inventory if it is not empty.
      Returns:
      - the item at the first index of the Inventory if it exists, null otherwise.
    • getItemLast

      public ItemComponent getItemLast()
      Returns the last element of the Inventory if it is not empty.
      Returns:
      - the item at the last index of the Inventory if it exists, null otherwise.
    • increaseCapacity

      public void increaseCapacity(int newCapacity)
      Increases the capacity of the Inventory if the given newSize is larger than the current size.
      Parameters:
      newCapacity - the new capacity of the Inventory.
    • find

      public boolean find(ItemComponent item)
      Checks if at least one instance of item is in the Inventory.
      Parameters:
      item - - the item to search the Inventory for.
      Returns:
      - true if there is at least one instance of item, false otherwise.
    • addItem

      public void addItem(ItemComponent item)
      Adds the item to the first empty index of the inventory.
      Parameters:
      item - - the item to be added to the Inventory.
    • removeItem

      public ItemComponent removeItem()
      Removes the item in last non-empty index of the inventory, specifically for meal station
    • addItemAt

      public void addItemAt(ItemComponent item, int index)
      Adds the item to the Inventory at the given index, if there is capacity.
      Parameters:
      item - - the item to be added to the Inventory.
      index - - the index where the item will be inserted.
      Throws:
      IllegalArgumentException - - if the given index is negative or out of bounds.
    • removeAt

      public ItemComponent removeAt(int index)
      Removes and returns the item from the Inventory at the given index, if it exists.
      Parameters:
      index - - the index where an item will be removed.
      Throws:
      IllegalArgumentException - - if the given index is negative or out of bounds.
    • getItemNames

      public List<String> getItemNames()
      Returns the names of all items present in the list, in order.
      Returns:
      - the list of names of items in the inventory, null if empty.
    • findName

      public boolean findName(String itemName)
      Returns true if the itemName is in the inventory, false otherwise.
      Parameters:
      itemName - - the item being checked for in this inventory.
      Returns:
      - true if item is in the inventory, false otherwise.
    • removeItemName

      public ItemComponent removeItemName(String itemName)
      Removes the first instance of the item with itemName from the Inventory.
      Parameters:
      itemName - - the name of the item to be removed.
      Returns:
      - the removed item i, null if not present.