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
57 changes: 57 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## Domain Model for User Stories

```
1.
As a member of the public,
So I can order a bagel before work,
I'd like to add a specific type of bagel to my basket.
```

```
2.
As a member of the public,
So I can change my order,
I'd like to remove a bagel from my basket.
```

```
3.
As a member of the public,
So that I can not overfill my small bagel basket
I'd like to know when my basket is full when I try adding an item beyond my basket capacity.
```

```
4.
As a Bob's Bagels manager,
So that I can expand my business,
I’d like to change the capacity of baskets.
```

```
5.
As a member of the public
So that I can maintain my sanity
I'd like to know if I try to remove an item that doesn't exist in my basket.
```

The following class will be used in all the user stories.

| Class | Members | Types |
|----------|---------|------------------------|
| `Basket` | `items` | `Map<String, Integer>` |
| | `max` | `Integer` |


| User Story | Methods | Return | Notes |
|------------|---------------------------------------|-----------|---------------------------------------------|
| 1 | `addBagel(String type, int quantity)` | `void` | |
| 2 | `removeBagel(String type)` | `void` | |
| 3 | `hasCapacity()` | `boolean` | Max limit (5) |
| 4 | `changeBasketCapacity(int limit)` | `void` | |
| 5 | `removeBagel(String type)` | `boolean` | Updated method in story 2 to return boolean |





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

import java.util.HashMap;
import java.util.Map;

public class Basket {
public Map<String, Integer> items;
public int max = 5;

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

public void addBagel(String type, int quantity) {
this.items.put(type, quantity);
}

public boolean removeBagel(String key) {
if (items.get(key) != null) {
items.remove(key);
return true;
}
else {
return false;
}
}

public boolean hasCapacity() {
int totalItemsInBasket = 0;
for (Map.Entry<String, Integer> item : items.entrySet()) {
totalItemsInBasket += item.getValue();
}
return totalItemsInBasket < max;
}

public void changeBasketCapacity(int limit) {
this.max = limit;
}

}
50 changes: 50 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,54 @@

class BasketTest {

@Test
public void testAddBagelAddsBagelToBasket() {
Basket basket = new Basket();
basket.addBagel("normal", 1);

Assertions.assertFalse(basket.items.isEmpty());
}

@Test
public void testRemoveBagelRemovesBagelFromBasket() {
Basket basket = new Basket();
basket.addBagel("normal", 1);

// Then we remove the bagel.
basket.removeBagel("normal");

Assertions.assertTrue(basket.items.isEmpty());
}

@Test
public void testHasCapacity() {
Basket basket = new Basket();
basket.addBagel("normal", 1);
basket.addBagel("special", 2);

// Check if it returns true when not full.
boolean hasMoreSpace = basket.hasCapacity();
Assertions.assertTrue(hasMoreSpace);

// Check if it returns false when full.
basket.addBagel("tasteless", 3);
Assertions.assertFalse(basket.hasCapacity());
}

@Test
public void changeBasketCapacity() {
Basket basket = new Basket();
basket.changeBasketCapacity(10);

Assertions.assertEquals(10, basket.max);
}

@Test
public void testRemoveBagelFromBasketIndicatesIfItemExists() {
Basket basket = new Basket();
basket.addBagel("normal", 1);
boolean itemDoesNotExists = basket.removeBagel("special");

Assertions.assertFalse(itemDoesNotExists);
}
}