package storage; import items.*; import java.util.*; /** * This class represents a pokemon trainer's inventory */ public class Inventory { public static final Scanner in = new Scanner(System.in); private final Map items; /** * Initializes a new empty inventory */ public Inventory() { this.items = new HashMap<>(); } /** * Adds a new item to the inventory * *

If the inventory already contains the item, the amount will be added to the existing amount.

* * @param item

The item to be added to the inventory

* @param amount

The amount of the item to add

*/ public void addItem(Item item, Integer amount) { if (items.get(item) != null) { amount += items.get(item); } items.put(item, amount); } /** * Gets all available poke balls from the inventory * @return

A list of poke balls

*/ public List getPokeBalls() { List pokeBalls = new ArrayList<>(); for (Item item : items.keySet()) { if (AbstractPokeBall.class.isAssignableFrom(item.getClass()) && items.get(item) > 0) { pokeBalls.add((AbstractPokeBall) item); } } return pokeBalls; } /** * Gets all available potions from the inventory * @return

A list of potions

*/ public List getPotions() { List potions = new ArrayList<>(); for (Item item : items.keySet()) { if (AbstractPotion.class.isAssignableFrom(item.getClass()) && items.get(item) > 0) { potions.add((AbstractPotion) item); } } return potions; } /** * Prints out information about available poke balls */ public void availablePokeBalls() { System.out.println("You may choose from these items:"); List pokeBalls = getPokeBalls(); for (int i = 0; i < pokeBalls.size(); i++) { System.out.printf("%d: (%d) %s%n", i + 1, items.get(pokeBalls.get(i)), pokeBalls.get(i)); } System.out.print(">"); } /** * Prints out information about available potions */ public void availablePotions() { System.out.println("You may choose from these items:"); List potionList = getPotions(); for (int i = 0; i < potionList.size(); i++) { System.out.printf("%d: (%d) %s%n", i + 1, items.get(potionList.get(i)), potionList.get(i)); } System.out.print(">"); } /** * Gets a potion according to the user's input * @return

The potion the user chose, or null on invalid choice

*/ public AbstractPotion getChosenPotion() { List potions = getPotions(); if (in.hasNextInt()) { int choice = in.nextInt() - 1; if (choice >= 0 && choice < potions.size()) { in.nextLine(); return potions.get(choice); } } else { in.nextLine(); } return null; } /** * Gets a poke ball according to the user's input * @return

The poke ball the user chose, or null on invalid choice

*/ public AbstractPokeBall getChosenPokeBall() { List pokeBalls = getPokeBalls(); if (in.hasNextInt()) { int choice = in.nextInt() - 1; if (choice >= 0 && choice < pokeBalls.size()) { in.nextLine(); return pokeBalls.get(choice); } } else { in.nextLine(); } return null; } /** * Gets a save-able string containing all non-empty items in the inventory * @return

A save string Name;Amount

*/ public String getSaveString() { StringBuilder saveString = new StringBuilder(); for (Item item : items.keySet()) { saveString.append(item.saveString()).append(";").append(items.get(item)); saveString.append("\n"); } return saveString.toString(); } }