Skip to content

Commit

Permalink
Refactoring and more separation of responsibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyBrobston committed Aug 3, 2016
1 parent 4580f50 commit 809e0e2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/vendingmachine/domain/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
public class Product {
private BigDecimal value;

public boolean isEnoughCoin(BigDecimal runningTotal) {
if (runningTotal == null) {
return false;
}
return runningTotal.compareTo(this.value) >= 0;
}

public Product setValue(String value) {
this.value = new ProductConverter().toProduct(value);
return this;
Expand All @@ -20,11 +27,4 @@ public Product setValue(BigDecimal value) {
this.value = value;
return this;
}

public boolean isEnoughCoin(BigDecimal runningTotal) {
if (runningTotal == null) {
return false;
}
return runningTotal.compareTo(this.value) >= 0;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/vendingmachine/formatter/Formatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.vendingmachine.formatter;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class Formatter {
public String toCurrency(BigDecimal bigDecimal) {
return NumberFormat.getCurrencyInstance().format(bigDecimal);
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/vendingmachine/output/Output.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.vendingmachine.output;

import com.vendingmachine.domain.Product;
import com.vendingmachine.formatter.Formatter;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class Output {
protected void displayWelcome() {
Expand All @@ -19,26 +19,23 @@ protected void askForNextCoin() {
}

protected void displayCurrentAmount(BigDecimal runningTotal) {
System.out.println("Current amount: " + formatCurrency(runningTotal));
System.out.println("Current amount: " + new Formatter().toCurrency(runningTotal));
}

protected void displayProductDispensed() {
System.out.println("Product dispensed, thank you");
}

protected void displayNotEnoughMoney(Product product) {
System.out.println("Not enough money, price is " + formatCurrency(product.getValue()));
System.out.println("Not enough money, price is " + new Formatter().toCurrency(product.getValue()));
}

protected void displayCoinReturn(BigDecimal value) {
System.out.println("Coin return: " + formatCurrency(value));
System.out.println("Coin return: " + new Formatter().toCurrency(value));
}

protected void displayNotAValidInput() {
System.out.println("Not a valid input or sold out.");
}

protected String formatCurrency(BigDecimal runningTotal) {
return NumberFormat.getCurrencyInstance().format(runningTotal);
}
}
18 changes: 18 additions & 0 deletions src/test/java/com/vendingmachine/formatter/FormatterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.vendingmachine.formatter;

import org.junit.Test;

import java.math.BigDecimal;

import static org.junit.Assert.assertEquals;

public class FormatterTest {
@Test
public void shouldFormatMoney() {
BigDecimal bigDecimal = new BigDecimal(13.37);

String currency = new Formatter().toCurrency(bigDecimal);

assertEquals("$13.37", currency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public void cleanUpStreams() {
public void shouldPrintInsertCoinIfInitialStartup() {
new VendingMachineService().initialStartUp();

assertEquals("Welcome to this super awesome Vending Machine application\nCoin options: NICKEL, DIME, QUARTER. Product options: COLA, CHIPS, CANDY. Other commands: RETURN, END.\nInsert coin, select a product, or return: ", byteArrayOutputStream.toString());
assertEquals("Welcome to this super awesome Vending Machine application\n" +
"Coin options: NICKEL, DIME, QUARTER. Product options: COLA, CHIPS, CANDY. Other commands: RETURN, END.\n" +
"Insert coin, select a product, or return: ", byteArrayOutputStream.toString());
}

@Test
Expand All @@ -44,10 +46,10 @@ public void shouldPrintCurrentAmountFiveToConsole() {

@Test
public void shouldPrintCurrentAmountFiveCurrentAmountTenCurrentAmountThirtyFive() {
VendingMachineService vendingMachineService = new VendingMachineService();
Coin nickel = new Coin().setValue(NICKEL);
Coin quarter = new Coin().setValue(QUARTER);

VendingMachineService vendingMachineService = new VendingMachineService();
vendingMachineService.inputCoin(nickel);
vendingMachineService.inputCoin(nickel);
vendingMachineService.inputCoin(quarter);
Expand All @@ -59,10 +61,10 @@ public void shouldPrintCurrentAmountFiveCurrentAmountTenCurrentAmountThirtyFive(

@Test
public void shouldPrintCurrentAmountFiveCoinReturnOne() {
VendingMachineService vendingMachineService = new VendingMachineService();
Coin nickel = new Coin().setValue(NICKEL);
Coin penny = new Coin().setValue(PENNY);

VendingMachineService vendingMachineService = new VendingMachineService();
vendingMachineService.inputCoin(nickel);
vendingMachineService.inputCoin(penny);

Expand Down

0 comments on commit 809e0e2

Please sign in to comment.