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
18 changes: 18 additions & 0 deletions EXERCISE1.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,30 @@ So that I can pay for products at checkout,
I'd like to be able to know the total cost of items in my basket.
```

| Classes | Methods | Scenario | Result |
|----------|----------------------------------|------------------------|---------------------------|
| `Basket` | `addItem(String item, int cost)` | If basket isn't full | Adds item to basket |
| | | If basket is full | Cant add item to basket |
| | | | |
| | `calculateCost()` | If basket isn't empty | Returns cost of all items |
| | | If basket is empty | Return 0 |
| | | | |



```
As an organised individual,
So that I can evaluate my shopping habits,
I'd like to see an itemised receipt that includes the name and price of the products
I bought as well as the quantity, and a total cost of my basket.
```

| Classes | Methods | Scenario | Result |
|------------|-----------------------------|------------------------------|-----------------------------|
| `Receipt` | `getReceipt(Basket basket)` | Get a receipt after shopping | Returns info about the trip |
| | | | |
| | | | |


- Add your domain models to this repository as a file named `domain-model`. This should either be a `.md` file like this one, or a screenshot / picture of your work.
- Your model doesn't have to look like the example provided in this file. If you feel like you need more or less columns, feel free to go with that. There is no "right way" to do this kind of thing, we're just designing a system to make our lives easier when it comes to the coding part.
27 changes: 27 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.booleanuk.core;

import java.util.HashMap;

public class Basket {

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

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

public int total(){
int totalCost = 0;

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

return totalCost;
}

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

import java.lang.reflect.Array;
import java.util.ArrayList;

public class CohortManager {

public boolean search(ArrayList<String> cohorts, String name){
return cohorts.contains(name);
}

}
45 changes: 45 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.booleanuk.core;

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

public class BasketTest {

@Test
public void addProductNotInBasket(){
// Setup
Basket basket = new Basket();
String productName = "crisps";
int price = 30;

// Execute and verify
Assertions.assertTrue(basket.add(productName, price));
}

@Test
public void addProductAlreadyInBasket(){
// Setup
Basket basket = new Basket();
String productName = "crisps";
int price = 30;

basket.add(productName, price);

// Execute and verify
Assertions.assertFalse(basket.add(productName, price));
}

@Test
public void totalCostBasket(){
Basket basket = new Basket();

basket.add("crisps", 30);
basket.add("fanta", 25);
basket.add("haribo", 30);

int totalCost = basket.total();

Assertions.assertEquals(85, totalCost);
}

}
34 changes: 34 additions & 0 deletions src/test/java/com/booleanuk/core/CohortManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

class CohortManagerTest {

@Test
public void searchForCohortsThatExists(){
// Setup
CohortManager cohortManager = new CohortManager();
ArrayList<String> cohorts = new ArrayList<>(){{
add("Liverpool");
add("Tottenham");
add("Arsenal");
add("Newcastle");
}};
String name = "Liverpool";

// Execute and verify
Assertions.assertTrue(cohortManager.search(cohorts, name));
}

@Test
public void searchForCohortsThatDoesNotExists(){
// Setup
CohortManager cohortManager = new CohortManager();
ArrayList<String> cohorts = new ArrayList<>(){{
add("Liverpool");
add("Tottenham");
add("Arsenal");
add("Newcastle");
}};
String name = "Burnley";

// Execute and verify
Assertions.assertFalse(cohortManager.search(cohorts, name));
}

}
Loading