-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCart.java
More file actions
61 lines (49 loc) · 1.57 KB
/
Cart.java
File metadata and controls
61 lines (49 loc) · 1.57 KB
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
import java.util.HashMap;
public class Cart {
private HashMap<Product, Integer> cartInfo;
private double total;
// defult constuctor
public Cart (){
this.cartInfo = new HashMap<>();
this.total = 0.0;
}
// method to store cart string info
public String[] getViewCart () {
String[] viewDisplay = new String [cartInfo.keySet().size()];
int i = 0;
for (Product p : this.cartInfo.keySet()) {
if (p!=null & cartInfo.get(p) > 0 &&
cartInfo.get(p)+ " x " + p.toString() != null)
{
viewDisplay[i] = cartInfo.get(p)+ " x " + p.toString();
i ++;
}
}
return viewDisplay;
}
// method to get cart info
public HashMap<Product, Integer> getCartInfo (){return cartInfo;}
public void addCart(Product p){
if (this.cartInfo.containsKey(p)){
int amount = this.cartInfo.get(p);
this.cartInfo.put(p, amount + 1);
}else{
this.cartInfo.put(p, 1);
}
addCart(p.getPrice());
}
public void removeCart(Product p){
if (this.cartInfo.containsKey(p)){
int amount = this.cartInfo.get(p);
this.cartInfo.put(p, amount - 1);
}
subCart(p.getPrice());
}
public void addCart (double amount){
this.total += amount;
}
public void subCart (double amount){
this.total -= amount;
}
public double getTotal() {return total;}
}