Class Inventory

java.lang.Object
com.csse3200.game.inventory.Inventory
All Implemented Interfaces:
InventoryInterface

public class Inventory extends Object implements InventoryInterface
The Inventory class manages a collection of items, allowing for storage, retrieval, and manipulation. This class supports adding and removing items, checking inventory status, and using items within the inventory. It is intended as a player inventory - to store items players retrieve from the game.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Inventory(int capacity)
    Constructs an Inventory with a specified capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Adds an item to the inventory, either by stacking it with existing items or placing it in a new slot.
    void
    addAt(int index, AbstractItem item)
    Adds an item at the specified index in the inventory, replacing any existing item at that index.
    void
    Clears all items from the inventory.
    void
    deleteItem(int code)
    Deletes the first occurrence of an item with the specified code from the inventory.
    deleteItemAt(int index)
    Deletes the item at the specified index from the inventory.
    getAt(int index)
    Retrieves the item at the specified index in the inventory.
    int
    Retrieves the total capacity of the inventory.
    int
    getIndex(int code)
    Retrieves the index of the first occurrence of an item with the given code.
    int
    Retrieves the index of the first occurrence of an item with the given name.
    boolean
    hasItem(int code)
    Checks if an item with the given code exists in the inventory.
    boolean
    Checks if an item with the given name exists in the inventory.
    boolean
    Checks if the inventory is full.
    void
    Loads and initialises the contents of the inventory from a save.
    int
    Returns the number of free slots available in the inventory.
    void
    Sorts the inventory by item code (in ascending order)
    void
    swap(int src, int target)
    Swaps the items between two inventory slots.
    void
    useItem(int code, ItemUsageContext context)
    Uses the first occurrence of an item with the specified code in the inventory.
    void
    useItemAt(int index, ItemUsageContext context)
    Uses the item at the specified index in the inventory.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Inventory

      public Inventory(int capacity)
      Constructs an Inventory with a specified capacity.
      Parameters:
      capacity - the maximum number of items the inventory can hold.
      Throws:
      IllegalArgumentException - if the capacity is not a positive integer.
  • Method Details

    • loadInventoryFromSave

      public void loadInventoryFromSave()
      Loads and initialises the contents of the inventory from a save. If one does not exist, creates one.
      See Also:
    • getCapacity

      public int getCapacity()
      Description copied from interface: InventoryInterface
      Retrieves the total capacity of the inventory.
      Specified by:
      getCapacity in interface InventoryInterface
      Returns:
      the total capacity of the inventory.
    • numFreeSlots

      public int numFreeSlots()
      Description copied from interface: InventoryInterface
      Returns the number of free slots available in the inventory.
      Specified by:
      numFreeSlots in interface InventoryInterface
      Returns:
      the number of free slots available in the inventory.
    • isFull

      public boolean isFull()
      Description copied from interface: InventoryInterface
      Checks if the inventory is full.
      Specified by:
      isFull in interface InventoryInterface
      Returns:
      true if the inventory is full, false otherwise.
    • hasItem

      public boolean hasItem(int code)
      Checks if an item with the given code exists in the inventory.
      Specified by:
      hasItem in interface InventoryInterface
      Parameters:
      code - the unique code of the item.
      Returns:
      true if the item is present, false otherwise.
    • hasItem

      public boolean hasItem(String name)
      Checks if an item with the given name exists in the inventory.
      Specified by:
      hasItem in interface InventoryInterface
      Parameters:
      name - the name of the item.
      Returns:
      true if the item is present, false otherwise.
    • getIndex

      public int getIndex(int code)
      Retrieves the index of the first occurrence of an item with the given code.
      Specified by:
      getIndex in interface InventoryInterface
      Parameters:
      code - the unique code of the item.
      Returns:
      the index of the item, or -1 if the item is not found.
    • getIndex

      public int getIndex(String name)
      Retrieves the index of the first occurrence of an item with the given name.
      Specified by:
      getIndex in interface InventoryInterface
      Parameters:
      name - the name of the item.
      Returns:
      the index of the item, or -1 if the item is not found.
    • getAt

      public AbstractItem getAt(int index)
      Retrieves the item at the specified index in the inventory.
      Specified by:
      getAt in interface InventoryInterface
      Parameters:
      index - the index of the item in the inventory
      Returns:
      the item at the specified index, or null if there is no item at that index
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out of range, i.e, (index < 0 || index >= capacity)
    • swap

      public void swap(int src, int target)
      Swaps the items between two inventory slots. If the target slot is empty, the item from the source slot is moved to the target slot. If the target slot contains an item, the items between the source and target slots are swapped.
      Parameters:
      src - The index of the source slot from which the item is being moved.
      target - The index of the target slot to which the item is being moved.
    • deleteItem

      public void deleteItem(int code)
      Deletes the first occurrence of an item with the specified code from the inventory.

      Currently removes the item entirely. Needs to be updated to reduce quantity if applicable - ie if we only want to delete n of the item.

      Specified by:
      deleteItem in interface InventoryInterface
      Parameters:
      code - the unique code of the item.
    • deleteItemAt

      public AbstractItem deleteItemAt(int index)
      Deletes the item at the specified index from the inventory.

      It is currently up to the user to ensure they have not exceeded the bounds of the inventory.

      If the item is removed, updates the internal mappings and adjusts the next available index.

      Specified by:
      deleteItemAt in interface InventoryInterface
      Parameters:
      index - the index of the item to delete.
      Returns:
      the item that was removed (or null if there was no item at that index)
    • clearInventory

      public void clearInventory()
      Clears all items from the inventory.

      Warning: All items will be deleted and cannot be recovered.

      Specified by:
      clearInventory in interface InventoryInterface
    • useItem

      public void useItem(int code, ItemUsageContext context)
      Uses the first occurrence of an item with the specified code in the inventory.

      If the item is consumable and becomes empty after use, it is removed from the inventory.

      Specified by:
      useItem in interface InventoryInterface
      Parameters:
      code - the unique code of the item.
      context - the context in which the item is used (see ItemUsageContext).
    • useItemAt

      public void useItemAt(int index, ItemUsageContext context)
      Uses the item at the specified index in the inventory.

      If the item is consumable and becomes empty after use, it is removed from the inventory.

      Specified by:
      useItemAt in interface InventoryInterface
      Parameters:
      index - the index of the item to use.
      context - the context in which the item is used (see ItemUsageContext).
    • add

      public int add(AbstractItem item)
      Adds an item to the inventory, either by stacking it with existing items or placing it in a new slot.
      Specified by:
      add in interface InventoryInterface
      Parameters:
      item - the item to add to the inventory.
      Returns:
      the index the item was added at (-1 if not added)
    • addAt

      public void addAt(int index, AbstractItem item)
      Adds an item at the specified index in the inventory, replacing any existing item at that index.

      Note - it is currently up to the user to determine the given index is valid!

      Specified by:
      addAt in interface InventoryInterface
      Parameters:
      index - the index at which to add the item.
      item - the item to add to the inventory.
    • sortByCode

      public void sortByCode()
      Sorts the inventory by item code (in ascending order)
      Specified by:
      sortByCode in interface InventoryInterface