package uulm.teamname.marvelous.gamelibrary.entities; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; /** Represents an inventory of 6 slots of {@link StoneType}s that can be manipulated. */ public class Inventory implements Iterable { /** The size of the inventory. */ private final int size = 6; /** The content of the inventory. */ private final HashSet content = new HashSet<>(size); /** Constructs a new {@link Inventory}. * @param content The starting content of the inventory. */ public Inventory(ArrayList content) { if(content.size() > size) { throw new IllegalArgumentException("Attempted to construct an inventory with more than "+size+" initial stones."); } for(StoneType stone : content) { if(content.contains(stone)) { throw new IllegalArgumentException("Attempted to construct an inventory with duplicate entries."); } content.add(stone); } } /** Constructs a new empty {@link Inventory}. */ public Inventory() { } /** Returns the number of free slots the inventory has. */ public int getFreeSlots() { return size - content.size(); } /** Checks if the inventory contains the given stone. * @param stone The {@link StoneType} to check for. */ public boolean hasStone(StoneType stone) { return content.contains(stone); } /** Adds a stone to the inventory. * @param stone The {@link StoneType} to add. */ public void addStone(StoneType stone) { if(content.contains(stone)) { throw new IllegalArgumentException("Attempted to add a duplicate stone to an inventory."); } if(content.size() == size) { throw new IllegalArgumentException("Attempted to add a stone to a full inventory."); } content.add(stone); } /** Removes a stone from the inventory. * @param stone The {@link StoneType} to remove. */ public void removeStone(StoneType stone) { if(!content.contains(stone)) { throw new IllegalArgumentException("Attempted to remove a nonexistent stone from an inventory."); } content.remove(stone); } /** Iterates over the inventory. */ @Override public Iterator iterator() { return content.iterator(); } }