From 681a7a7f59450663178513c016bef93a3a63b373 Mon Sep 17 00:00:00 2001 From: Maxim Volkov Date: Thu, 17 Feb 2022 22:08:36 +0100 Subject: [PATCH] Added: Get Count Of Stock + Basic Test --- .../de/hs_fulda/ciip/projjpn/Warehouse.java | 10 +++++++--- .../hs_fulda/ciip/projjpn/WarehouseTest.java | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/projjpn/src/main/java/de/hs_fulda/ciip/projjpn/Warehouse.java b/projjpn/src/main/java/de/hs_fulda/ciip/projjpn/Warehouse.java index 3a13f09..6b6a867 100644 --- a/projjpn/src/main/java/de/hs_fulda/ciip/projjpn/Warehouse.java +++ b/projjpn/src/main/java/de/hs_fulda/ciip/projjpn/Warehouse.java @@ -4,12 +4,16 @@ import java.util.HashMap; public class Warehouse { protected HashMap pool = new HashMap(); - - - public Item insertItem(Item item) { return pool.putIfAbsent(item.getTitel(), item); } + public int getCountOfStock() { + int sumItems = 0; + for (HashMap.Entry set : pool.entrySet()) { + sumItems += set.getValue().getCurrentStock(); + } + return sumItems; + } } \ No newline at end of file diff --git a/projjpn/src/test/java/de/hs_fulda/ciip/projjpn/WarehouseTest.java b/projjpn/src/test/java/de/hs_fulda/ciip/projjpn/WarehouseTest.java index ef10f59..f713f83 100644 --- a/projjpn/src/test/java/de/hs_fulda/ciip/projjpn/WarehouseTest.java +++ b/projjpn/src/test/java/de/hs_fulda/ciip/projjpn/WarehouseTest.java @@ -28,4 +28,22 @@ public class WarehouseTest extends TestCase { // Then assertEquals(expectedTitel, gotItem.getTitel()); } + + + public void test_growingCountOfItemsInWarehouse() { + // Given + Warehouse warehouse = new Warehouse(); + int unitsPerItemType = 3; + int expectedSize = 13; + for(int i = 0; i < expectedSize; i++) { + Item item = new Item("ItemDummy" + i, "DescriptionDummy" + i, unitsPerItemType, 12.0f); + warehouse.insertItem(item); + } + int expectedSum = expectedSize * unitsPerItemType; + int actualSumOfAllItems = warehouse.getCountOfStock(); + + // Then + assertEquals(expectedSum, actualSumOfAllItems); + + } }