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
28 changes: 28 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

| Methods | Members | Scenario | Result |
|--------------------------------------|---------|------------------------|----------------------------------------------|
| `calculate(List<Integer> itemCosts)` | | No item prices in list | total = 0 |
| | | n items prices in list | total = sum of n item prices |
| `addItem(List<Integer> itemCosts, int newItemCost)` | | Valid itemCost | Item price gets added at the end of the list |
| | | Invalid itemCost | Item doesn't get added + error message |







| Methods | Members | Scenario | Result |
|-------------------------------------------------|------------------------------|------------------------------------------------------------------------------|-------------------------------------|
| `getNameAndPrice(HashMap<String, Integer> map)` | Hashmap<String, Integer> map | Valid key-value pairs | return the name and price correctly |
| | int quantity | Missing keys or value | return error |
| `getQuantity()` | | Valid quantity (not -1) | return quantity |
| | | Invalid quantity | return error |
| `calculateTotal(String itemName, int quantity)` | | itemName exists in the hashmap as a key and a valid value, quantity is valid | return the total price |
| | | Missing or invalid paramets | return error |






24 changes: 24 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.booleanuk.core;

import java.util.HashMap;

public class Basket {

HashMap<String, Integer> items = new HashMap<>();
int total = 0;

public boolean add(String product, int price) {
if (!items.containsKey(product)) {
items.put(product, price);
return true;
}
return false;
}

public int total() {
for (String key : items.keySet()) {
total += items.get(key);
}
return this.total;
}
}
38 changes: 38 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class BasketTest {

@Test
public void itemIsNotAlreadyInBasket() {
Basket basket = new Basket();
basket.items.put("Apple", 1);
basket.items.put("Banana", 20);
Assertions.assertTrue(basket.add("TestItem1", 1));
}

@Test
public void itemIsAlreadyInBasket() {
Basket basket = new Basket();
basket.items.put("Apple", 1);
basket.items.put("Banana", 20);
basket.items.put("TestItem2", 1);
Assertions.assertFalse(basket.add("TestItem2", 1));
}

@Test
public void totalShouldBeZero() {
Basket basket = new Basket() {};
Assertions.assertEquals(0, basket.total());
}

@Test
public void totalShouldIncrease() {
Basket basket = new Basket() {};
basket.add("Banana", 20);
Assertions.assertEquals(20, basket.total());
}

}
Loading