-
-
Save louismrose/729166 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import java.util.*; | |
import org.junit.*; | |
public class SellOneItemTest { | |
public static class Sale { | |
private Display display; | |
private PriceList priceList; | |
public Sale(Display display, Map<String, String> pricesByBarcode) { | |
this.display = display; | |
this.priceList = new PriceList(pricesByBarcode); | |
} | |
public void onBarcode(String barcode) { | |
display.formatAndDisplay(textForBarcode(barcode)); | |
} | |
private String textForBarcode(String barcode) { | |
if (barcode.isEmpty()) | |
return "Scanning error: empty barcode"; | |
else if (priceList.doesNotInclude(barcode)) | |
return "No product with barcode " + barcode; | |
else | |
return priceList.priceFor(barcode); | |
} | |
} | |
public static class PriceList { | |
public Map<String, String> pricesByBarcode; | |
public PriceList(Map<String, String> pricesByBarcode) { | |
this.pricesByBarcode = pricesByBarcode; | |
} | |
public boolean doesNotInclude(String barcode) { | |
return !pricesByBarcode.containsKey(barcode); | |
} | |
public String priceFor(String barcode) { | |
return pricesByBarcode.get(barcode); | |
} | |
} | |
public static class Display { | |
private String text; | |
public void formatAndDisplay(String text) { | |
this.text = format(text); | |
} | |
private String format(String text) { | |
if (isZeroDollars(text)) { | |
return "FREE"; | |
} else { | |
return text; | |
} | |
} | |
private boolean isZeroDollars(String price) { | |
return "$0.00".equals(price); | |
} | |
public String getText() { | |
return text; | |
} | |
} | |
private static Map<String, String> products() { | |
return new HashMap<String, String>() { | |
{ | |
put("123", "$12.50"); | |
put("456", "$20.00"); | |
} | |
}; | |
} | |
@Test | |
public void productFound() throws Exception { | |
assertEquals("$12.50", textDisplayedFor("123")); | |
} | |
@Test | |
public void anotherProductFound() throws Exception { | |
assertEquals("$20.00", textDisplayedFor("456")); | |
} | |
@Test | |
public void productNotFound() throws Exception { | |
assertEquals("No product with barcode 999", textDisplayedFor("999")); | |
} | |
@Test | |
public void emptyBarcode() throws Exception { | |
assertEquals("Scanning error: empty barcode", textDisplayedFor("")); | |
} | |
@Test | |
public void noProductsToFind() throws Exception { | |
assertEquals("Scanning error: empty barcode", textDisplayedFor("", null)); | |
} | |
@Test | |
public void productWithPriceZeroDisplayedAsFree() throws Exception { | |
Map<String, String> aFreeProduct = new HashMap<String, String>() { | |
{ | |
put("789", "$0.00"); | |
} | |
}; | |
assertEquals("FREE", textDisplayedFor("789", aFreeProduct)); | |
} | |
@Test | |
public void zeroDisplayedAsFree() throws Exception { | |
Display display = new Display(); | |
display.formatAndDisplay("$0.00"); | |
assertEquals("FREE", display.getText()); | |
} | |
private static String textDisplayedFor(String barcode) { | |
return textDisplayedFor(barcode, products()); | |
} | |
private static String textDisplayedFor(String barcode, Map<String, String> pricesByBarcode) { | |
Display display = new Display(); | |
Sale sale = new Sale(display, pricesByBarcode); | |
sale.onBarcode(barcode); | |
return display.getText(); | |
} | |
} |
You're welcome, Louis. I'm glad I could help. Please write a little about your experience, and enjoy the practice. Take care.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Once more, thanks for the comments J. B.
I think, in the real world, a price list is an appropriate name for the thing that I'd use to find the price of a product. (It's a shame that there's no domain expert to ask here!)
Yes, you're right. setText() is a bad name now. I prefer formatAndDisplay in this version.
I think I'm done -- thanks for the guidance. I've been using some of the tips in my day-to-day work, and feel like my code is more expressive and easier to refactor as a result.