-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShoppingCart.java
68 lines (58 loc) · 1.73 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//Malak Saifelnasr 101142247
//Dana El Sherif 101148722
package mystore;
/**
* This class represents a shoppingCart
* @author Dana and Malak
* @version 5.0
*/
public class ShoppingCart extends ProductStockContainer{
private final int cartID;
private double total = 0.0;
public ShoppingCart(int cartID) {
this.cartID = cartID;
}
/**
* this method adds quantity of a product from the shoppingCart
* @param p the product to be added
* @param q the number of products to be added
*/
@Override
public void addProductQuantity(Product p, int q) {
super.addProductQuantity(p,q);
total += p.price * q;
}
/**
* this method removes a quantity of product from the shopping cart
* @param p the product type object to be added
* @param q an int of the amount of product to be removed
* @return
*/
@Override
public boolean removeProductQuantity(Product p, int q) {
for (int i = 0; i < super.getProducts().size(); i++) {
ProductStockPair pair = super.getProducts().get(i);
if (pair.product.id == p.id) {
if (pair.stock >= q) {
super.getProducts().set(i, new ProductStockPair(pair.product, pair.stock - q));
total -= p.price * q;
return true;
}
return false;
}
}
return false;
}
/**
* this method returns the total of the shopping cart
* @return a double of the total cost
*/
public double getTotal() {
return total;
}
/**
* this method returns the cartID
* @return an int of the cartID
*/
public int getCartID() { return cartID; }
}