From 58a2a232206943a346f20be8e82b483620a79f22 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Thu, 19 Mar 2020 14:31:57 +0100 Subject: [PATCH] Javadoc --- .../informatik/cardgame/model/Inventory.java | 48 ++++++++++++++++++- .../informatik/cardgame/ui/CommandLine.java | 2 +- .../cardgame/ui/command/Command.java | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/edu/kit/informatik/cardgame/model/Inventory.java b/src/edu/kit/informatik/cardgame/model/Inventory.java index 05bc6b2..ac41812 100644 --- a/src/edu/kit/informatik/cardgame/model/Inventory.java +++ b/src/edu/kit/informatik/cardgame/model/Inventory.java @@ -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 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 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 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(); diff --git a/src/edu/kit/informatik/cardgame/ui/CommandLine.java b/src/edu/kit/informatik/cardgame/ui/CommandLine.java index c654050..63d311d 100644 --- a/src/edu/kit/informatik/cardgame/ui/CommandLine.java +++ b/src/edu/kit/informatik/cardgame/ui/CommandLine.java @@ -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 { diff --git a/src/edu/kit/informatik/cardgame/ui/command/Command.java b/src/edu/kit/informatik/cardgame/ui/command/Command.java index 8fe88e2..fc8db9a 100644 --- a/src/edu/kit/informatik/cardgame/ui/command/Command.java +++ b/src/edu/kit/informatik/cardgame/ui/command/Command.java @@ -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