diff --git a/eShop.iml b/eShop.iml new file mode 100644 index 0000000000000000000000000000000000000000..309a6cd4ceba8fb9287d98069e57a69c86522cf0 --- /dev/null +++ b/eShop.iml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> + <output url="file://$MODULE_DIR$/target/classes" /> + <output-test url="file://$MODULE_DIR$/target/test-classes" /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> + <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> + <excludeFolder url="file://$MODULE_DIR$/target" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + <orderEntry type="library" name="Maven: org.assertj:assertj-core:3.10.0" level="project" /> + <orderEntry type="library" name="Maven: junit:junit:4.12" level="project" /> + <orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> + </component> +</module> \ No newline at end of file diff --git a/src/main/java/cz/cvut/eshop/shop/ShoppingCart.java b/src/main/java/cz/cvut/eshop/shop/ShoppingCart.java index 31fb0e6493795296b471673da5f34ee2da6bab13..c4af848ec3509927d64d9d84d99e27a0af813105 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 = items.size() - 1; i >= 0; i--) { Item temp_item = (Item) items.get(i); if (temp_item.getID() == itemID) { items.remove(i); @@ -63,7 +63,7 @@ public class ShoppingCart { */ public int getTotalPrice() { int total = 0; - for (int i = items.size() - 1; i <= 0; i--) { + for (int i = items.size() - 1; i >= 0; i--) { Item temp_item = (Item) items.get(i); total += temp_item.getPrice(); } diff --git a/src/test/java/shop/ShoppingCartTest.java b/src/test/java/shop/ShoppingCartTest.java new file mode 100644 index 0000000000000000000000000000000000000000..ce4027341d4c19473f2ce485f449b7ac86a083b8 --- /dev/null +++ b/src/test/java/shop/ShoppingCartTest.java @@ -0,0 +1,82 @@ +package shop; + +import cz.cvut.eshop.shop.Item; +import cz.cvut.eshop.shop.ShoppingCart; +import cz.cvut.eshop.shop.StandardItem; +import org.junit.*; + +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class ShoppingCartTest { + @Test + public void constructorTest(){ + //setup + ShoppingCart shopCart = new ShoppingCart(); + //assert + assertNotNull(shopCart); + } + + @Test + public void howManyItemsAreInTheCart_noItemSupposedToBe(){ + //setup + ShoppingCart shopCart = new ShoppingCart(); + ArrayList<Item> actual = shopCart.getCartItems(); + assertEquals(0, actual.size()); + //act + shopCart.getCartItems(); + } + + @Test + public void howManyItemsInTheCart_oneItemIsSupposedToBe(){ + //setup + ShoppingCart shopCart = new ShoppingCart(); + Item item = new StandardItem(1, "name", 3.14f,"category",10); + //act + shopCart.addItem(item); + int actual = shopCart.getItemsCount(); + //assert + assertEquals(1, actual); + } + + @Test + public void whatIsThePriceOfCart_20(){ + //setup + ShoppingCart shopCart = new ShoppingCart(); + Item item1 = new StandardItem(1, "name1", 1,"category",10); + Item item2 = new StandardItem(2, "name2", 2,"category",20); + Item item3 = new StandardItem(3, "name3", 17,"category",30); + //act + shopCart.addItem(item1); + shopCart.addItem(item2); + shopCart.addItem(item3); + //BYLO POTREBA UPRAVIT PUVODNI FUNKCI getTotalPrice(), JINAK NEFUNGOVALA A HAZELA CHYBY + int price = shopCart.getTotalPrice(); + //assert + assertEquals(20,price); + } + + @Test + public void isItemRemoved_2(){ + //setup + ShoppingCart shopCart = new ShoppingCart(); + Item item1 = new StandardItem(1, "name1", 1,"category",10); + Item item2 = new StandardItem(2, "name2", 2,"category",20); + Item item3 = new StandardItem(3, "name3", 17,"category",30); + //act + shopCart.addItem(item1); + shopCart.addItem(item2); + shopCart.addItem(item3); + int numberOfItems1 = shopCart.getItemsCount(); + //assert + assertEquals(3,numberOfItems1); + //act + //BYLO POTREBA UPRAVIT PUVODNI FUNKCI removeItem(), JINAK NEFUNGOVALA A HAZELA CHYBY + shopCart.removeItem(2); + //assert + int numberOfItems2 = shopCart.getItemsCount(); + assertEquals(2,numberOfItems2); + } +}