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
29 changes: 29 additions & 0 deletions DomainModel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Domain Model

## CohortManager Class

| Method | Variables | Scenario | Output |
|---|---|---|---|
| | ArrayList\<String> cohorts | | |
| search(String name) | | Found name in cohorts | return true |
| | | Did not find name in cohorts | return false |

## ShoppingCart Class

| Method | Variables | Scenario | Output |
|---|---|---|---|
| | ArrayList\<ShopItem> cart | | |
| getTotalCost() | | valid total cost | return total cost of items in basket |
| | | invalid total cost | return -1 |
| getReceipt() | | valid list of items | return receipt String |
| | | invalid list of items | return null |
| addItem(ShopItem item) | | valid item | add item to cart |
| | | invalid item | throw error|

## ShopItem Class
| Method | Variables | Scenario | Output |
|---|---|---|---|
| | String name | | |
| | Float price | | |
| getPrice() | | | return price |

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

import java.util.HashMap;

public class Basket {
private HashMap<String, Integer> items;

public Basket() {
items = new HashMap<>();
}

boolean add(String product, int price) {
if (items.containsKey(product))
return false;

items.put(product, price);
return true;
}

int total() {
int total = 0;

for (Integer price : items.values()) {
total += price;
}

return total;
}
}
12 changes: 11 additions & 1 deletion src/main/java/com/booleanuk/core/CohortManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class CohortManager {
private ArrayList<String> cohorts;

public CohortManager(ArrayList<String> cohorts) {
this.cohorts = cohorts;
}

}
boolean search(String name) {
return cohorts.contains(name);
}
}
44 changes: 44 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.booleanuk.core;

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

public class BasketTest {
@Test
void testAddNonExistingItem() {
Basket basket = new Basket();
String itemName = "orange";
int price = 5;

Assertions.assertTrue(basket.add(itemName, price));
}

@Test
void testAddExistingItem() {
Basket basket = new Basket();
String itemName = "orange";
int price = 5;

basket.add(itemName, price);

Assertions.assertFalse(basket.add(itemName, price));
}

@Test
void testTotalEmptyBasket() {
Basket basket = new Basket();

Assertions.assertEquals(0, basket.total());
}

@Test
void testTotal() {
Basket basket = new Basket();
basket.add("a", 5);
basket.add("b",35);
basket.add("c", 100);
basket.add("d", 70);

Assertions.assertEquals(210, basket.total());
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/booleanuk/core/CohortManagerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
package com.booleanuk.core;

import java.util.ArrayList;

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

class CohortManagerTest {
@Test
public void searchForCohortThatExists() {
// setup
ArrayList<String> cohorts = new ArrayList<>(){{
add("Robin");
add("Mathias");
add("Magnus");
add("Jonas");
add("Thomas");
}};
CohortManager manager = new CohortManager(cohorts);
String name = "Robin";

// execute and verify
Assertions.assertTrue(manager.search(name));
}

@Test
public void searchForCohortThatDoesNotExist() {
// setup
ArrayList<String> cohorts = new ArrayList<>(){{
add("Robin");
add("Mathias");
add("Magnus");
add("Jonas");
add("Thomas");
}};
CohortManager manager = new CohortManager(cohorts);
String name = "Andreas";

// execute and verify
Assertions.assertFalse(manager.search(name));
}
}
Loading