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
11 changes: 11 additions & 0 deletions domain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
| `Classes` | `Member` | `Methods` | `Scenario` | `Outcome` |
|-----------|-----------------------------------|---------------------------------|--------------------------|-------------------------------------------------|
| `Basket` | `Hashmap<String, Integer> bagels` | `add(String bagel, int num)` | Bagel-key already exists | Return true, Bagel number is incremented by num |
| | `int capacity` | | Bagel-key does not exist | Return true, Bagel key is created with value |
| | | | Basket is full | Return false
| | | `remove(String bagel, int num)` | Bagel-key exists | Return true, Bagel value is decremented by num |
| | | | Bagel-key does not exist | Return false |
| | | `checkCapacity()` | Basket is full | Return false |
| | | | Basket is not full | Return true |
| | | `incCapacity(int num)` | Capacity value exists | Capacity increases by num |

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

public class Basket {
HashMap<String, Integer> bagels = new HashMap<String, Integer>();
int capacity;

public Basket(int num){
capacity=num;
}

public boolean add(String bagel, int num){
if (!(bagels.containsKey(bagel) & checkCapacity())){
bagels.put(bagel, 0);
}

for (int i=0; i<num; i++){
if (checkCapacity()) {
bagels.replace(bagel, bagels.get(bagel) + 1);

}
else {
System.out.println("The basket is full");
return false;
}
}
return true;



}

public boolean remove(String bagel, int num){
if (bagels.containsKey(bagel)){
if (bagels.get(bagel)==0){
System.out.println("The basket does not contain this bagle type");
return false;
}

bagels.replace(bagel, bagels.get(bagel)-num);
return true;
}
else{
System.out.println("The basket does not contain this bagle type");
return false;
}
}

public boolean checkCapacity(){
int num=0;
for (String key: bagels.keySet()){
num+=bagels.get(key);

if (num>=capacity){
return false;
}
}
return true;

}

public boolean incCapacity(int num){
capacity+=num;
return true;
}
}
74 changes: 74 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,79 @@
import org.junit.jupiter.api.Test;

class BasketTest {
public BasketTest(){

}

@Test
public void testAddExists(){
Basket basket= new Basket(10);
Assertions.assertTrue(basket.add("Nice bagel", 5));
}

@Test
public void testRemoveExists(){
Basket basket= new Basket(10);
Assertions.assertFalse(basket.remove("Nice bagel", 5));
}


@Test
public void testCapacityExists(){
Basket basket= new Basket(10);
Assertions.assertTrue(basket.checkCapacity());
}


@Test
public void testIncCapacityExists(){
Basket basket= new Basket(10);
Assertions.assertTrue(basket.incCapacity(10));
}

// testing User story 1
@Test
public void testAdd(){
Basket basket= new Basket(10);
Assertions.assertTrue(basket.add("Nice bagel", 5));
Assertions.assertTrue(basket.add("Nice bagel", 5));
}

// testing User story 2 & 5
@Test
public void testRemove(){
Basket basket= new Basket(10);
basket.add("Nice bagel", 5);
Assertions.assertTrue(basket.remove("Nice bagel", 1));
Assertions.assertFalse(basket.remove("Fine bagel", 1));
}

// testing User story 3
@Test
public void testAddingtoFull(){
Basket basket= new Basket(10);
basket.add("Nice bagel", 9);
Assertions.assertFalse(basket.add("Nice bagel", 2));
}

// testing User story 3
@Test
public void testCheckCapacity(){
Basket basket= new Basket(10);
basket.add("Nice bagel", 5);
basket.add("Nice bagel", 5);
Assertions.assertFalse(basket.checkCapacity());
}

// testing User story 4
@Test
public void testIncCapacity(){
Basket basket= new Basket(10);
Assertions.assertTrue(basket.incCapacity(5));
}





}