Repository navigation
Expand file tree
/
Copy pathCustomer.java
More file actions
102 lines (84 loc) · 3.4 KB
/
Copy pathCustomer.java
File metadata and controls
102 lines (84 loc) · 3.4 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
package talabatapp;
import java.io.*;
import java.util.ArrayList;
public class Customer extends User implements Serializable {
private String address;
private ArrayList<Product> cart = new ArrayList<>();
private ArrayList<String> orders = new ArrayList<>();
public Customer(String name, String email, String password, UserRole role) {
super(name, email, password, role);
}
public Customer(String name, String email, String password, UserRole role, String address) {
super(name, email, password, role.CUSTOMER);
this.address = address;
}
// Register function to add a new customer
public void register(String name, String email, String password, UserRole role, String address) {
try {
ArrayList<User> users = new ArrayList<>();
File file = new File("D:\\Users.txt");
// Load existing users if file exists
if (file.exists() && file.length() > 0) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
users = (ArrayList<User>) ois.readObject();
} catch (Exception ex) {
System.out.println("Error reading existing users: " + ex.getMessage());
}
}
// Add new customer
Customer newCustomer = new Customer(name, email, password, role, address);
users.add(newCustomer);
// Write updated list back
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
oos.writeObject(users);
System.out.println("Customer registered successfully.");
}
} catch (Exception e) {
System.out.println("Error registering customer: " + e.getMessage());
}
}
public void addToCart(Product product) {
try {
if (product != null) {
cart.add(product);
System.out.println("Product added to cart: " + product.getName());
} else {
throw new IllegalArgumentException("Product cannot be null.");
}
} catch (Exception e) {
System.out.println("Error adding product to cart: " + e.getMessage());
}
}
public void removeFromCart(Product product) {
try {
if (cart.contains(product)) {
cart.remove(product);
System.out.println("Product removed from cart: " + product.getName());
} else {
throw new IllegalArgumentException("Product not found in cart.");
}
} catch (Exception e) {
System.out.println("Error removing product from cart: " + e.getMessage());
}
}
// Track an order (Just a sample logic)
public void trackOrder(int orderId) {
try {
if (orderId >= 0 && orderId < orders.size()) {
System.out.println("Tracking Order: " + orders.get(orderId));
} else {
throw new IndexOutOfBoundsException("Order ID not valid.");
}
} catch (Exception e) {
System.out.println("Error tracking order: " + e.getMessage());
}
}
// Getter for cart
public ArrayList<Product> getCart() {
return cart;
}
// Getter for orders
public ArrayList<String> getOrders() {
return orders;
}
}