diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..0c5aa0a --- /dev/null +++ b/domain-model.md @@ -0,0 +1,22 @@ +## Domain model Product class +| Variables | Methods | Scenario | Outputs | +|----------------|---------------------|--------------------|---------------------------------| +| int fullPrice | | | | +| float discount | | | | +| String name | | | | +| | `int getCost()` | Method gets called | positive integer or 0 | +| | `String toString()` | Method gets called | Detailed information about item | + +## Domain model CheckoutMachine class: +| Variables | Methods | Scenario | Output | +|------------------------|--------------------------------------------------|-----------------------|--------------------------------------------------| +| `List basket` | | | | +| | `void addProduct(Product product)` | User adds product | Adds a single product to the basket | +| | `void addProduct(Product product, int count)` | User adds products | Adds how ever many products specified in call | +| | | | | +| | `void removeProduct(Product product)` | User removes product | Removes a single product from the basket | +| | `void removeProduct(Product product, int count)` | User removes products | Removes how ever many products specified in call | +| | | | | +| | `boolean processPayment()` | Payment failed | false, display some error message | +| | | Payment succeeded | true, display some error message | +| | `private void printReceipt()` | Method gets called | Prints receipt to customer | diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java new file mode 100644 index 0000000..aaaa73c --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,19 @@ +package com.booleanuk.core; + +import java.util.HashMap; + +public class Basket { + public HashMap items = new HashMap<>(); + + public void add(String product, int price) { + this.items.put(product, price); + } + + public float total() { + int price = 0; + for (int p : this.items.values()) + price += p; + + return price; + } +} diff --git a/src/main/java/com/booleanuk/core/Product.java b/src/main/java/com/booleanuk/core/Product.java new file mode 100644 index 0000000..030de6f --- /dev/null +++ b/src/main/java/com/booleanuk/core/Product.java @@ -0,0 +1,17 @@ +package com.booleanuk.core; + +public class Product { + public String name; + public int fullPriceInNOK; + public float discount; // 0..1, 0 means no discount 1 means 100% + + public Product(String name, int fullPriceInNOK, float discount) { + this.name = name; + this.fullPriceInNOK = fullPriceInNOK; + this.discount = discount; + } + + public float price() { + return this.fullPriceInNOK * (1 - discount); + } +} diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java new file mode 100644 index 0000000..3b78638 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,26 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class BasketTest { + @Test + public void testAddProductToBasket() { + Basket basket = new Basket(); + basket.add("Bread", 29); + Assertions.assertEquals(1, basket.items.size()); + } + + @Test + public void testCalculateTotal() { + Basket basket = new Basket(); + basket.add("Bread", 29); + basket.add("Soda", 20); + + Assertions.assertEquals(2, basket.items.size()); + Assertions.assertEquals(29 + 20, basket.total()); + + basket.add("Milk", 25); + } + +} diff --git a/src/test/java/com/booleanuk/core/ProductTest.java b/src/test/java/com/booleanuk/core/ProductTest.java new file mode 100644 index 0000000..d34a496 --- /dev/null +++ b/src/test/java/com/booleanuk/core/ProductTest.java @@ -0,0 +1,20 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class ProductTest { + @Test + public void shouldInstantiateCorrectly() { + Product product = new Product("Bread", 29, 0.0f); + Assertions.assertEquals("Bread", product.name); + Assertions.assertEquals(29, product.fullPriceInNOK); + Assertions.assertEquals(0.0f, product.discount); + } + + @Test + public void shouldCalculatePrice() { + Product product = new Product("Bread", 29, 0.0f); + Assertions.assertEquals(29, product.price()); + } +}