-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdmin.java
More file actions
318 lines (255 loc) · 12.3 KB
/
Admin.java
File metadata and controls
318 lines (255 loc) · 12.3 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import java.util.ArrayList;
import java.util.Scanner;
public class Admin {
//admin username and password
private String adminUsername = "admin";
private String adminPassword = "admin123";
boolean adminLoggedIn = false; // Flag to check if admiin logged in
Shop st = new Shop();
private Shop shop; // reference to Shop class
Scanner scan = new Scanner(System.in);
// Constructor to initialize the Admin with a Shop instance
public Admin(Shop shop) {
this.shop = shop;
}
// Admin login dashboard method
public void loginAdmin() {
System.out.println("\nAdmin Login");
//repeat until admin input correct username and password
while(!adminLoggedIn){
System.out.print("Enter username : "); //admin enter uername
String inputUsername = scan.next();
System.out.print("Enter password : "); //admin enter password
String inputPassword = scan.next();
// verify credentials
if (inputUsername.equals(adminUsername) && inputPassword.equals(adminPassword)) {
adminLoggedIn = true; //Login Successful
System.out.println("Login Successful!");
adminMenu(); // Navigate to admin dashboard
} else {
System.out.println("Invalid username or password.\n");
while(true){
//try again if login fails or exit admin login
System.out.println("1.Try Again");
System.out.println("2.Exit");
System.out.print("Select an option : ");
int adminChoice = scan.nextInt();
System.out.println("");
switch(adminChoice){
case 1:
loginAdmin(); //back to the admin login dashboard to try
break;
case 2:
st.loginDashboard(); //back to main dashboard
break;
default:
System.out.println("Invalid Option!");
}
}
}
}
}
// Admin selection dashboard method
public void adminMenu(){
while (adminLoggedIn) {
System.out.println("\nWelcome to the Admin Dashboard!");
System.out.println("1. Manage Products");
System.out.println("2. View Customer Orders");
System.out.println("3. Logout");
System.out.print("Select an option: ");
int adminChoice = scan.nextInt();
switch(adminChoice){
case 1: //product managment dashboard
manageProducts();
break;
case 2: //view customer order status
viewOrders();
break;
case 3: //back to main dashboard
System.out.println("Admin Logout...\n");
st.loginDashboard();
break;
default:
System.out.println("Invalid Option!");
}
}
}
// Admin product management dashboard method
public void manageProducts(){
while (true) {
System.out.println("\nProducts Management");
System.out.println("1. Add Product");
System.out.println("2. Modify Product");
System.out.println("3. Remove Product");
System.out.println("4. View Product");
System.out.println("5. Back to Dashboard");
System.out.print("Enter your choice: ");
int adminChoice = scan.nextInt();
switch(adminChoice){
case 1: //add new product
addProduct();
break;
case 2: //update exisiting product
updateProduct();
break;
case 3: //remove exisiting product
removeProduct();
break;
case 4 : //view available products
showProduct();
break;
case 5: //back to dashboard
adminMenu();
break;
default:
System.out.println("Invalid Option!");
}
}
}
// Method to add products
public void addProduct(){
showProduct(); // Display product list before added
System.out.println("\n============ Add New Product ============");
System.out.print("Enter Product ID : ");
String id = scan.next();
scan.nextLine(); // Consume the leftover newline
System.out.print("Enter Product Name : ");
String name = scan.nextLine();
System.out.print("Enter Description : ");
String description = scan.nextLine();
System.out.print("Enter Quantity : ");
int quantity = scan.nextInt();
System.out.print("Enter Price : ");
float price = scan.nextFloat();
// Add product to the shop's product list
shop.getProducts().add(new Product(id, name, description,quantity, price));
System.out.println("Product added successfully!\n");
showProduct(); // Display updated product list
}
// Method to display all available products
public void showProduct(){
System.out.println("\n=============== Products ===============");
// Loop through each product in the shop's product list
for (Product product : shop.getProducts()) {
product.showInfo(); // updatedproduct list
}
}
// Method to update existing products
public void updateProduct(){
showProduct(); // Display all available products before updating
System.out.println("\n============ Update Product ============");
// admin to enter the product ID for updating
System.out.print("Enter Product ID : ");
String id = scan.next();
boolean productFound = false; // Flag to check if product exists
// Loop through the product list to find the matching Id
for (Product product : shop.getProducts()) {
if (product.getId().equals(id)) {
productFound = true; // Mark the product as found
// Show current product details
System.out.println("\n=========== Product Details ============");
System.out.println("Product Name : " + product.getName());
System.out.println("Description : " + product.getDescription());
System.out.println("Quantity : " + product.getQuantity());
System.out.println("Price : " + product.getPrice());
// Confirm removal from the admin
System.out.print("\nAre you sure to upadate this product? [yes/no]");
String upadteOption = scan.next();
if (upadteOption.equalsIgnoreCase("Yes")) {
//update product details
System.out.println("\n============ Update Product ============");
System.out.print("Enter New Product Name : ");
scan.nextLine(); // Consume leftover newline
String newName = scan.nextLine(); // Consume leftover newline
System.out.print("Enter Description : ");
String newDesc = scan.nextLine();
System.out.print("Enter New Quantity : ");
int newQty = scan.nextInt();
System.out.print("Enter New Price : ");
float newPrice = scan.nextFloat();
scan.nextLine(); // Consume leftover newline
// Update product details
product.setName(newName);
product.setDescription(newDesc);
product.setQuantity(newQty);
product.setPrice(newPrice);
System.out.println("\nProduct updated successfully!");
showProduct(); // Display updated product list
break; // Exit loop
}
else if (upadteOption.equalsIgnoreCase("No")) {
// Cancel update and exit the loop
System.out.println("\nProduct not updated!");
break;
}
else {
// Handle invalid input for removal confirmation
System.out.println("Invalid option!");
break;
}
}
}
//if product not found in list
if (!productFound) {
System.out.println("Product with ID " + id + " not found.");
}
}
// Method to remove an existing product from the inventory
public void removeProduct(){
showProduct(); // Display the current product list before removal
System.out.println("\n============ Remove Product ============");
boolean productFound = false; // Flag to track if the product exists
System.out.print("Enter Product ID : ");
String id = scan.next();
// Retrieve the list of products from the shop
ArrayList<Product> product = shop.getProducts();
// Check through the product list to find the matching ID
for (int i = 0; i < product.size(); i++) {
if (product.get(i).getId().equals(id)) {
// Display product details before removal
System.out.println("======== Product to be Removed =========");
product.get(i).showInfo();
// Confirm removal from the admin
System.out.print("\nAre you sure to remove this product? [yes/no]");
String removeOption = scan.next();
if (removeOption.equalsIgnoreCase("Yes")) {
// Remove the product from the list
product.remove(i);
System.out.println("Product removed successfully!");
productFound = true;
showProduct(); // Display the updated product list
break; // Exit loop after removal
}
else if (removeOption.equalsIgnoreCase("No")) {
// Cancel removal and exit the loop
System.out.println("Product removal canceled.");
productFound = true;
break;
}
else {
// Handle invalid input for removal confirmation
System.out.println("Invalid option!");
productFound = true;
break;
}
}
}
//if product not found
if (!productFound) {
System.out.println("Product with ID " + id + " not found.");
}
}
// View all customer orders
private void viewOrders() {
// Display no orders if orders not placed by customer
if (Order.getOrders().isEmpty()) {
System.out.println("\nNo orders yet.");
} else {
System.out.println("\n==== Order History =====");
// // Loop through all the orders
for (Order order : Order.getOrders()) {
order.showOrder(); // Display each order with the corresponding invoice number
}
}
}
}