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

public class Basket {
public HashMap<String, Integer> basketList;
public int totalCost;

public Basket() {
this.basketList = new HashMap<>();
this.totalCost = 0;
}

public void addProduct(String product, int price) {
basketList.put(product, price);
}

public int calculateTotalCostInBasket() {
for (Map.Entry<String, Integer> entry : this.basketList.entrySet()) {
this.totalCost += entry.getValue();
}
return this.totalCost;
}
}
27 changes: 27 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.booleanuk.core;

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

public class BasketTest {

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

int beforeAddedItem = basket.basketList.size();
basket.addProduct("Product 1", 10);
int afterAddedItem = basket.basketList.size();

Assertions.assertTrue(beforeAddedItem < afterAddedItem);
}

@Test
public void testCalculatingTotalCostInBasket() {
Basket basket = new Basket();
basket.addProduct("Product 1", 10);

int totalCost = basket.calculateTotalCostInBasket();
Assertions.assertEquals(10, totalCost);
}
}