Skip to content

Commit

Permalink
Added more scenarios to CoinCoverter, changed Coins.NULL to Coins.NON…
Browse files Browse the repository at this point in the history
…E and updated Insert coins to Insert coins or select product
  • Loading branch information
TonyBrobston committed Jul 28, 2016
1 parent d708ac7 commit 560a7cd
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/vendingmachine/constants/Coins.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.math.BigDecimal;

public final class Coins {
public static final BigDecimal NULL = BigDecimal.ZERO;
public static final BigDecimal NONE = BigDecimal.ZERO;
public static final BigDecimal PENNY = new BigDecimal(0.01);
public static final BigDecimal NICKEL = new BigDecimal(0.05);
public static final BigDecimal DIME = new BigDecimal(0.10);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/vendingmachine/converter/CoinConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public BigDecimal toCoin(String entry) {
} else if (isHalfDollar(entryWithoutSpaces)) {
return HALF_DOLLAR;
} else {
return NULL;
return NONE;
}
}

Expand All @@ -31,15 +31,15 @@ private boolean isNickel(String entry) {
}

private boolean isDime(String entry) {
return "dime".equalsIgnoreCase(entry) || ".10".equals(entry) || "0.10".equals(entry);
return "dime".equalsIgnoreCase(entry) || ".10".equals(entry) || "0.10".equals(entry) || ".1".equals(entry) || "0.1".equals(entry);
}

private boolean isQuarter(String entry) {
return "quarter".equalsIgnoreCase(entry) || ".25".equals(entry) || "0.25".equals(entry);
}

private boolean isHalfDollar(String entry) {
return "halfdollar".equalsIgnoreCase(entry) || ".50".equals(entry) || "0.50".equals(entry);
return "halfdollar".equalsIgnoreCase(entry) || ".50".equals(entry) || "0.50".equals(entry) || ".5".equals(entry) || "0.5".equals(entry);
}

private String removeSpaces(String entry) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vendingmachine/service/CoinService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private String determineConsoleOutput(Coin coin) {
}

private void askForNextCoin() {
System.out.print("Insert coin: ");
System.out.print("Insert coin or select a product: ");
}

private String formatCurrency(BigDecimal runningTotal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ public static Object[][] toCoinDataProvier() {
{ ".01", PENNY },
{ ".05", NICKEL },
{ ".10", DIME },
{ ".1", DIME },
{ ".25", QUARTER },
{ ".50", HALF_DOLLAR },
{ ".5", HALF_DOLLAR },
{ "0.01", PENNY },
{ "0.05", NICKEL },
{ "0.10", DIME },
{ "0.1", DIME },
{ "0.25", QUARTER },
{ "0.50", HALF_DOLLAR },
{ "", NULL },
{ "asdf", NULL }
{ "0.5", HALF_DOLLAR },
{ "", NONE},
{ "asdf", NONE}
};
}

Expand Down
26 changes: 13 additions & 13 deletions src/test/java/com/vendingmachine/service/CoinServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ public void cleanUpStreams() {
System.setOut(null);
}

@Test
public void shouldPrintInsertCoinIfNoCoinIsInserted() {
new CoinService();

assertEquals("Insert coin or select a product: ", byteArrayOutputStream.toString());
}

@Test
public void shouldPrintCurrentAmountFiveToConsole() {
Coin nickel = new Coin().setValue(NICKEL);

new CoinService().insertCoin(nickel);

assertEquals("Insert coin: Current amount: $0.05\nInsert coin: ", byteArrayOutputStream.toString());
assertEquals("Insert coin or select a product: Current amount: $0.05\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}

@Test
Expand All @@ -43,7 +50,7 @@ public void shouldPrintCurrentAmountFiveCurrentAmountTenCurrentAmountThirtyFive(
coinService.insertCoin(nickel);
coinService.insertCoin(quarter);

assertEquals("Insert coin: Current amount: $0.05\nInsert coin: Current amount: $0.10\nInsert coin: Current amount: $0.35\nInsert coin: ", byteArrayOutputStream.toString());
assertEquals("Insert coin or select a product: Current amount: $0.05\nInsert coin or select a product: Current amount: $0.10\nInsert coin or select a product: Current amount: $0.35\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}

@Test
Expand All @@ -55,7 +62,7 @@ public void shouldPrintCurrentAmountFiveCoinReturnOne() {
coinService.insertCoin(nickel);
coinService.insertCoin(penny);

assertEquals("Insert coin: Current amount: $0.05\nInsert coin: Coin return: $0.01\nInsert coin: ", byteArrayOutputStream.toString());
assertEquals("Insert coin or select a product: Current amount: $0.05\nInsert coin or select a product: Coin return: $0.01\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}

@Test
Expand All @@ -64,7 +71,7 @@ public void shouldPrintCurrentAmountTwentyFive() {

new CoinService().insertCoin(quarter);

assertEquals("Insert coin: Current amount: $0.25\nInsert coin: ", byteArrayOutputStream.toString());
assertEquals("Insert coin or select a product: Current amount: $0.25\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}

@Test
Expand All @@ -73,7 +80,7 @@ public void shouldPrintCoinReturnOne() {

new CoinService().insertCoin(penny);

assertEquals("Insert coin: Coin return: $0.01\nInsert coin: ", byteArrayOutputStream.toString());
assertEquals("Insert coin or select a product: Coin return: $0.01\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}

@Test
Expand All @@ -82,13 +89,6 @@ public void shouldPrintCoinReturnFifty() {

new CoinService().insertCoin(halfDollar);

assertEquals("Insert coin: Coin return: $0.50\nInsert coin: ", byteArrayOutputStream.toString());
}

@Test
public void shouldPrintInsertCoinIfNoCoinIsInserted() {
new CoinService();

assertEquals("Insert coin: ", byteArrayOutputStream.toString());
assertEquals("Insert coin or select a product: Coin return: $0.50\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}
}

0 comments on commit 560a7cd

Please sign in to comment.