Skip to content

Instantly share code, notes, and snippets.

@stephanhuez
Forked from jbrains/SellOneItemTest.java
Created December 3, 2010 07:28
Show Gist options
  • Save stephanhuez/726682 to your computer and use it in GitHub Desktop.
Save stephanhuez/726682 to your computer and use it in GitHub Desktop.

Revisions

  1. @jbrains jbrains created this gist Nov 30, 2010.
    104 changes: 104 additions & 0 deletions SellOneItemTest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    package ca.jbrains.pos.test;

    import static org.junit.Assert.*;

    import java.util.*;

    import org.junit.*;

    public class SellOneItemTest {
    public static class Sale {
    private Display display;
    private Map<String, String> pricesByBarcode;

    public Sale(Display display, Map<String, String> pricesByBarcode) {
    this.display = display;
    this.pricesByBarcode = pricesByBarcode;
    }

    public void onBarcode(String barcode) {
    if ("".equals(barcode)) {
    display.setText("Scanning error: empty barcode");
    return;
    }

    if (pricesByBarcode.containsKey(barcode))
    display.setText(pricesByBarcode.get(barcode));
    else
    display.setText("No product with barcode " + barcode);
    }

    }

    public static class Display {
    private String text;

    public void setText(String text) {
    this.text = text;
    }

    public String getText() {
    return text;
    }
    }

    @Test
    public void productFound() throws Exception {
    Display display = new Display();
    Sale sale = new Sale(display, new HashMap<String, String>() {
    {
    put("123", "$12.50");
    put("456", "$20.00");
    }
    });

    sale.onBarcode("123");

    assertEquals("$12.50", display.getText());
    }

    @Test
    public void anotherProductFound() throws Exception {
    Display display = new Display();
    Sale sale = new Sale(display, new HashMap<String, String>() {
    {
    put("123", "$12.50");
    put("456", "$20.00");
    }
    });

    sale.onBarcode("456");

    assertEquals("$20.00", display.getText());
    }

    @Test
    public void productNotFound() throws Exception {
    Display display = new Display();
    Sale sale = new Sale(display, new HashMap<String, String>() {
    {
    put("123", "$12.50");
    put("456", "$20.00");
    }
    });

    sale.onBarcode("999");

    assertEquals("No product with barcode 999", display.getText());
    }

    @Test
    public void emptyBarcode() throws Exception {
    Display display = new Display();
    Sale sale = new Sale(display, new HashMap<String, String>() {
    {
    put("123", "$12.50");
    put("456", "$20.00");
    }
    });

    sale.onBarcode("");

    assertEquals("Scanning error: empty barcode", display.getText());
    }
    }