diff --git a/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java b/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java index 8bb8d9f50f528a97b5b3eb4a0876ef552e97ec4c..c31c7e936562a8bb9c1dd02ee5892d991da26eeb 100644 --- a/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java +++ b/src/main/java/cz/cvut/eshop/archive/PurchasesArchive.java @@ -34,10 +34,7 @@ public class PurchasesArchive { } return 0; } - - - public void putOrderToPurchasesArchive(Order order) { orderArchive.add(order); ArrayList<Item> orderItems = order.getItems(); @@ -50,6 +47,13 @@ public class PurchasesArchive { } } } - + + public HashMap<Integer, ItemPurchaseArchiveEntry> getItemPurchaseArchive() { + return itemPurchaseArchive; + } + + public ArrayList<Order> getOrderArchive() { + return orderArchive; + } } diff --git a/src/test/java/cz/cvut/eshop/archive/ItemPurchaseArchiveEntryTest.java b/src/test/java/cz/cvut/eshop/archive/ItemPurchaseArchiveEntryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f0bdeeaae717fd1d9f69934351466912478422ee --- /dev/null +++ b/src/test/java/cz/cvut/eshop/archive/ItemPurchaseArchiveEntryTest.java @@ -0,0 +1,35 @@ +package cz.cvut.eshop.archive; + +import cz.cvut.eshop.shop.Item; +import cz.cvut.eshop.shop.StandardItem; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ItemPurchaseArchiveEntryTest { + @Test + public void increaseCountHowManyTimesHasBeenSold_increaseSoldCounterBy2_shouldReturn3() { + Item item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ItemPurchaseArchiveEntry itemArchive = new ItemPurchaseArchiveEntry(item); + itemArchive.increaseCountHowManyTimesHasBeenSold(2); + + assertEquals(3, itemArchive.getCountHowManyTimesHasBeenSold()); + } + + @Test + public void getRefItem_gettingThisItemFromArchive_shouldReturnCorrectItem() { + Item item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ItemPurchaseArchiveEntry itemArchive = new ItemPurchaseArchiveEntry(item); + Item correctItem = itemArchive.getRefItem(); + + assertEquals(item, correctItem); + } + + @Test + public void toString_callThisItemToString_shouldReturnCorrectText() { + Item item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ItemPurchaseArchiveEntry itemArchive = new ItemPurchaseArchiveEntry(item); + + assertEquals("ITEM "+item.toString()+" HAS BEEN SOLD "+1+" TIMES", itemArchive.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/cz/cvut/eshop/archive/PurchasesArchiveTest.java b/src/test/java/cz/cvut/eshop/archive/PurchasesArchiveTest.java new file mode 100644 index 0000000000000000000000000000000000000000..5cbe64536660ae214b049f3c32db0d77ff687169 --- /dev/null +++ b/src/test/java/cz/cvut/eshop/archive/PurchasesArchiveTest.java @@ -0,0 +1,78 @@ +package cz.cvut.eshop.archive; + +import cz.cvut.eshop.shop.Order; +import cz.cvut.eshop.shop.ShoppingCart; +import cz.cvut.eshop.shop.StandardItem; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.*; + +public class PurchasesArchiveTest { + @Test + public void putOrderToPurchasesArchive_addOrderToArchive_shouldAddsOrderToOrderArchiveAndToPurchaseOrderArchive() { + StandardItem item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ShoppingCart cart = new ShoppingCart(); + cart.addItem(item); + Order order = new Order(cart); + + PurchasesArchive archive = new PurchasesArchive(); + archive.putOrderToPurchasesArchive(order); + + List<Order> list = new ArrayList(); + list.add(order); + Map<Integer, ItemPurchaseArchiveEntry> map = new HashMap(); + map.put(item.getID(), new ItemPurchaseArchiveEntry(item)); + + assertEquals(list, archive.getOrderArchive()); + assertEquals(map.size(), archive.getItemPurchaseArchive().size()); + assertEquals(map.get(item.getID()).toString(), archive.getItemPurchaseArchive().get(item.getID()).toString()); + } + + @Test + public void putOrderToPurchasesArchive_addSameItemToOrderTwice_shouldAddItToSameHashmap() { + StandardItem item = new StandardItem(1, "pen", 10, "writing accessories", 1); + ShoppingCart cart = new ShoppingCart(); + cart.addItem(item); + cart.addItem(item); + Order order = new Order(cart); + + PurchasesArchive archive = new PurchasesArchive(); + archive.putOrderToPurchasesArchive(order); + + ArrayList<Order> list = new ArrayList(); + list.add(order); + HashMap<Integer, ItemPurchaseArchiveEntry> map = new HashMap(); + ItemPurchaseArchiveEntry purchaseArchiveEntry = new ItemPurchaseArchiveEntry(item); + purchaseArchiveEntry.increaseCountHowManyTimesHasBeenSold(1); + map.put(item.getID(), purchaseArchiveEntry); + + assertEquals(list, archive.getOrderArchive()); + assertEquals(map.size(), archive.getItemPurchaseArchive().size()); + assertEquals(map.get(item.getID()).toString(), archive.getItemPurchaseArchive().get(item.getID()).toString()); + } + + @Test + public void getHowManyTimesHasBeenItemSold_addSameItemToTwoCarts_shouldReturnItemHasBeenSold2Times() { + StandardItem item1 = new StandardItem(1, "pen", 10, "writing accessories", 1); + StandardItem item2 = new StandardItem(2, "pencil", 5, "writing accessories", 0); + + ShoppingCart cart1 = new ShoppingCart(); + ShoppingCart cart2 = new ShoppingCart(); + cart1.addItem(item1); + cart2.addItem(item1); + cart2.addItem(item2); + Order order1 = new Order(cart1); + Order order2 = new Order(cart2); + + PurchasesArchive archive = new PurchasesArchive(); + archive.putOrderToPurchasesArchive(order1); + archive.putOrderToPurchasesArchive(order2); + + assertEquals(2, archive.getHowManyTimesHasBeenItemSold(item1)); + } +}