This commit is contained in:
Arne Keller 2020-03-19 14:31:57 +01:00
parent 19712a7039
commit 58a2a23220
3 changed files with 48 additions and 4 deletions

View File

@ -3,6 +3,12 @@ package edu.kit.informatik.cardgame.model;
import java.util.*;
import java.util.stream.Stream;
/**
* Player inventory. Contains {@link CardCategory#RESOURCE resources} and {@link Item items}.
*
* @author Arne Keller
* @version 1.0
*/
public class Inventory {
/**
* Resources collected by the player. Obviously only contains {@link CardCategory#RESOURCE resource} cards.
@ -13,7 +19,16 @@ public class Inventory {
*/
private final List<Item> items = new ArrayList<>();
public void addResource(Card resourceCard) {
/**
* Add a resource card to this inventory.
*
* @param resourceCard resource card
* @throws IllegalArgumentException if the provided card is not a resource card
*/
public void addResource(Card resourceCard) throws IllegalArgumentException {
if (resourceCard.category() != CardCategory.RESOURCE) {
throw new IllegalArgumentException("card is not a resource");
}
resources.add(resourceCard);
}
@ -37,7 +52,16 @@ public class Inventory {
}
}
public void build(Item item) {
/**
* Build the specified item.
*
* @param item item to build
* @throws IllegalArgumentException if there are not enough resources to build the item
*/
public void build(Item item) throws IllegalArgumentException {
if (!canBuild(item)) {
throw new IllegalArgumentException("can not build item");
}
// remove used resources
for (final Card resource : item.resourcesNeeded()) {
// resources picked up last get used up first
@ -46,19 +70,36 @@ public class Inventory {
items.add(item);
}
/**
* @return items in the inventory
*/
public List<Item> getItems() {
// have to copy here: caller does not expect an auto-updating list
return new ArrayList<>(items);
}
/**
* @return stream of items in the inventory
*/
public Stream<Item> itemStream() {
return items.stream();
}
/**
* Check whether this inventory contains a specified item.
*
* @param item single item
* @return whether this inventory contains that item
*/
public boolean contains(Item item) {
return items.contains(item);
}
/**
* Remove an item, if it exists.
*
* @param item item to remove
*/
public void removeItem(Item item) {
items.remove(item);
}
@ -91,6 +132,9 @@ public class Inventory {
return Arrays.stream(resourcesNeeded).allMatch(Objects::isNull);
}
/**
* Clear the inventory, deleting all resources and items.
*/
public void clear() {
resources.clear();
items.clear();

View File

@ -52,7 +52,7 @@ public final class CommandLine {
command = CommandFactory.getCommand(input);
} catch (NumberFormatException | InvalidInputException e) {
Terminal.printError(e.getMessage());
continue;
continue; // to next command
}
// attempt to execute command
try {

View File

@ -5,7 +5,7 @@ import edu.kit.informatik.cardgame.model.LogicException;
import edu.kit.informatik.cardgame.ui.InvalidInputException;
/**
* Command that can be applied to a simulation.
* Command that can be applied to a card game.
* Commands are implemented as separate classes for easy modification and expansion.
*
* @author Arne Keller