Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
28bc57a
I missed that i should commit for every test when i did the testCreat…
OliverLundhAndersson Aug 14, 2024
74e962e
Passing tests for addBagel()
OliverLundhAndersson Aug 14, 2024
4460ddf
started testRemoveFromBasket
OliverLundhAndersson Aug 14, 2024
c645630
continuing testRemoveFromBasket
OliverLundhAndersson Aug 14, 2024
e8f64d7
continuing testRemoveFromBasket
OliverLundhAndersson Aug 14, 2024
ab70cd8
continuing testRemoveFromBasket
OliverLundhAndersson Aug 14, 2024
49e1397
continuing testRemoveFromBasket
OliverLundhAndersson Aug 14, 2024
29e6caa
completed testRemoveFromBasket, for now
OliverLundhAndersson Aug 14, 2024
aa50962
started testSetCapacity
OliverLundhAndersson Aug 14, 2024
d8523c4
continue testSetCapacity
OliverLundhAndersson Aug 14, 2024
24fdb3e
continue testSetCapacity
OliverLundhAndersson Aug 14, 2024
23a4b41
continue testSetCapacity
OliverLundhAndersson Aug 14, 2024
0062a7a
continue testSetCapacity
OliverLundhAndersson Aug 14, 2024
9cfc063
continue testSetCapacity
OliverLundhAndersson Aug 14, 2024
ba69f26
More removeBagel tests
OliverLundhAndersson Aug 14, 2024
29a1580
Adding requirement to be Bob to tests
OliverLundhAndersson Aug 14, 2024
89fd5ae
Adding requirement to be Bob to tests, passing
OliverLundhAndersson Aug 14, 2024
75f5dd5
Adding requirement to be Bob to tests, failing
OliverLundhAndersson Aug 14, 2024
05e81af
Adding requirement to be Bob to tests. You now need to be Bob
OliverLundhAndersson Aug 14, 2024
4ee046d
Comments on tests
OliverLundhAndersson Aug 14, 2024
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_MODEL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
| Classes | Variables | Methods | Scenario | Outcome |
|----------|----------------------------|------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|
| 'Basket' | 'ArrayList<String> basket' | 'addBagel(String type)' | Add bagel to not full basket. | Return 'true' |
| | | | Tried to add bagel to full basket. | Return 'false' and print "Basket is full" |
| | | 'removeBagel(String type)' | Remove bagel when bagel with 'type' exists and basket is not empty | Return 'true' |
| | | | Tried to remove bagel with 'type' that do not exist in basket | Return 'false' and print "No such bagel exists in your basket" |
| | | | Tried to remove bagel from empty basket | Return 'false' and print "Basket is empty" |
| | 'int capacity' | 'setCapacity(int capacity, boolean isManager)' | Tried to set the basket capacity to a number smaller than the number of bagels the basket is currently holding. | Return 'false' and print "Basket contains to many bagels. Please empty the basket first" |
| | | | Set the capacity to a non-problematic number | Return 'true' |
| | | | Set capacity to a non-positive number. | Return 'false' and print "Capacity must be a positive number" |
| | | | Tried to set capacity without being Bob | Return 'false' and print "You're not Bob" |
54 changes: 54 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class Basket {
public int capacity;
public ArrayList<String> basket;

public Basket(int capacity) {
this.basket = new ArrayList<>();
if (capacity > 0) {
this.capacity = capacity;
} else {
this.capacity = 1;
}
}

public boolean addBagel(String type){
if (this.basket.size() < this.capacity) {
this.basket.add(type);
return true;
}
System.out.println("Basket is full");
return false;
}

public boolean removeBagel(String type) {
if (this.basket.isEmpty()) {
System.out.println("Basket is empty");
return false;
} else if (this.basket.contains(type)) {
// Remove only one of type if multiple exists
for (int i = 0; i < this.basket.size(); i++) {
if (this.basket.get(i).equals(type)) {
this.basket.remove(i);
return true;
}
}
}
System.out.println("No such bagel exists in your basket");
return false;
}

public boolean setCapacity(int capacity, boolean isBob) {
if (!isBob) {
System.out.println("You're not Bob");
return false;
}
if (capacity < 1) {
System.out.println("Capacity must be a positive number");
return false;
} else if (capacity < basket.size()) {
System.out.println("Basket contains to many bagels. Please empty the basket first");
return false;
}
this.capacity = capacity;
return true;
}
}
81 changes: 80 additions & 1 deletion src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,87 @@
package com.booleanuk.core;

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

import static org.junit.jupiter.api.Assertions.*;

class BasketTest {

@Test
public void testCreateBasket() {
// Test so that you can only initiate a basket with a positive number.
Basket basket1 = new Basket(1);
Basket basketM1 = new Basket(-1);
Basket basket5 = new Basket(5);

assertEquals(1, basket1.capacity);
assertEquals(1, basketM1.capacity);
assertEquals(5, basket5.capacity);
}

@Test
public void testAddToBasket() {
// Test if you can add more bagels to basket than the capacity
Basket basket = new Basket(2);

boolean b1 = basket.addBagel("1");
boolean b2 = basket.addBagel("2");
boolean b3 = basket.addBagel("3");

assertTrue(b1);
assertTrue(b2);
assertFalse(b3);
}


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

// Test removal from empty basket
boolean b1 = basket.removeBagel("1");
assertFalse(b1);

// Test removal of the same bagel twice
basket.addBagel("1");
boolean b2 = basket.removeBagel("1");
boolean b3 = basket.removeBagel("1");

assertTrue(b2);
assertFalse(b3);

// Test removal of non-existent bagel
basket.addBagel("1");
boolean b4 = basket.removeBagel("2");
assertFalse(b4);

// Test that only one bagel is removed at a time
basket.addBagel("1");
basket.addBagel("1");
boolean b5 = basket.removeBagel("1");
boolean b6 = basket.removeBagel("1");
assertTrue(b5);
assertTrue(b6);
}

@Test
public void testSetCapacity() {
// Test that capacity changes
Basket basket = new Basket(2);
basket.setCapacity(3, true);
assertEquals(3, basket.capacity);

// Test that capacity needs to be positive
boolean b1 = basket.setCapacity(-1, true);
assertFalse(b1);

// Test that capacity cant be lower than the current amount of bagels in basket
basket.addBagel("1");
basket.addBagel("2");
boolean b2 = basket.setCapacity(1, true);
assertFalse(b2);

// Test that you need to be Bob
boolean b3 = basket.setCapacity(5, false);
assertFalse(b3);
}
}