Commit 06cbc763 authored by Karel Frajták's avatar Karel Frajták

Merge branch 'Ukol1' into 'master'

Ukol1

See merge request !18
parents 8f01acb7 5e0ad0d3
...@@ -88,7 +88,7 @@ public class Order { ...@@ -88,7 +88,7 @@ public class Order {
} }
public float getTotalAmount() { public float getTotalAmount() {
int totalAmount = 0; float totalAmount = 0;
// spocitat vernostni body za polozky v nak. voziku // spocitat vernostni body za polozky v nak. voziku
for (Item item : items) { for (Item item : items) {
totalAmount += item.getPrice(); totalAmount += item.getPrice();
......
...@@ -42,7 +42,7 @@ public class ShoppingCart { ...@@ -42,7 +42,7 @@ public class ShoppingCart {
*/ */
public void removeItem(int itemID) { 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); Item temp_item = (Item) items.get(i);
if (temp_item.getID() == itemID) { if (temp_item.getID() == itemID) {
items.remove(i); items.remove(i);
...@@ -61,9 +61,9 @@ public class ShoppingCart { ...@@ -61,9 +61,9 @@ public class ShoppingCart {
* *
* @return total price with discount * @return total price with discount
*/ */
public int getTotalPrice() { public float getTotalPrice() {
int total = 0; float total = 0f;
for (int i = items.size() - 1; i <= 0; i--) { for (int i = 0; i < items.size(); i++) {
Item temp_item = (Item) items.get(i); Item temp_item = (Item) items.get(i);
total += temp_item.getPrice(); total += temp_item.getPrice();
} }
......
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
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
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment