-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCustomer.java
More file actions
268 lines (216 loc) · 6.21 KB
/
Customer.java
File metadata and controls
268 lines (216 loc) · 6.21 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Class: Customer.java
Purpose:
- Represents a customer in the online shopping system
- Manages the customer's shopping cart, payment method, and orders
- Extends User class for customer-specific attributes
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Customer extends User implements Operations {
private static final int MAX_INSTANCES = 100;
private static int instanceCount = 0;
private ShoppingCart cart;
private String paymentMethod;
private ArrayList<Order> orders;
// Constructor
public Customer(String userID, String name, String phoneNumber, String address, String password) {
super(userID, name, phoneNumber, address, password);
if (instanceCount >= MAX_INSTANCES) {
throw new IllegalStateException("Reached max Customer limit " + MAX_INSTANCES);
}
this.cart = new ShoppingCart(userID);
this.orders = new ArrayList<>();
instanceCount++;
}
// Getters and setters
public static int getMaxInstances() {
return MAX_INSTANCES;
}
public static int getInstanceCount() {
return instanceCount;
}
public ShoppingCart getCart() {
return cart;
}
public String getPaymentMethod() {
return paymentMethod;
}
public ArrayList<Order> getOrders() {
return orders;
}
public void setCart(ShoppingCart cart) {
this.cart = cart;
}
public void setPaymentMethod(String paymentMethod) {
this.paymentMethod = paymentMethod;
}
public void setOrders(ArrayList<Order> orders) {
this.orders = orders;
}
// Customer-related functions
public void changePaymentMethod(String paymentMethod) {
setPaymentMethod(paymentMethod);
System.out.printf("Your payment method was changed to %s!%n", paymentMethod);
}
// Product-related functions
@Override
public void addProduct(Product p, int qty) {
cart.addProduct(p, qty);
}
@Override
public void removeProduct(Product p, int qty) {
cart.removeProduct(p, qty);
}
// Order-related functions
public void displayOrders() {
if (orders.isEmpty()) {
System.out.println("You have no orders yet.");
return;
}
for (Order o : orders) {
System.out.println(o.getOrderId());
}
}
public void placeOrder() {
if (this.cart.getproductsList().isEmpty()) {
System.out.println("Your cart is empty. Please add items before placing an order.");
return;
}
if (paymentMethod == null) {
System.out.println("Please set a payment method before placing an order.");
return;
}
// Create new order
Order order = new Order();
// Copy from cart to order
order.setProductOrdered(this.cart.getproductsList());
// Print order content
System.out.println("You ordered:");
for (Map.Entry<Product, Integer> entry : order.getProductOrdered().entrySet()) {
System.out.printf("- %s x %d%n", entry.getKey().getName(), entry.getValue());
}
// Process order
order.processOrder();
// Output total cost
System.out.printf("Total cost: $%.2f%n", order.computeTotalCost());
// Add order to user order history
orders.add(order);
}
public void confirmDelivery(String orderID) {
Order found = null;
for (Order o : orders) {
if (o.getOrderId().equals(orderID)) {
found = o;
break;
}
}
if (found.isCanceled()) {
System.out.println("Cannot deliver a canceled order.");
return;
}
if (orders.isEmpty()) {
System.out.println("You have no orders. Please place an order first.");
return;
}
if (found.isDelivered()) {
System.out.println("This order has already been marked as delivered.");
return;
}
// Mark as delivered
found.setDelivered(true);
found.setDeliveryDate(new java.util.Date());
System.out.println("Your order has been marked as delivered.");
}
public void cancelOrder(String orderID) {
if (orders.isEmpty()) {
System.out.println("You have no orders. Please place an order first.");
return;
}
Order found = null;
for (Order o : orders) {
if (o.getOrderId().equals(orderID)) {
found = o;
break;
}
}
if (found == null) {
System.out.println("Order not found.");
return;
}
found.cancelOrder();
orders.remove(found);
}
public void returnOrder(String orderID) {
if (orders.isEmpty()) {
System.out.println("You have no orders. Please place an order first.");
return;
}
Order found = null;
for (Order o : orders) {
if (o.getOrderId().equals(orderID)) {
found = o;
break;
}
}
if (found == null) {
System.out.println("Order not found.");
return;
}
found.initiateReturn();
}
public void completeReturn(String orderID) {
if (orders.isEmpty()) {
System.out.println("You have no orders. Please place an order first.");
return;
}
Order found = null;
for (Order o : orders) {
if (o.getOrderId().equals(orderID)) {
found = o;
break;
}
}
if (found == null) {
System.out.println("Order not found.");
return;
}
found.completeReturn();
}
public void checkRefund(String orderID) {
if (orders.isEmpty()) {
System.out.println("You have no orders. Please place an order first.");
return;
}
Order found = null;
for (Order o : orders) {
if (o.getOrderId().equals(orderID)) {
found = o;
break;
}
}
if (found == null) {
System.out.println("Order not found.");
return;
}
found.refundStatus();
}
@Override
public HashMap<String, Product> searchProducts(
String category,
double minPrice,
double maxPrice,
boolean isAvailable,
boolean discountAvailable) {
HashMap<String, Product> out = new HashMap<>();
for (Product p : Main.products) { // <- static access
if (category != null && !category.equalsIgnoreCase(p.getCategory())) continue;
if (p.getPrice() < minPrice || p.getPrice() > maxPrice) continue;
if (isAvailable && !p.isAvailable()) continue;
if (discountAvailable && p.getDiscountPercent() <= 0) continue;
out.put(p.getProductId(), p);
}
return out;
}
}