diff options
-rw-r--r-- | src/eu/equalparts/cardbase/Cardbase.java | 28 | ||||
-rw-r--r-- | src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 44 | ||||
-rw-r--r-- | test/eu/equalparts/cardbase/CardbaseSortTest.java | 2 | ||||
-rw-r--r-- | test/eu/equalparts/cardbase/CardbaseTest.java | 28 | ||||
-rw-r--r-- | test/eu/equalparts/cardbase/cli/CardbaseCLITest.java | 18 |
5 files changed, 59 insertions, 61 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index bdbdeb8..15e4860 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -85,12 +85,15 @@ public class Cardbase { * @param cardToAdd the card to be added. The count value * of this object is added to the existing count if the card * already exists. + *TODO fix comment */ - public void addCard(Card cardToAdd) { + public void addCard(Card cardToAdd, int count) { Card card = getCardByHash(cardToAdd.hashCode()); if (card != null) { - card.count += cardToAdd.count; + card.count += count; } else { + // TODO disgusting, refactor + cardToAdd.count = count; cards.put(cardToAdd.hashCode(), cardToAdd); } } @@ -110,9 +113,12 @@ public class Cardbase { * @param cardToRemove the card to be removed. * @param count the amount of the card to be removed. * @return the number of cards actually removed. + *TODO comment */ - public Integer removeCard(Card cardToRemove) { + public Integer removeCard(Card cardToRemove, int count) { Card card = getCardByHash(cardToRemove.hashCode()); + // TODO disgusting, refactor + cardToRemove.count = count; Integer removed = 0; if (card != null) { if (card.count <= cardToRemove.count) { @@ -126,14 +132,14 @@ public class Cardbase { return removed; } -// /** -// * Returns a card from the cardbase by set code and number. -// * If no such card is in the cardbase, returns null. -// * -// * @param setCode the set to which the requested card belongs. -// * @param number the requested card's set number. -// * @return the requested {@code Card} or null if no card is found. -// */ + /** + * Returns a card from the cardbase by set code and number. + * If no such card is in the cardbase, returns null. + * + * @param setCode the set to which the requested card belongs. + * @param number the requested card's set number. + * @return the requested {@code Card} or null if no card is found. + */ public Card getCard(String setCode, String number) { return cards.get(Card.makeHash(setCode, number)); } diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index b55b73d..d526e78 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -33,6 +33,7 @@ public final class CardbaseCLI { private enum Action { ADD, REMOVE; public Card card; + public int count; } /** @@ -385,8 +386,7 @@ public final class CardbaseCLI { if (count.matches("[-]?[0-9]+")) { Integer intCount = Integer.valueOf(count); if (intCount > 0) { - cardToRemove.count = intCount; - removeCard(cardToRemove); + removeCard(cardToRemove, intCount); } else { System.out.println("Cannot remove " + count + " cards."); } @@ -415,23 +415,27 @@ public final class CardbaseCLI { // a blank line after adding a card repeats the addition unlimitedly if (number.isEmpty()) { if (lastAction == Action.ADD) { - addCard(lastAction.card); + addCard(lastAction.card, lastAction.count); } else { System.out.println("Please enter a card number."); } } else { Card cardToAdd = selectedSet.getCardByNumber(number); if (cardToAdd != null) { - Integer count = 1; - if (args != null && args.length > 0 && args[0].matches("[0-9]+")) { - count = Integer.valueOf(args[0]); - if (count <= 0) { - System.out.println("Cannot add " + count + " cards."); - return; + String count = args != null && args.length > 0 ? args[0] : "1"; + + if (count.matches("[-]?[0-9]+")) { + + Integer intCount = Integer.valueOf(count); + + if (intCount > 0) { + addCard(cardToAdd, intCount); + } else { + System.out.println("Cannot add " + intCount + " cards."); } + } else { + System.out.println(count + " is not a valid number of cards."); } - cardToAdd.count = count; - addCard(cardToAdd); } else { System.out.println(number + " does not correspond to a card in " + selectedSet.name + "."); } @@ -447,9 +451,9 @@ public final class CardbaseCLI { private void undo() { if (lastAction != null) { if (lastAction == Action.ADD) { - removeCard(lastAction.card); + removeCard(lastAction.card, lastAction.count); } else if (lastAction == Action.REMOVE) { - addCard(lastAction.card); + addCard(lastAction.card, lastAction.count); } // can only undo once lastAction = null; @@ -464,13 +468,15 @@ public final class CardbaseCLI { * * @param card the card to add, set this object's * count field to determine the count to add. + * TODO comment */ - private void addCard(Card card) { - System.out.println("Added " + card.count + "x " + card.name + "."); - cardbase.addCard(card); + private void addCard(Card card, int count) { + System.out.println("Added " + count + "x " + card.name + "."); + cardbase.addCard(card, count); savePrompt = true; lastAction = Action.ADD; lastAction.card = card; + lastAction.count = count; } /** @@ -479,14 +485,16 @@ public final class CardbaseCLI { * * @param card the card to remove, set this object's count field * to determine how many of the card to remove. + * TODO comment */ - private void removeCard(Card card) { - Integer removed = cardbase.removeCard(card); + private void removeCard(Card card, int count) { + Integer removed = cardbase.removeCard(card, count); if (removed > 0) { System.out.println("Removed " + removed + "x " + card.name + "."); savePrompt = true; lastAction = Action.REMOVE; lastAction.card = card; + lastAction.count = removed; } else { System.out.println(card.name + " is not in the cardbase."); } diff --git a/test/eu/equalparts/cardbase/CardbaseSortTest.java b/test/eu/equalparts/cardbase/CardbaseSortTest.java index 44b68be..e3a92ee 100644 --- a/test/eu/equalparts/cardbase/CardbaseSortTest.java +++ b/test/eu/equalparts/cardbase/CardbaseSortTest.java @@ -41,7 +41,7 @@ public class CardbaseSortTest { public void setUp() throws Exception { uut = new Cardbase(); for (Card card : testCards) { - uut.addCard(card); + uut.addCard(card, card.count); } } diff --git a/test/eu/equalparts/cardbase/CardbaseTest.java b/test/eu/equalparts/cardbase/CardbaseTest.java index 82b05ba..424befc 100644 --- a/test/eu/equalparts/cardbase/CardbaseTest.java +++ b/test/eu/equalparts/cardbase/CardbaseTest.java @@ -144,7 +144,7 @@ public class CardbaseTest { uut = new Cardbase(testFile); assertEquals("Cardbase should contain no cards.", 0, uut.getCards().size()); - uut.addCard(testCard); + uut.addCard(testCard, testCard.count); uut.writeCollection(testFile); uut = new Cardbase(testFile); @@ -176,7 +176,7 @@ public class CardbaseTest { ***********************************************************************************/ @Test public void newCardIsAdded() throws Exception { - uut.addCard(testCard); + uut.addCard(testCard, testCard.count); Card addedCard = uut.getCard("M15", "281"); assertNotNull("Card was not found in cardbase.", addedCard); @@ -186,10 +186,10 @@ public class CardbaseTest { @Test public void existingCardIsIncremented() throws Exception { Card shivanClone = testCard.clone(); - uut.addCard(shivanClone); + uut.addCard(shivanClone, shivanClone.count); Card shivanClone2 = testCard.clone(); shivanClone2.count = 2; - uut.addCard(shivanClone2); + uut.addCard(shivanClone2, shivanClone2.count); Card addedCard = uut.getCard("M15", "281"); assertNotNull("Card was not found in cardbase.", addedCard); @@ -202,7 +202,7 @@ public class CardbaseTest { @Test public void cardAddedIsNull() throws Exception { exception.expect(NullPointerException.class); - uut.addCard(null); + uut.addCard(null, 0); } /*********************************************************************************** @@ -212,9 +212,9 @@ public class CardbaseTest { public void cardRemoveCountIsLessThanCardCount() throws Exception { Card shivanClone = testCard.clone(); shivanClone.count = 5; - uut.addCard(shivanClone); + uut.addCard(shivanClone, shivanClone.count); - int removed = uut.removeCard(testCard); + int removed = uut.removeCard(testCard, testCard.count); Card removedCard = uut.getCard("M15", "281"); assertNotNull("Card was not found in cardbase.", removedCard); @@ -225,9 +225,9 @@ public class CardbaseTest { @Test public void cardRemoveCountIsEqualToCardCount() throws Exception { Card shivanClone = testCard.clone(); - uut.addCard(shivanClone); + uut.addCard(shivanClone, shivanClone.count); - int removed = uut.removeCard(testCard); + int removed = uut.removeCard(testCard, testCard.count); Card removedCard = uut.getCard("M15", "281"); assertNull("Card was not removed from cardbase.", removedCard); @@ -238,11 +238,11 @@ public class CardbaseTest { public void cardRemoveCountIsGreaterThanCardCount() throws Exception { Card shivanClone = testCard.clone(); shivanClone.count = 3; - uut.addCard(shivanClone); + uut.addCard(shivanClone, shivanClone.count); Card shivanClone2 = testCard.clone(); shivanClone2.count = 5; - int removed = uut.removeCard(shivanClone2); + int removed = uut.removeCard(shivanClone2, shivanClone2.count); Card removedCard = uut.getCard("M15", "281"); assertNull("Card was not removed from cardbase.", removedCard); @@ -255,12 +255,12 @@ public class CardbaseTest { @Test public void removedCardIsNull() throws Exception { exception.expect(NullPointerException.class); - uut.removeCard(null); + uut.removeCard(null, 0); } @Test public void removedCardIsNotInCardbase() throws Exception { - int removed = uut.removeCard(testCard); + int removed = uut.removeCard(testCard, testCard.count); assertEquals("Removed count should be 0.", 0, removed); } @@ -270,7 +270,7 @@ public class CardbaseTest { ***********************************************************************************/ @Test public void correctCardIsReturnedByGetter() throws Exception { - uut.addCard(testCard.clone()); + uut.addCard(testCard.clone(), testCard.count); Card card = uut.getCard("M15", "281"); diff --git a/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java b/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java index 5756337..9a98469 100644 --- a/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java +++ b/test/eu/equalparts/cardbase/cli/CardbaseCLITest.java @@ -7,10 +7,7 @@ import java.io.File; import java.io.PrintStream; import java.util.Scanner; -import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -44,24 +41,11 @@ public class CardbaseCLITest { @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); - @BeforeClass - public static void setUpBeforeClass() throws Exception { - - } - - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - @Before public void setUp() throws Exception { uut = new CardbaseCLI(TEST_REMOTE); testOutput.reset(); } - - @After - public void tearDown() throws Exception { - } /*********************************************************************************** * Start up tests, happy path @@ -1065,7 +1049,7 @@ public class CardbaseCLITest { } finally { System.setOut(console); } - assertEquals("Wrong glance.", "20 Cunning Strike (FRF, 150)\nTotal: 20" + EOL, testOutput.toString()); + assertEquals("Wrong glance.", "20 Cunning Strike (FRF, 150)\nTotal: 20" + EOL, testOutput.toString()); } /* |