tdd-java-demo

tdd java demo with micro-commits

git clone https://9o.is/git/tdd-java-demo.git

commit ff2a094031ebcbb33008b6c24de8805fd936d057
parent 1767a622f1ff799ba3a79b9143c5c9294ea5160b
Author: Jul <jul@9o.is>
Date:   Wed, 31 Mar 2021 17:47:27 +0800

deducts a penny if there is an apple in the basket

Diffstat:
Msrc/main/java/takehome/Runner.java | 4++++
Msrc/test/java/takehome/RunnerTest.java | 19+++++++++++++++----
2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/main/java/takehome/Runner.java b/src/main/java/takehome/Runner.java @@ -28,6 +28,10 @@ public class Runner { Basket basket = new Basket(getItemAmounts(items)); double total = basket.getTotal(); + Item apple = items[3]; + double amount = basket.getAmounts().get(apple); + if (amount > 0) total -= 0.01; + DecimalFormat df = new DecimalFormat("0.00"); out.println("Total: $" + df.format(total)); } diff --git a/src/test/java/takehome/RunnerTest.java b/src/test/java/takehome/RunnerTest.java @@ -14,17 +14,28 @@ class RunnerTest { @Test public void example() { - assertThat(output(), contains( + assertThat(output("1\r1\r1\r0"), contains( "How much soup? ", "How much bread? ", "How much milk? ", "How much apple? ", - "Total: $2.85" + "Total: $2.75" )); } - private List<String> output() { - ByteArrayInputStream in = new ByteArrayInputStream("1\r1\r1\r1".getBytes()); + @Test + public void exampleWithDiscount() { + assertThat(output("1\r1\r1\r1"), contains( + "How much soup? ", + "How much bread? ", + "How much milk? ", + "How much apple? ", + "Total: $2.84" + )); + } + + private List<String> output(String input) { + ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes()); ByteArrayOutputStream out = new ByteArrayOutputStream(); new Runner(in, out).run(); return Arrays.asList(out.toString().split("\n"));