diff --git a/src/main/java/cz/cvut/eshop/shop/Order.java b/src/main/java/cz/cvut/eshop/shop/Order.java index 361789258669a8bae654eb17144a84543cb4d385..3f0d74d9966c0d1ef0d356c30874c906af9d2bce 100644 --- a/src/main/java/cz/cvut/eshop/shop/Order.java +++ b/src/main/java/cz/cvut/eshop/shop/Order.java @@ -88,7 +88,7 @@ public class Order { } public float getTotalAmount() { - int totalAmount = 0; + float totalAmount = 0; // spocitat vernostni body za polozky v nak. voziku for (Item item : items) { totalAmount += item.getPrice(); diff --git a/src/main/java/cz/cvut/eshop/shop/ShoppingCart.java b/src/main/java/cz/cvut/eshop/shop/ShoppingCart.java index 31fb0e6493795296b471673da5f34ee2da6bab13..f7ae6b19a47568b86f35a84e9f1d69b922d87656 100644 --- a/src/main/java/cz/cvut/eshop/shop/ShoppingCart.java +++ b/src/main/java/cz/cvut/eshop/shop/ShoppingCart.java @@ -42,7 +42,7 @@ public class ShoppingCart { */ public void removeItem(int itemID) { - for (int i = items.size() - 1; i <= 0; i--) { + for (int i = 0; i < items.size(); i++) { Item temp_item = (Item) items.get(i); if (temp_item.getID() == itemID) { items.remove(i); @@ -61,9 +61,9 @@ public class ShoppingCart { * * @return total price with discount */ - public int getTotalPrice() { - int total = 0; - for (int i = items.size() - 1; i <= 0; i--) { + public float getTotalPrice() { + float total = 0f; + for (int i = 0; i < items.size(); i++) { Item temp_item = (Item) items.get(i); total += temp_item.getPrice(); } diff --git a/src/test/java/cz/cvut/eshop/shop/OrderTest.java b/src/test/java/cz/cvut/eshop/shop/OrderTest.java new file mode 100644 index 0000000000000000000000000000000000000000..26cffda2066affc0a154c9864584c37895befc31 --- /dev/null +++ b/src/test/java/cz/cvut/eshop/shop/OrderTest.java @@ -0,0 +1,49 @@ +package cz.cvut.eshop.shop; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class OrderTest { + + ShoppingCart shoppingCart = new ShoppingCart(); + Order order; + Customer customer = new Customer(0); + @Before + public void fillShoppingCartAndCreateOrderAndAddCustomer () { + Item item = new StandardItem(1, "bread", 4.50f, "food", 3); + Item item2 = new StandardItem(2, "apple", 2.50f, "food", 5); + shoppingCart.addItem(item); + shoppingCart.addItem(item2); + + order = new Order(shoppingCart); + + order.setCustomer(customer); + } + + @Test + public void create_orderCreatedFromShoppingCardBefore_shouldSetLoyaltyPointToCustomer() { + + order.create(); + + assertEquals(8, customer.getLoyaltyPoints()); + } + + @Test + public void applyDiscount_usingSC() { + order.create(); + order.applyDiscount(); + + float discount = (float) (0.2 * 8); + float price = 7 - discount; + + assertEquals(price, order.getTotalAmount(), 0.05f); + + } + + @Test + public void getTotalAmount_usingSameSC_shouldBe7() { + assertEquals(7f, order.getTotalAmount(), 0.05f); + } +} \ No newline at end of file diff --git a/src/test/java/cz/cvut/eshop/shop/ShoppingCartTest.java b/src/test/java/cz/cvut/eshop/shop/ShoppingCartTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8b5559c5310bba174f35781c8fcdd513fce256e4 --- /dev/null +++ b/src/test/java/cz/cvut/eshop/shop/ShoppingCartTest.java @@ -0,0 +1,62 @@ +package cz.cvut.eshop.shop; + +import org.junit.Test; + +import java.util.Iterator; + +import static org.junit.Assert.*; + +public class ShoppingCartTest { + + @Test + public void getCartItems_insertNewAndGet_shouldReturnInsertedOne() { + ShoppingCart shCart = new ShoppingCart(); + Item item = new StandardItem(1, "bread", 4.50f, "food", 3); + shCart.addItem(item); + + assertEquals(1, shCart.getCartItems().size()); + + Iterator<Item> it = shCart.getCartItems().iterator(); + assertEquals(item.getID(), it.next().getID()); + assertFalse(it.hasNext()); + + } + + @Test + public void removeItem_insertAndRemove_itemsSizeShouldBeZero() { + ShoppingCart shCart = new ShoppingCart(); + Item item = new StandardItem(1, "bread", 4.50f, "food", 3); + shCart.addItem(item); + assertEquals(1, shCart.getCartItems().size()); + + shCart.removeItem(item.getID()); + assertEquals(0, shCart.getCartItems().size()); + + } + + @Test + public void getItemsCount_insertTwoItems_shouldBeTwo() { + ShoppingCart shCart = new ShoppingCart(); + Item item = new StandardItem(1, "bread", 4.50f, "food", 3); + shCart.addItem(item); + shCart.addItem(item); + + assertEquals(2, shCart.getItemsCount()); + } + + @Test + public void getTotalPrice_insertOneItem_shouldBeSameAsItemPrice() { + ShoppingCart shCart = new ShoppingCart(); + Item item = new StandardItem(1, "bread", 4.50f, "food", 3); + shCart.addItem(item); + + assertEquals(item.getPrice(), shCart.getTotalPrice(),0.05f); + } + + @Test + public void getTotalPrice_insertNone_shouldBeZero() { + ShoppingCart shCart = new ShoppingCart(); + + assertEquals(0f, shCart.getTotalPrice(),0.05f); + } +} \ No newline at end of file diff --git a/src/test/java/cz/cvut/eshop/shop/StandardItemTest.java b/src/test/java/cz/cvut/eshop/shop/StandardItemTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7fcf7d315edcdae4cc1f5d24911d84576a443d23 --- /dev/null +++ b/src/test/java/cz/cvut/eshop/shop/StandardItemTest.java @@ -0,0 +1,16 @@ +package cz.cvut.eshop.shop; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class StandardItemTest { + + @Test + public void copy_copyItem_shouldReturnSameItem() { + StandardItem item = new StandardItem(1, "Name", 9.68f, "Vegetable", 5); + StandardItem copyItem = item.copy(); + + assertEquals(item, copyItem); + } +} \ No newline at end of file