-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
269 lines (217 loc) · 11.2 KB
/
Customer.java
File metadata and controls
269 lines (217 loc) · 11.2 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
269
import java.util.ArrayList;
import java.util.Scanner;
public class Customer {
// Customer username and password
private String customerUsername ;
private String customerPassword ;
boolean isCustomerLoggedIn = false; // Flag to check if customer logged in
Shop st = new Shop();
private Shop shop;
private String loggedInCustomer = ""; // Stores the username of the currently logged in customer.
Scanner scan = new Scanner(System.in);
private static ArrayList<Customer> customers = new ArrayList<>(); // Customer register store list
private ArrayList<Product> cart = new ArrayList<>(); // Customer cart to store product list
private ArrayList<Order> order = new ArrayList<>(); // Customer orders store list
// Constructor to initialize the shop object
public Customer(String customerUsername, String customerPassword, Shop shop) {
this.customerUsername = customerUsername;
this.customerPassword = customerUsername;
this.shop = shop;
}
// Method to customer login dashboard
public void loginCustomer(){
System.out.println("\nCustomer Login");
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Select an option : ");
int choice = scan.nextInt();
switch (choice){
case 1: // Customer create new account and login
createAccount();
authenticateCustomer();
break;
case 2: // Customer login
authenticateCustomer();
break;
case 3: // Back to main dashboard
st.loginDashboard();
default:
System.out.println("Invalid Option!");
}
}
// method to create a new customer account
public void createAccount(){
System.out.println("\nCreate New Account");
System.out.print("Enter username : ");
String newUsername = scan.next();
System.out.print("Enter password : ");
String newPassword = scan.next();
// Check if the username already exists
for (Customer custname : customers) { // Iterate over the list of existing customers
if (custname.customerUsername.equalsIgnoreCase(newUsername)) { // If matched existing account
System.out.println("Username already exists! Try again.");
createAccount();
return;
}
}
// Create new customer and add to list
Customer newCustomer = new Customer(newUsername, newPassword, shop); // Create a new customer object
customers.add(newCustomer); // Add the new customer to the list of customers
System.out.println("Account create successfully! You can now login.");
}
public void authenticateCustomer(){
System.out.println("\nLogin To Account");
// Repeat until customer input correct username and password
while (!isCustomerLoggedIn) {
System.out.print("Enter username: ");
String inputUsername = scan.next(); // Read the input username
System.out.print("Enter password: ");
String inputPassword = scan.next(); // Read the input password
// Validate username and password
for (Customer cus : customers) { // Loop through the list of registered customers
if (cus.customerUsername.equalsIgnoreCase(inputUsername) && cus.customerPassword.equals(inputPassword)) {
isCustomerLoggedIn = true; // Set login flag to true
loggedInCustomer = inputUsername; // Store the username of the logged in customer
System.out.println("Login Successful!");
showCustomerMenu(); // Show the customer menu
return;
}
}
// If login fails
System.out.println("Invalid username or password.\n");
System.out.println("1. Try Again");
System.out.println("2. Exit");
System.out.print("Select an option: ");
int choice = scan.nextInt();
if (choice == 2) { // If the user chooses to exit
shop.loginDashboard(); // Go back to the login dashboard
return; // Exit the method
}
}
}
// Method to customer menu after login
public void showCustomerMenu(){
// Loop runs as long as the customer is logged in
while (isCustomerLoggedIn) {
System.out.println("\nWelcome to City Electronics!");
System.out.println("1. Browse Products");
System.out.println("2. Place Order");
System.out.println("3. Logout");
System.out.print("Select an option: ");
int customerChoice = scan.nextInt(); // Read the customer’s choice
switch(customerChoice){
case 1: // browse products
browseProducts();
break;
case 2: // place order
placeOrder();
break;
case 3: // back to main dashboard
System.out.println("Logout...\n");
st.loginDashboard(); //go back to the login dashboard
break;
default: // If the customer enters an invalid option
System.out.println("Invalid Option!");
}
}
}
// Method to display the list of available products
public void browseProducts(){
System.out.println("\n============ Browse Products ===========");
// Loop through the list of products in the shop
for (Product product : shop.getProducts()) {
System.out.println("Product ID : " + product.getId());
System.out.println("Product Name : " + product.getName());
System.out.println("Description : " + product.getDescription());
System.out.println("Price : " + product.getPrice());
// check if product is in stock or out of stock
if (product.getQuantity()>0){
System.out.println("Available"); // If the quantity is greater than 0, the product is available
}
else {
System.out.println("Out of Stock"); // If the quantity is 0 or less, the product is out of stock
}
System.out.println("-----------------------------------------");
}
}
// Method to customer placing order
public void placeOrder(){
browseProducts(); // Display products to browse
System.out.println("\n============= Place Order ==============");
Product selectedProduct = null; // Variable to store the selected product
while(true){ // Loop to handle customer order placement
// customer product selection to add to cart by product name or id
System.out.print("Enter Product ID : ");
String productSelection = scan.next();
// checking for the selected product exists
for (Product product : shop.getProducts()){
if (product.getId().equalsIgnoreCase(productSelection)){
selectedProduct = product; // Store the selected product
}
}
if (selectedProduct == null){ // if product not found
System.out.println("Product not found!");
}
else if (selectedProduct.getQuantity() <= 0 ){ //if product out of stock
System.out.println("Sorry, out of stock");
}
else { //customer quantity enter for the product
System.out.print("Enter quantity : ");
int qty = scan.nextInt();
if (qty <= 0 ){ // if quantity is invalid
System.out.println("Invalid quantity. Try again.");
}
else if (qty > selectedProduct.getQuantity()){ // if quantity exceeds stock
System.out.println("Insufficient stock!");
}
else { // add product to cart
cart.add(new Product(selectedProduct.getId(), selectedProduct.getName(),selectedProduct.getDescription(), qty, selectedProduct.getPrice()));
System.out.println(qty + " * " + selectedProduct.getName() + " added to cart.\n");
}
}
// customer can continue shopping or go to checkout
System.out.print("Press 1 to Continue Shopping or 2 to Go to Checkout : ");
int choice = scan.nextInt();
if (choice == 2){ // If the user chooses to exit
checkout(); // Go back to the login dashboard
return; // Exit the method
}
}
}
// Method to customer checkout menu
public void checkout(){
float total = 0.0f; // Variable to store the total order amount
System.out.println("\n=============== Checkout ===============");
// display all items in the cart and calculate total
for (Product product : cart) {
product.showInfo(); // Display product details
total += product.getPrice() * product.getQuantity(); // Calculate total amount
}
System.out.println("Total Amount : " + total);
System.out.print("Do you want to confirm the order? [yes/no]: ");
String orderConfirm = scan.next();
// order status whether customer confirm order or not
String status;
if (orderConfirm.equalsIgnoreCase("yes")) {
status = "Paid"; // Set status as paid
System.out.println("\nOrder Placed Successfully!");
// Reduce the stock quantity of each ordered product
for (Product orderedProduct : cart) {
for (Product shopProduct : shop.getProducts()) {
if (shopProduct.getId().equals(orderedProduct.getId())) {
shopProduct.setQuantity(shopProduct.getQuantity() - orderedProduct.getQuantity());
}
}
}
} else {
status = "Canceled"; // Set status as canceled
System.out.println("\nOrder Canceled.");
}
// Create a new order and add it to the orders list
ArrayList<Product> orderProducts = new ArrayList<>(cart);
Order newOrder = new Order(loggedInCustomer, orderProducts, total, status); // Create new order
Order.getOrders().add(newOrder); // Add order to the order list
cart.clear(); // Clear the cart after order
}
}