Package com.csse3200.game.inventory
Class Inventory
java.lang.Object
com.csse3200.game.inventory.Inventory
- All Implemented Interfaces:
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 -
Method Summary
Modifier and TypeMethodDescriptionint
add
(AbstractItem item) 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
isFull()
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.
-
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 interfaceInventoryInterface
- 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 interfaceInventoryInterface
- 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 interfaceInventoryInterface
- 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 interfaceInventoryInterface
- Parameters:
code
- the unique code of the item.- Returns:
true
if the item is present,false
otherwise.
-
hasItem
Checks if an item with the given name exists in the inventory.- Specified by:
hasItem
in interfaceInventoryInterface
- 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 interfaceInventoryInterface
- Parameters:
code
- the unique code of the item.- Returns:
- the index of the item, or -1 if the item is not found.
-
getIndex
Retrieves the index of the first occurrence of an item with the given name.- Specified by:
getIndex
in interfaceInventoryInterface
- Parameters:
name
- the name of the item.- Returns:
- the index of the item, or -1 if the item is not found.
-
getAt
Retrieves the item at the specified index in the inventory.- Specified by:
getAt
in interfaceInventoryInterface
- 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 interfaceInventoryInterface
- Parameters:
code
- the unique code of the item.
-
deleteItemAt
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 interfaceInventoryInterface
- 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 interfaceInventoryInterface
-
useItem
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 interfaceInventoryInterface
- Parameters:
code
- the unique code of the item.context
- the context in which the item is used (seeItemUsageContext
).
-
useItemAt
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 interfaceInventoryInterface
- Parameters:
index
- the index of the item to use.context
- the context in which the item is used (seeItemUsageContext
).
-
add
Adds an item to the inventory, either by stacking it with existing items or placing it in a new slot.- Specified by:
add
in interfaceInventoryInterface
- Parameters:
item
- the item to add to the inventory.- Returns:
- the index the item was added at (-1 if not added)
-
addAt
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 interfaceInventoryInterface
- 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 interfaceInventoryInterface
-