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
69 changes: 69 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,74 @@
package com.booleanuk.core;


import java.util.HashMap;

public class Basket {
// TODO: Difference between ArrayList and simple List??
// TODO: Should this be private variables?
HashMap<String, Integer> products;
int maxCapacity;
int size = 0;

public Basket() {
this.products = new HashMap<>();
this.maxCapacity = 3;
}

public boolean add(String productName) {
// Check if max capacity
if (this.size >= maxCapacity) {
System.out.println("Basket is full, can't add more items.");
return false;
}

// List properties
int numOfProducts;

// Update numOfProducts if already exist, otherwise just add
if (products.containsKey(productName)) {
numOfProducts = products.get(productName) + 1;
} else {
numOfProducts = 1;
}

// Add product
products.put(productName, numOfProducts);

// Update size
size++;

return true;
}

public boolean remove(String productName) {

// Check if product does not exist
if (!this.products.containsKey(productName)) {
System.out.println("Product does not exist. Can't remove.");
return false;
}

// Update quantity if already in list, remove if it is the last product in this type/category
int newNumOfProducts = this.products.get(productName) - 1;
if (newNumOfProducts == 0) {
this.products.remove(productName);
} else {
this.products.put(productName, newNumOfProducts);
}

// Update size
size--;

return true;
}

public boolean changeCapacity(int newMaxCapacity) {
if (newMaxCapacity < 1) {
System.out.println("Invalid max capacity. Can't be less than 1");
return false;
}
this.maxCapacity = newMaxCapacity;
return true;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/booleanuk/core/Domain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
| Classes | Variables | Methods | Scenario | Outcome |
|--------------------------------|---------------------------------------|-----------------------------------|-----------------------------------------------|-----------------------------------|
| `Basket` | `HashMap<String, Integer> products` | `add(String productName)` | Item succesfully added. | true |
| | | | Basket is full. Item could not be added. | false, Error: Basket is full |
| | | `remove(String productName)` | Item is in basket and can be removed. | true |
| | | | Item is not in basket and can not be removed. | false, Error: Item does not exist |
| | `int maxCapacity` | `changeCapacity(int newCapacity)` | If newCapacity is more than 0. | true |
| | | | If newCapacity is less than 1. | false |

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

import java.util.ArrayList;
import java.util.HashMap;

class BasketTest {

// User story 1.
@Test
public void add() {
Basket basket = new Basket();
Assertions.assertTrue(basket.add("Unicorn Bagel"));
Assertions.assertEquals(1, basket.size);

basket.add("Unicorn Bagel");
Assertions.assertEquals(2, basket.products.get("Unicorn Bagel")); // Index 0 is numOfProducts

basket.add("Sunhaven Bagel");
Assertions.assertFalse(basket.add("Moon Bagel")); // Exceed maxCapacity
}

@Test
public void remove() {
Basket basket = new Basket();
basket.add("Small Bagel");
basket.add("Small Bagel");

// Remove first item, 1 left
Assertions.assertTrue(basket.remove("Small Bagel"));
Assertions.assertEquals(1, basket.products.get("Small Bagel"));

// Remove second item, 0 left (should be removed from list)
Assertions.assertTrue(basket.remove("Small Bagel"));
Assertions.assertFalse(basket.products.containsKey("Small Bagel"));

// Remove not existing item
Assertions.assertFalse(basket.remove("Small Bagel"));
}

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

// Change to valid max capacity
Assertions.assertTrue(basket.changeCapacity(5));
Assertions.assertEquals(5, basket.maxCapacity);

// Change to invalid max capacity
Assertions.assertFalse(basket.changeCapacity(0));
Assertions.assertFalse(basket.changeCapacity(-1));
}
}