Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -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<Product> 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 |
19 changes: 19 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.booleanuk.core;

import java.util.HashMap;

public class Basket {
public HashMap<String, Integer> 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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/booleanuk/core/Product.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
20 changes: 20 additions & 0 deletions src/test/java/com/booleanuk/core/ProductTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}