From 8e3bd2bd2517c1074971da8ace762e28264fbbd2 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Wed, 11 Mar 2020 10:27:13 +0100 Subject: [PATCH] Use logic exception in model classes --- src/edu/kit/informatik/model/CardGame.java | 44 +++++++++---------- .../kit/informatik/model/LogicException.java | 7 +++ .../informatik/ui/InvalidInputException.java | 2 +- 3 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 src/edu/kit/informatik/model/LogicException.java diff --git a/src/edu/kit/informatik/model/CardGame.java b/src/edu/kit/informatik/model/CardGame.java index 2cf7331..cbf5b95 100644 --- a/src/edu/kit/informatik/model/CardGame.java +++ b/src/edu/kit/informatik/model/CardGame.java @@ -40,16 +40,16 @@ public class CardGame { /** * Start a new game with the specified stack of cards. * @param cardStack stack of cards to use, where the first element is the first to take - * @throws InvalidInputException if card stack has wrong distribution of cards + * @throws LogicException if card stack has wrong distribution of cards * @return whether the game could be successfully started */ - public boolean start(Deque cardStack) throws InvalidInputException { + public boolean start(Deque cardStack) throws LogicException { if (cardStack == null || currentCardStack == null) { if (Collections.frequency(cardStack, WOOD) != 16 || Collections.frequency(cardStack, PLASTIC) != 16 || Collections.frequency(cardStack, METAL) != 16 || Collections.frequency(cardStack, SPIDER) != 5 || Collections.frequency(cardStack, SNAKE) != 5 || Collections.frequency(cardStack, TIGER) != 5 || Collections.frequency(cardStack, THUNDERSTORM) != 1) { - throw new InvalidInputException("invalid deck: missing or surplus cards"); + throw new LogicException("invalid deck: missing or surplus cards"); } this.cardStack = new ArrayDeque<>(cardStack); reset(); @@ -59,11 +59,11 @@ public class CardGame { } } - public Card draw() throws InvalidInputException { + public Card draw() throws LogicException { if (currentCardStack == null || currentCardStack.isEmpty()) { - throw new InvalidInputException("no card to draw exists"); + throw new LogicException("no card to draw exists"); } else if (awaitedDiceSize != null) { - throw new InvalidInputException("roll dice, please"); + throw new LogicException("roll dice, please"); } final Card card = currentCardStack.removeFirst(); if (card.isResource()) { @@ -82,13 +82,13 @@ public class CardGame { return card; } - public String rollDice(int size, int roll) throws InvalidInputException { + public String rollDice(int size, int roll) throws LogicException { if (awaitedDiceSize == null) { - throw new InvalidInputException("not expecting dice roll"); + throw new LogicException("not expecting dice roll"); } else if (awaitedDiceSize != size) { - throw new InvalidInputException("unexpected dice size"); + throw new LogicException("unexpected dice size"); } else if (roll > awaitedDiceSize || roll < 1) { - throw new InvalidInputException("impossible roll"); + throw new LogicException("impossible roll"); } awaitedDiceSize = null; final int minimumNeeded = minimumDiceRoll; @@ -120,13 +120,13 @@ public class CardGame { } } - public String build(Item item) throws InvalidInputException { + public String build(Item item) throws LogicException { if (item == null) { - throw new InvalidInputException("can not build item"); + throw new LogicException("can not build item"); } else if (item.requiresFireplace() && !items.contains(FIREPLACE)) { - throw new InvalidInputException("need fireplace to build"); + throw new LogicException("need fireplace to build"); } else if (items.contains(item) && !item.canHaveMoreThanOne()) { - throw new InvalidInputException("already built"); + throw new LogicException("already built"); } else if (canBuild(item)) { // remove used resources for (final Card resource : item.resourcesNeeded()) { @@ -175,25 +175,25 @@ public class CardGame { return currentCardStack != null; } - public Deque getResources() throws InvalidInputException { + public Deque getResources() throws LogicException { if (!gameStarted()) { - throw new InvalidInputException("can not get resources: game not started"); + throw new LogicException("can not get resources: game not started"); } // do not allow caller to modify internal queue return new ArrayDeque<>(resources); } - public List getItems() throws InvalidInputException { + public List getItems() throws LogicException { if (!gameStarted()) { - throw new InvalidInputException("can not get buildings: game not started"); + throw new LogicException("can not get buildings: game not started"); } // do not allow caller to modify internal list return new ArrayList<>(items); } - public Set getBuildableItems() throws InvalidInputException { + public Set getBuildableItems() throws LogicException { if (!gameStarted()) { - throw new InvalidInputException("can not get buildable items: game not started"); + throw new LogicException("can not get buildable items: game not started"); } return Arrays.stream(Item.values()).filter(this::canBuild).collect(Collectors.toSet()); } @@ -202,9 +202,9 @@ public class CardGame { currentCardStack = null; } - public void reset() { + public void reset() throws LogicException { if (cardStack == null) { - throw new InvalidInputException("can not reset a game that is not started!"); + throw new LogicException("can not reset a game that is not started!"); } else { this.currentCardStack = new ArrayDeque<>(cardStack); } diff --git a/src/edu/kit/informatik/model/LogicException.java b/src/edu/kit/informatik/model/LogicException.java new file mode 100644 index 0000000..a19e5ac --- /dev/null +++ b/src/edu/kit/informatik/model/LogicException.java @@ -0,0 +1,7 @@ +package edu.kit.informatik.model; + +public class LogicException extends Exception { + public LogicException(String message) { + super(message); + } +} diff --git a/src/edu/kit/informatik/ui/InvalidInputException.java b/src/edu/kit/informatik/ui/InvalidInputException.java index a9bc8e2..8a3c82f 100644 --- a/src/edu/kit/informatik/ui/InvalidInputException.java +++ b/src/edu/kit/informatik/ui/InvalidInputException.java @@ -1,6 +1,6 @@ package edu.kit.informatik.ui; -public class InvalidInputException extends RuntimeException { +public class InvalidInputException extends Exception { public InvalidInputException(String message) { super(message); }