-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShoppingCart.java
127 lines (92 loc) · 3.66 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package CakeShop;
// import java.applet.*;
// import java.awt.*;
// import java.net.*;
import java.util.*;
// This class is a simple container of shopping cart cakes.
// It is observable, which means that it notifies any interested
// classes whenever it changes.
public class ShoppingCart {
// Our vector is like an array and things being added to it are the cakes the
// customer has bought
Vector<Object> cakes;
protected int totalCost = 0;
public ShoppingCart() {
cakes = new Vector<Object>();
}
// Adding a new cake and updating the totalCost
// Takes in an object of the cake
public void addItem(ShoppingCartItem newCake) {
// See if there's already an item like this in the cart
int currIndex = cakes.indexOf(newCake);
// Our event listener to help with the adding and removing from the cart
ShoppingCartEvent event = new ShoppingCartEvent();
if (currIndex == -1) {
// If the item is new, add it to the cart
cakes.add(newCake);
event.item = newCake;
event.eventType = ShoppingCartEvent.ADDED_ITEM;
} else {
// If there is a similar item, just add the quantities
ShoppingCartItem currItem = (ShoppingCartItem) cakes.elementAt(currIndex);
currItem.add(newCake);
event.item = currItem;
event.eventType = ShoppingCartEvent.CHANGED_ITEM;
}
totalCost += newCake.cakeCost * newCake.quantity;
// Tell the observers what just happened
// setChanged();
// notifyObservers(event);
}
// Remove item removes an item from the cart. Since it removes
// n cakes from the cart at a time, if there are more than n cakes
// in the cart, it just subtracts n from the quantity.
public void removeItem(ShoppingCartItem oldItem) {
// Find this object in the cart
int currIndex = cakes.indexOf(oldItem);
ShoppingCartEvent event = new ShoppingCartEvent();
if (currIndex == -1) {
// If it wasn't there, just return, assume everything's okay
return;
} else {
ShoppingCartItem currItem = (ShoppingCartItem) cakes.elementAt(currIndex);
// If you are trying to subtract more cakes than are in the cart,
// adjust the amount you want to subtract so it is equal to the
// number of cakes in the cart.
if (oldItem.quantity > currItem.quantity) {
oldItem.quantity = currItem.quantity;
}
// Adjust the totalCost
totalCost -= oldItem.cakeCost * oldItem.quantity;
currItem.subtract(oldItem);
event.item = currItem;
event.eventType = ShoppingCartEvent.CHANGED_ITEM;
// If the quantity drops to 0, remove the item entirely
if (currItem.quantity == 0) {
cakes.removeElementAt(currIndex);
event.eventType = ShoppingCartEvent.REMOVED_ITEM;
}
}
// Tell everyone what happened
// setChanged();
// notifyObservers(event);
}
// getItems returns a copy of all the cakes in the cart
// public Vector<Object> getItems() {
// return cakes;
// }
public void getItems() {
Object[] cakeArr = cakes.toArray();
for (Object cake : cakeArr) {
System.out.println(cake);
}
}
// Vector method to help us return Our cakes
public Vector<Object> cakesBought() {
return cakes;
}
// To hep us return a nice array of our things
public String toString() {
return this.cakesBought().toString();
}
}