-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShoppingCart.java
50 lines (45 loc) · 1.59 KB
/
ShoppingCart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.br.onlineshoppingsystem.entities;
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart {
private List<ShoppingCartItems> items = new ArrayList<>();
public ShoppingCart(){
}
public ShoppingCart(List<ShoppingCartItems> items) {
this.items = items;
}
public List<ShoppingCartItems> getItems() {
return items;
}
public void addItem(Products products, int quantity){
// for to verify if some item in Shopping car has the same name, if it is true
// it will increment the quantity
for (ShoppingCartItems carItem : items){
if(carItem.getProduct().getName().equals(products.getName())){
carItem.incrementQuantity(quantity);
return;
}
}
// if it is false so just add an item
items.add(new ShoppingCartItems(products, quantity));
}
public void removeItem(Products products, int quantity){
for (ShoppingCartItems carItem : items){
if(carItem.getProduct().getName().equals(products.getName())){
carItem.decrementQuantity(quantity);
return;
}
}
items.remove(new ShoppingCartItems(products, quantity));
}
public void removeEntireProduct(ShoppingCartItems shoppingCartItem){
getItems().remove(shoppingCartItem);
}
public double totalCost(){
double total = 0;
for (ShoppingCartItems carItems : items){
total += carItems.getQuantity() * carItems.getProduct().getPrice();
}
return total;
}
}