Refactoring of Game Adds and improves all comments Restructures files Improves items Makes project into a maven project
137 lines
4.1 KiB
Java
137 lines
4.1 KiB
Java
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<Item, Integer> items;
|
|
|
|
/**
|
|
* Initializes a new empty inventory
|
|
*/
|
|
public Inventory() {
|
|
this.items = new HashMap<>();
|
|
}
|
|
|
|
/**
|
|
* Adds a new item to the inventory
|
|
*
|
|
* <p>If the inventory already contains the item, the amount will be added to the existing amount.</p>
|
|
*
|
|
* @param item <p>The item to be added to the inventory</p>
|
|
* @param amount <p>The amount of the item to add</p>
|
|
*/
|
|
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 <p>A list of poke balls</p>
|
|
*/
|
|
public List<AbstractPokeBall> getPokeBalls() {
|
|
List<AbstractPokeBall> 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 <p>A list of potions</p>
|
|
*/
|
|
public List<AbstractPotion> getPotions() {
|
|
List<AbstractPotion> 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<AbstractPokeBall> 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<AbstractPotion> 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 <p>The potion the user chose, or null on invalid choice</p>
|
|
*/
|
|
public AbstractPotion getChosenPotion() {
|
|
List<AbstractPotion> 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 <p>The poke ball the user chose, or null on invalid choice</p>
|
|
*/
|
|
public AbstractPokeBall getChosenPokeBall() {
|
|
List<AbstractPokeBall> 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 <p>A save string Name;Amount</p>
|
|
*/
|
|
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();
|
|
}
|
|
}
|