Skip to content

Commit

Permalink
Wrote validation to determine whether the input was a coin or a produ…
Browse files Browse the repository at this point in the history
…ct and then sent it down the previously built bath (either insertCoin or dispenseProduct)
  • Loading branch information
TonyBrobston committed Jul 28, 2016
1 parent bd91f6e commit 8ba9793
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 6 deletions.
1 change: 1 addition & 0 deletions .idea/vending-machine-kata.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
<version>1.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion src/main/java/com/vendingmachine/domain/Coin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class Coin {
private BigDecimal value = BigDecimal.ZERO;

public boolean isValid() {
public boolean isValidForUse() {
List<BigDecimal> validCoins = Arrays.asList(NICKEL, DIME, QUARTER);
return validCoins.contains(this.value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.vendingmachine.domain.Coin;
import com.vendingmachine.domain.Product;
import com.vendingmachine.validator.Validator;

import java.math.BigDecimal;
import java.text.NumberFormat;
Expand All @@ -13,6 +14,14 @@ public VendingMachineService() {
askForNextCoin();
}

public void input(String value) {
if (new Validator().isACoin(value)) {
insertCoin(new Coin().setValue(value));
} else {
dispenseProduct(new Product().setValue(value));
}
}

public void insertCoin(Coin coin) {
displayRunningTotalOrCoinReturn(coin);
askForNextCoin();
Expand All @@ -30,7 +39,7 @@ public void dispenseProduct(Product product) {
}

private void displayRunningTotalOrCoinReturn(Coin coin) {
if (coin.isValid()) {
if (coin.isValidForUse()) {
runningTotal = runningTotal.add(coin.getValue());
displayRunningTotal();
} else {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/vendingmachine/validator/Validator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.vendingmachine.validator;

import com.vendingmachine.converter.CoinConverter;
import com.vendingmachine.converter.ProductConverter;

import java.math.BigDecimal;

import static com.vendingmachine.constants.Coins.NONE;
import static com.vendingmachine.constants.Products.INVALID;

public class Validator {
public boolean isACoin(String value) {
BigDecimal coin = new CoinConverter().toCoin(value);
return !coin.equals(NONE);
}

public boolean isAProduct(String value) {
BigDecimal product = new ProductConverter().toProduct(value);
return !product.equals(INVALID);
}
}
6 changes: 3 additions & 3 deletions src/test/java/com/vendingmachine/domain/CoinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@RunWith(DataProviderRunner.class)
public class CoinTest {
@DataProvider
public static Object[][] coinDataProvier() {
public static Object[][] coinDataProvider() {
return new Object[][] {
{ NICKEL, true },
{ DIME, true },
Expand All @@ -25,9 +25,9 @@ public static Object[][] coinDataProvier() {
}

@Test
@UseDataProvider("coinDataProvier")
@UseDataProvider("coinDataProvider")
public void shouldPassIfIsValidCoin(BigDecimal value, boolean expected) {
assertEquals(expected, new Coin().setValue(value).isValid());
assertEquals(expected, new Coin().setValue(value).isValidForUse());
}

@DataProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void shouldPrintCoinReturnFifty() {
}

@Test
public void shouldTakeTwoQuartersAndDispenseChips() {
public void shouldTakeThreeQuartersAndDispenseChips() {
VendingMachineService vendingMachineService = new VendingMachineService();
Coin quarter = new Coin().setValue(QUARTER);
vendingMachineService.insertCoin(quarter);
Expand Down Expand Up @@ -135,4 +135,20 @@ public void shouldTakeOneQuartersAndAttemptToDispenseChips() {
"Not enough money, price is $0.50\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}

@Test
public void shouldWorkWithStringsToTakeThreeQuartersAndDispenseChips() {
VendingMachineService vendingMachineService = new VendingMachineService();
vendingMachineService.input("quarter");
vendingMachineService.input("quarter");
vendingMachineService.input("quarter");

vendingMachineService.input("chips");

assertEquals("Insert coin or select a product: " +
"Current amount: $0.25\nInsert coin or select a product: " +
"Current amount: $0.50\nInsert coin or select a product: " +
"Current amount: $0.75\nInsert coin or select a product: " +
"Product dispensed, thank you\nCurrent amount: $0.00\nInsert coin or select a product: ", byteArrayOutputStream.toString());
}

}
42 changes: 42 additions & 0 deletions src/test/java/com/vendingmachine/validator/ValidatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.vendingmachine.validator;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

@RunWith(DataProviderRunner.class)
public class ValidatorTest {
@DataProvider
public static Object[][] coinDataProvider() {
return new Object[][] {
{ "nickel", true },
{ "dime", true },
{ "chips", false }
};
}

@Test
@UseDataProvider("coinDataProvider")
public void shouldPassIfIsValidCoin(String value, boolean expected) {
assertEquals(expected, new Validator().isACoin(value));
}

@DataProvider
public static Object[][] productDataProvider() {
return new Object[][] {
{ "chips", true },
{ "candy", true },
{ "dime", false }
};
}

@Test
@UseDataProvider("productDataProvider")
public void shouldPassIfIsValidProduct(String value, boolean expected) {
assertEquals(expected, new Validator().isAProduct(value));
}
}

0 comments on commit 8ba9793

Please sign in to comment.