-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOnline Auction Platform.java
507 lines (438 loc) · 18.2 KB
/
Online Auction Platform.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package dsp;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
// Binary Search Tree Node and Tree classes
class BSTNode {
Product product;
BSTNode left, right;
public BSTNode(Product product) {
this.product = product;
this.left = this.right = null;
}
}
class BinarySearchTree {
private BSTNode root;
public void insert(Product product) {
root = insertRec(root, product);
}
private BSTNode insertRec(BSTNode root, Product product) {
if (root == null) {
root = new BSTNode(product);
return root;
}
if (product.getName().compareTo(root.product.getName()) < 0) {
root.left = insertRec(root.left, product);
} else if (product.getName().compareTo(root.product.getName()) > 0) {
root.right = insertRec(root.right, product);
}
return root;
}
public Product search(String productName) {
return searchRec(root, productName);
}
private Product searchRec(BSTNode root, String productName) {
if (root == null || root.product.getName().equals(productName)) {
return root != null ? root.product : null;
}
if (productName.compareTo(root.product.getName()) < 0) {
return searchRec(root.left, productName);
}
return searchRec(root.right, productName);
}
public void inorderTraversal() {
inorderRec(root);
}
private void inorderRec(BSTNode root) {
if (root != null) {
inorderRec(root.left);
System.out.println("Product: " + root.product.getName() + ", Status: " + root.product.getStatus());
inorderRec(root.right);
}
}
}
// Product Class
class Product {
private String id;
private String name;
private String category;
private String status;
private double minBid;
private double highestBid;
private Buyer highestBidder;
private Thread biddingThread;
private Thread trackingThread;
public Product(String id, String name, String category) {
this.id = id;
this.name = name;
this.category = category;
this.status = "Not Started";
this.highestBid = 0.0;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public String getStatus() {
return status;
}
public double getMinBid() {
return minBid;
}
public double getHighestBid() {
return highestBid;
}
public Buyer getHighestBidder() {
return highestBidder;
}
public void setMinBid(double minBid) {
this.minBid = minBid;
}
public void placeBid(double bid, Buyer buyer) {
if (bid > highestBid && bid >= minBid) {
highestBid = bid;
highestBidder = buyer;
System.out.println("New highest bid: $" + highestBid);
} else {
System.out.println("Bid must be higher than the current highest bid and minimum bid.");
}
}
public void startBidding() {
if (biddingThread == null) {
biddingThread = new Thread(() -> {
System.out.println("Bidding started for " + name);
try {
Thread.sleep(30000); // Bidding period of 30 seconds
} catch (InterruptedException e) {
System.out.println("Bidding interrupted.");
}
System.out.println("Bidding ended for " + name + ". Final highest bid: $" + highestBid);
status = "Bidding Closed";
});
status = "Bidding in Progress";
biddingThread.start();
} else {
System.out.println("Bidding is already in progress for this product.");
}
}
public void trackPackage() {
if (trackingThread == null) {
trackingThread = new Thread(() -> {
String[] stages = {"Ready to Deliver", "Out for Delivery", "Delivered"};
for (String stage : stages) {
try {
Thread.sleep(10000); // Update every 10 seconds
} catch (InterruptedException e) {
System.out.println("Tracking interrupted.");
}
status = stage;
System.out.println("Tracking " + name + ": " + stage);
}
});
trackingThread.start();
} else {
System.out.println("Tracking already in progress for this product.");
}
}
public void updateStatus(String newStatus) {
status = newStatus;
}
}
// Admin, Seller, and Buyer classes
class Admin {
private String username = "admin";
private String password = "admin123";
public boolean login(String username, String password) {
return this.username.equals(username) && this.password.equals(password);
}
public void setMinBid(Product product, double minBid) {
product.setMinBid(minBid);
System.out.println("Minimum bid set to $" + minBid + " for " + product.getName());
}
public void trackPackage(Product product) {
product.trackPackage();
}
public void approveProduct(Product product) {
product.updateStatus("Approved");
System.out.println("Product " + product.getName() + " approved.");
}
public void viewProducts(BinarySearchTree bst) {
System.out.println("Admin Viewing All Products:");
bst.inorderTraversal();
}
}
class Seller {
private List<Product> productHistory = new ArrayList<>();
public boolean login(String username, String password) {
System.out.println("Seller logged in with username: " + username);
return true;
}
// Adding a product to the seller's product list
public Product addProduct(String name) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Product ID: ");
String id = scanner.nextLine();
System.out.print("Enter Product Category: ");
String category = scanner.nextLine();
Product newProduct = new Product(id, name, category);
productHistory.add(newProduct);
System.out.println("Product " + name + " added to your listing.");
return newProduct;
}
// Viewing the product history for the seller
public void viewProductHistory() {
System.out.println("Product History:");
for (Product product : productHistory) {
System.out.println("ID: " + product.getId() + ", Name: " + product.getName() + ", Category: " + product.getCategory() + ", Status: " + product.getStatus());
}
}
// Sorting products by their category and displaying them
public void sortProductsByCategory() {
productHistory.sort(Comparator.comparing(Product::getCategory));
System.out.println("Products sorted by category:");
viewProductHistory();
}
}
class Buyer {
private String username;
private String password;
public Buyer(String username, String password) {
this.username = username;
this.password = password;
}
public boolean login(String username, String password) {
return this.username.equals(username) && this.password.equals(password);
}
public void viewProduct(Product product) {
System.out.println("Viewing Product: " + product.getName() + ", Status: " + product.getStatus() +
", Min Bid: $" + product.getMinBid() + ", Highest Bid: $" + product.getHighestBid());
}
public void placeBid(Product product) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your bid amount: ");
double bid = scanner.nextDouble();
product.placeBid(bid, this);
System.out.println("do anyone wnat to place next bid yes(1)/no(0)");
int chh = scanner.nextInt();
if(chh==0){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("bid interrupted");
}
System.out.println("THIS item is sold to "+ username +" \nPROCEED WITH PAYMENT!!!");
}
}
public void makePayment(Product product) {
if (this == product.getHighestBidder()) {
System.out.println("Processing payment of $" + product.getHighestBid());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Processing payment was interrupted.");
}
System.out.println("Payment successful for $" + product.getHighestBid());
} else {
System.out.println("Only the highest bidder can make payment.");
}
}
public void trackPackage(Product product) {
product.trackPackage();
}
}
class Graph {
private HashMap<Product, List<Product>> adjList = new HashMap<>();
public void addEdge(Product from, Product to) {
adjList.putIfAbsent(from, new ArrayList<>());
adjList.putIfAbsent(to, new ArrayList<>());
adjList.get(from).add(to);
System.out.println("Edge added from " + from.getName() + " to " + to.getName());
}
public void displayGraph() {
System.out.println("Product Dependency Graph:");
for (Product product : adjList.keySet()) {
System.out.print(product.getName() + " -> ");
for (Product connectedProduct : adjList.get(product)) {
System.out.print(connectedProduct.getName() + " ");
}
System.out.println();
}
}
}
class Main {
private static LinkedList<Product> products = new LinkedList<>();
private static BinarySearchTree bst = new BinarySearchTree();
private static Admin admin = new Admin();
private static Seller seller = new Seller();
private static HashMap<String, Buyer> buyers = new HashMap<>();
private static Graph productGraph = new Graph(); // Initialize the Graph
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean exit = false;
while (!exit) {
System.out.println("\nMain Menu:");
System.out.println("1. Admin Login");
System.out.println("2. Seller Login");
System.out.println("3. Buyer Login for Bidding");
System.out.println("4. Add Product Dependency (Graph Edge)");
System.out.println("5. Display Product Graph");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter Admin username: ");
String adminUsername = scanner.nextLine();
System.out.print("Enter Admin password: ");
String adminPassword = scanner.nextLine();
if (admin.login(adminUsername, adminPassword)) {
System.out.println("Admin logged in.");
adminMenu(scanner);
} else {
System.out.println("Invalid Admin credentials.");
}
break;
case 2:
System.out.print("Enter Seller username: ");
String sellerUsername = scanner.nextLine();
System.out.print("Enter Seller password: ");
String sellerPassword = scanner.nextLine();
if (seller.login(sellerUsername, sellerPassword)) {
System.out.print("Enter Product Name to add: ");
String productName = scanner.nextLine();
Product newProduct = seller.addProduct(productName);
products.add(newProduct);
bst.insert(newProduct);
}
break;
case 3:
System.out.print("Enter Buyer username: ");
String buyerUsername = scanner.nextLine();
System.out.print("Enter Buyer password: ");
String buyerPassword = scanner.nextLine();
Buyer currentBuyer = buyers.get(buyerUsername);
if (currentBuyer == null) {
currentBuyer = new Buyer(buyerUsername, buyerPassword);
buyers.put(buyerUsername, currentBuyer);
System.out.println("New Buyer account created.");
}
// Displaying Products for Bidding
System.out.print("Choose Product Name to Bid on: ");
String bidProductName = scanner.nextLine();
Product bidProduct = bst.search(bidProductName);
if (bidProduct != null) {
currentBuyer.viewProduct(bidProduct);
System.out.println("1. Place Bid\n2. Make Payment\n3. Track Package\nChoose an option:");
int buyerChoice = scanner.nextInt();
scanner.nextLine();
switch (buyerChoice) {
case 1:
// Call the placeBid method (this is where the 3-second window is managed)
currentBuyer.placeBid(bidProduct);
break;
case 2:
currentBuyer.makePayment(bidProduct);
break;
case 3:
currentBuyer.trackPackage(bidProduct);
break;
default:
System.out.println("Invalid option.");
}
} else {
System.out.println("Product not found.");
}
break;
case 4:
System.out.print("Enter source product name for dependency: ");
String sourceName = scanner.nextLine();
Product sourceProduct = bst.search(sourceName);
System.out.print("Enter destination product name for dependency: ");
String destName = scanner.nextLine();
Product destProduct = bst.search(destName);
if (sourceProduct != null && destProduct != null) {
productGraph.addEdge(sourceProduct, destProduct);
} else {
System.out.println("One or both products not found.");
}
break;
case 5:
productGraph.displayGraph();
break;
case 6:
System.out.println("Exiting the application.");
exit = true;
break;
default:
System.out.println("Invalid choice. Please try again.");
}
}
scanner.close();
}
private static void adminMenu(Scanner scanner) {
boolean adminExit = false;
while (!adminExit) {
System.out.println("\nAdmin Menu:");
System.out.println("1. Set Minimum Bid for Product");
System.out.println("2. Track Product Package");
System.out.println("3. Approve Product for Bidding");
System.out.println("4. View All Products");
System.out.println("5. Logout");
System.out.print("Choose an option: ");
int adminChoice = scanner.nextInt();
scanner.nextLine();
switch (adminChoice) {
case 1:
System.out.print("Enter Product Name: ");
String productName = scanner.nextLine();
Product product = bst.search(productName);
if (product != null) {
System.out.print("Enter Minimum Bid Amount: ");
double minBid = scanner.nextDouble();
admin.setMinBid(product, minBid);
} else {
System.out.println("Product not found.");
}
break;
case 2:
System.out.print("Enter Product Name: ");
String trackProductName = scanner.nextLine();
Product trackProduct = bst.search(trackProductName);
if (trackProduct != null) {
admin.trackPackage(trackProduct);
} else {
System.out.println("Product not found.");
}
break;
case 3:
System.out.print("Enter Product Name: ");
String approveProductName = scanner.nextLine();
Product approveProduct = bst.search(approveProductName);
if (approveProduct != null) {
admin.approveProduct(approveProduct);
} else {
System.out.println("Product not found.");
}
break;
case 4:
admin.viewProducts(bst);
break;
case 5:
System.out.println("Admin logged out.");
adminExit = true;
break;
default:
System.out.println("Invalid option.");
}
}
}
}