Guest User

Untitled

a guest
Jul 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. ackage ca.jbrains.pos.test;
  2.  
  3. import org.junit.*;
  4. import static org.junit.Assert.*;
  5. import java.util.*;
  6.  
  7. public class SellOneItemTest {
  8. private POSDisplay posDisplay = new POSDisplay();
  9. private Sale sale;
  10.  
  11. public interface Catalog {
  12. Price lookupPrice(String barcode);
  13. boolean hasBarcode(String barcode);
  14. }
  15.  
  16. public static class InMemoryCatalog implements Catalog {
  17. private Map<String, Price> pricesByBarcode;
  18.  
  19. public InMemoryCatalog(Map<String, Price> pricesByBarcode) {
  20. if (pricesByBarcode == null) {
  21. throw new IllegalArgumentException("pricesByBarcode = " + pricesByBarcode);
  22. }
  23.  
  24. this.pricesByBarcode = pricesByBarcode;
  25. }
  26.  
  27. public Price lookupPrice(String barcode) {
  28. return pricesByBarcode.get(barcode);
  29. }
  30.  
  31. public boolean hasBarcode(String barcode) {
  32. return pricesByBarcode.containsKey(barcode);
  33. }
  34. }
  35.  
  36. public static class Sale {
  37. private POSDisplay posDisplay;
  38. private Catalog catalog;
  39.  
  40. public Sale(POSDisplay posDisplay, Catalog catalog) {
  41. this.posDisplay = posDisplay;
  42. this.catalog = catalog;
  43. }
  44.  
  45. public void onBarcode(String barcode) {
  46. if ("".equals(barcode)) {
  47. posDisplay.displayScannedEmptyBarcodeMessage();
  48. return;
  49. }
  50.  
  51. if (!catalog.hasBarcode(barcode)) {
  52. posDisplay.displayProductNotFoundMessage(barcode);
  53. return;
  54. }
  55.  
  56. posDisplay.displayPrice(catalog.lookupPrice(barcode));
  57. }
  58. }
  59.  
  60. public static class Price {
  61. private int cents;
  62.  
  63. public Price(int cents) {
  64. this.cents = cents;
  65. }
  66.  
  67. public static Price inCents(int cents) {
  68. return new Price(cents);
  69. }
  70.  
  71. public String format() {
  72. return cents == 0 ? "FREE" : NumberFormat.getCurrencyInstance().format(cents / 100.0d);
  73. }
  74. }
  75.  
  76. public static class POSDisplay {
  77. private String text;
  78.  
  79. private void setText(String text) {
  80. this.text = text;
  81. }
  82.  
  83. public String getText() {
  84. return text;
  85. }
  86.  
  87. public void displayScannedEmptyBarcodeMessage() {
  88. setText("Scanning error: empty barcode");
  89. }
  90.  
  91. public void displayProductNotFoundMessage(String barcode) {
  92. setText("No product with barcode " + barcode);
  93. }
  94.  
  95. public void displayPrice(Price price) {
  96. setText(price.format());
  97. }
  98. }
  99.  
  100. @SuppressWarnings("serial")
  101. @Before
  102. public void setUp() {
  103. sale = new Sale(posDisplay, new InMemoryCatalog(new HashMap<String, Price>() {
  104. {
  105. put("123", Price.inCents(1250));
  106. put("456", Price.inCents(2000));
  107. put("333", Price.inCents(0));
  108. }
  109. }));
  110. }
  111.  
  112. @Test
  113. public void productFound() throws Exception {
  114. assertForBarcodeDisplayShows("123", "$12.50");
  115. }
  116.  
  117. @Test
  118. public void anotherProductFound() throws Exception {
  119. assertForBarcodeDisplayShows("456", "$20.00");
  120. }
  121.  
  122. @Test
  123. public void productNotFound() throws Exception {
  124. assertForBarcodeDisplayShows("999", "No product with barcode 999");
  125. }
  126.  
  127. @Test
  128. public void emptyBarcode() throws Exception {
  129. assertForBarcodeDisplayShows("", "Scanning error: empty barcode");
  130. }
  131.  
  132. @Test
  133. public void freeProductHasDistinctFormat() throws Exception {
  134. assertEquals("FREE", Price.inCents(0).format());
  135. }
  136.  
  137. @Test(expected=RuntimeException.class)
  138. public void nullProductListIsInvalid() {
  139. new InMemoryCatalog(null);
  140. }
  141.  
  142. private void assertForBarcodeDisplayShows(String barcode, String message) {
  143. sale.onBarcode(barcode);
  144. assertEquals(message, posDisplay.getText());
  145. }
  146. }
Add Comment
Please, Sign In to add comment