Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94e24ac

Browse files
committed
Starting point
2 parents f560a08 + d3f3fe8 commit 94e24ac

File tree

4 files changed

+83
-8
lines changed

4 files changed

+83
-8
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.serenitydojo.shoppingcart;
2+
3+
public class ItemInCart {
4+
5+
private final ShoppingItem item;
6+
private final int quantity;
7+
8+
public ItemInCart(ShoppingItem item, int quantity) {
9+
this.item = item;
10+
this.quantity = quantity;
11+
}
12+
13+
public ShoppingItem getItem() {
14+
return item;
15+
}
16+
17+
public int getQuantity() {
18+
return quantity;
19+
}
20+
}
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
package com.serenitydojo.shoppingcart;
22

3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
38
public class ShoppingCart {
49

5-
public void add(int quantity, ShoppingItem item) {}
10+
private List<ItemInCart> shoppingCartContents = new ArrayList<>();
11+
private double appliedDiscount = 0.0;
612

7-
public void calculateTotal() {}
13+
public void add(int quantity, ShoppingItem item) {
14+
shoppingCartContents.add(new ItemInCart(item,quantity));
15+
}
816

9-
public void addSalesTax() {}
17+
public void addBags(int numberOfBags) {
18+
ShoppingItem bag = new ShoppingItem("Shopping Bag", "bag", 0.50,"324534563546356","Shop");
19+
shoppingCartContents.add(new ItemInCart(bag, numberOfBags));
20+
}
1021

11-
public void addBags(int numberOfBags) {}
22+
public void applyDiscount(double discount) {
23+
ShoppingItem discountLineItem = new ShoppingItem("Discount", "1", -1 * discount,"","");
24+
shoppingCartContents.add(new ItemInCart(discountLineItem, 1));
25+
}
1226

1327
public double getTotal() {
14-
return 0;
28+
return shoppingCartContents.stream().mapToDouble(item -> item.getQuantity() * item.getItem().getPrice()).sum();
1529
}
1630
}

src/main/java/com/serenitydojo/shoppingcart/ShoppingItem.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,24 @@ public ShoppingItem(String name, String units, double price, String serialNumber
1414
this.serialNumber = serialNumber;
1515
this.brand = brand;
1616
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public String getUnits() {
23+
return units;
24+
}
25+
26+
public double getPrice() {
27+
return price;
28+
}
29+
30+
public String getSerialNumber() {
31+
return serialNumber;
32+
}
33+
34+
public String getBrand() {
35+
return brand;
36+
}
1737
}

src/test/java/com/serenitydojo/shoppingcart/WhenCalculatingTotalPrice.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,34 @@ public void shouldCalculatePriceWhenArticlesAreAddedToTheCart() {
1313
ShoppingItem item3 = new ShoppingItem("Apples", "bag", 6.99,"345457584524562","Farmers Pick");
1414
ShoppingItem item4 = new ShoppingItem("Oranges", "bag", 7.99,"986865754876587","Farmers Pick");
1515
cart.add(2, item1);
16-
cart.add(1, item4);
16+
cart.add(3, item2);
1717
cart.add(1, item3);
18+
cart.add(1, item4);
19+
double totalPrice = cart.getTotal();
20+
Assert.assertEquals(totalPrice, 36.93,0.0);
21+
}
22+
@Test
23+
public void bagsCost50pEach() {
24+
ShoppingCart cart = new ShoppingCart();
25+
ShoppingItem item1 = new ShoppingItem("Rice", "1 kg", 4.99,"1235357214735835","Rice R Us");
26+
ShoppingItem item2 = new ShoppingItem("Milk", "1 lt", 3.99,"7879827234234232","Home Brand Dairy");
27+
ShoppingItem item3 = new ShoppingItem("Apples", "bag", 6.99,"345457584524562","Farmers Pick");
28+
ShoppingItem item4 = new ShoppingItem("Oranges", "bag", 7.99,"986865754876587","Farmers Pick");
29+
cart.add(2, item1);
1830
cart.add(3, item2);
19-
cart.calculateTotal();
20-
cart.addSalesTax();
31+
cart.add(1, item3);
32+
cart.add(1, item4);
2133
cart.addBags(3);
2234
double totalPrice = cart.getTotal();
2335
Assert.assertEquals(totalPrice, 38.43,0.0);
2436
}
37+
38+
/**
39+
* TODO: Write another test to check what happens when you apply a discount using the "applyDiscount()" method
40+
*
41+
*/
42+
@Test
43+
public void shouldReduceThePriceWhenADiscountIsApplied() {
44+
45+
}
2546
}

0 commit comments

Comments
 (0)