-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuction.java
More file actions
1223 lines (1070 loc) · 40.4 KB
/
Auction.java
File metadata and controls
1223 lines (1070 loc) · 40.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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text. *;
import java.util. *;
public class Auction {
private static Scanner scanner = new Scanner(System.in);
private static String username;
private static Connection conn;
private static Statement stmt;
enum Category {
ELECTRONICS,
BOOKS,
HOME,
CLOTHING,
SPORTINGGOODS,
OTHERS
}
enum Condition {
NEW,
LIKE_NEW,
GOOD,
ACCEPTABLE
}
enum BidStatus {
OPENED,
TENDERED,
CLOSED
}
private static boolean LoginMenu() {
String userpass;
Boolean isSuccess = true;
do {
System.out.print("----< User Login >\n" +
" ** To go back, enter 'back' in user ID.\n" +
" user ID: ");
try{
username = scanner.next();
scanner.nextLine();
if(username.equalsIgnoreCase("back")){
return false;
}
System.out.print(" password: ");
userpass = scanner.next();
scanner.nextLine();
}catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
username = null;
return false;
}
/* Your code should come here to check ID and password */
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT COUNT(*) FROM users WHERE user_id = ? AND pw = ?");
pStmt.setString(1, username);
pStmt.setString(2, userpass);
ResultSet rset = pStmt.executeQuery();
if (!rset.next() || rset.getInt(1) == 0) {
/* If Login Fails */
System.out.println("Error: Incorrect user name or password\n");
isSuccess = false;
}
else {
System.out.println("You are successfully logged in.\n");
return true;
}
}catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
return false;
}
}
while(!isSuccess);
return true;
}
private static boolean SellMenu() {
Category category = null;
Condition condition = null;
char choice;
String name, description;
double startPrice, buynowPrice;
boolean useCoupon, flag_catg = true, flag_cond = true;
Integer couponId = null;
do{
System.out.println(
"----< Sell Item >\n" +
"---- Choose a category.\n" +
" 1. Electronics\n" +
" 2. Books\n" +
" 3. Home\n" +
" 4. Clothing\n" +
" 5. Sporting Goods\n" +
" 6. Other Categories\n" +
" P. Go Back to Previous Menu"
);
try {
choice = scanner.next().charAt(0);;
}catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
continue;
}
flag_catg = true;
switch ((int) choice){
case '1':
category = Category.ELECTRONICS;
continue;
case '2':
category = Category.BOOKS;
continue;
case '3':
category = Category.HOME;
continue;
case '4':
category = Category.CLOTHING;
continue;
case '5':
category = Category.SPORTINGGOODS;
continue;
case '6':
category = Category.OTHERS;
continue;
case 'p':
case 'P':
return false;
default:
System.out.println("Error: Invalid input is entered. Try again.");
flag_catg = false;
continue;
}
}while(!flag_catg);
do{
System.out.println(
"---- Select the condition of the item to sell.\n" +
" 1. New\n" +
" 2. Like-new\n" +
" 3. Used (Good)\n" +
" 4. Used (Acceptable)\n" +
" P. Go Back to Previous Menu"
);
try {
choice = scanner.next().charAt(0);;
scanner.nextLine();
}catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
continue;
}
flag_cond = true;
switch (choice) {
case '1':
condition = Condition.NEW;
break;
case '2':
condition = Condition.LIKE_NEW;
break;
case '3':
condition = Condition.GOOD;
break;
case '4':
condition = Condition.ACCEPTABLE;
break;
case 'p':
case 'P':
return false;
default:
System.out.println("Error: Invalid input is entered. Try again.");
flag_cond = false;
continue;
}
}while(!flag_cond);
try {
System.out.println("---- Name of the item: ");
name = scanner.nextLine();
System.out.println("---- Description of the item (one line): ");
description = scanner.nextLine();
System.out.println("---- Start price: ");
while (!scanner.hasNextDouble()) {
scanner.next();
System.out.println("Invalid input is entered. Please enter Start price: ");
}
startPrice = scanner.nextDouble();
scanner.nextLine();
System.out.println("---- Buy-It-Now price: ");
while (!scanner.hasNextDouble()) {
scanner.next();
System.out.println("Invalid input is entered. Please enter Buy-It-Now price: ");
}
buynowPrice = scanner.nextDouble();
scanner.nextLine();
System.out.print("---- Will you use a coupon? (Y/N): ");
useCoupon = scanner.next().equalsIgnoreCase("Y");
scanner.nextLine();
if(useCoupon) {
System.out.println("---- Coupon ID: ");
couponId = scanner.nextInt();
scanner.nextLine();
PreparedStatement checkStmt = conn.prepareStatement("SELECT COUNT(*) FROM coupon WHERE coupon_id = ? AND available = 'USABLE'");
checkStmt.setInt(1, couponId);
ResultSet rset = checkStmt.executeQuery();
if (rset.next() && rset.getInt(1) == 0) {
System.out.println("Error: Invalid or unusable coupon ID.");
throw new Exception();
}
}
System.out.println("---- Bid starting time is now, and Bid closing time 2 weeks later from now.");
}catch (Exception e) {
System.out.println("Error: Invalid input is entered. Going back to the previous menu.");
return false;
}
/* TODO: Your code should come here to store the user inputs in your database */
try{
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO item VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP + INTERVAL '14days', 'OPENED', ?, ?)");
pStmt.setString(1, name);
pStmt.setString(2, category.name());
pStmt.setString(3, condition.name());
pStmt.setString(4, description);
pStmt.setDouble(5, startPrice);
pStmt.setDouble(6, buynowPrice);
pStmt.setString(7, username);
if(useCoupon) {
pStmt.setInt(8, couponId);
}
else {
pStmt.setNull(8, java.sql.Types.INTEGER);
}
pStmt.executeUpdate();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
return false;
}
System.out.println("Your item has been successfully listed.\n");
return true;
}
private static boolean SignupMenu() {
/* 2. Sign Up */
String new_username, userpass, usernick, isAdmin;
System.out.print("----< Sign Up >\n" +
" ** To go back, enter 'back' in user ID.\n" +
"---- user name: ");
try {
new_username = scanner.next();
scanner.nextLine();
if(new_username.equalsIgnoreCase("back")){
return false;
}
System.out.print("---- password: ");
userpass = scanner.next();
scanner.nextLine();
System.out.print("---- nickname: ");
usernick = scanner.next();
scanner.nextLine();
System.out.print("---- In this user an administrator? (Y/N): ");
isAdmin = scanner.next();
scanner.nextLine();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Please select again.");
return false;
}
/* TODO: Your code should come here to create a user account in your database */
try {
PreparedStatement checkStmt = conn.prepareStatement("SELECT COUNT(*) FROM users WHERE user_id = ?");
checkStmt.setString(1, new_username);
ResultSet rset = checkStmt.executeQuery();
if(rset.next() && rset.getInt(1) != 0) {
System.out.println("Error: This user ID is already taken. Please try a different one.");
return false;
}
PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO users VALUES (?, ?, ?, ?)");
insertStmt.setString(1, new_username);
insertStmt.setString(2, userpass);
insertStmt.setString(3, usernick);
insertStmt.setBoolean(4, isAdmin.equalsIgnoreCase("Y"));
insertStmt.executeUpdate();
System.out.println("Your account has been successfully created.\n");
return true;
} catch(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
return false;
}
}
private static boolean AdminMenu() {
/* 3. Login as Administrator */
char choice;
String adminname, adminpass;
String keyword, seller, user;
int percent;
System.out.print("----< Login as Administrator >\n" +
" ** To go back, enter 'back' in user ID.\n" +
"---- admin ID: ");
try {
adminname = scanner.next();
scanner.nextLine();
if(adminname.equalsIgnoreCase("back")){
return false;
}
System.out.print("---- password: ");
adminpass = scanner.nextLine();
// TODO: check the admin's account and password.
PreparedStatement pStmt = conn.prepareStatement("SELECT COUNT(*) FROM users WHERE user_id = ? AND pw = ? AND isAdmin = TRUE");
pStmt.setString(1, adminname);
pStmt.setString(2, adminpass);
ResultSet rset = pStmt.executeQuery();
if (!rset.next() || rset.getInt(1) == 0) {
/* If Login Fails */
System.out.println("Error: Incorrect user name or password, or you are not an administrator");
return false;
}
System.out.println("You are successfully logged in as an administrator.\n");
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
return false;
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
return false;
}
boolean login_success = true;
if(!login_success){
// login failed. go back to the previous menu.
return false;
}
do {
System.out.println(
"----< Admin menu > \n" +
" 1. Print Sold Items per Category \n" +
" 2. Print Sold Items for Seller \n" +
" 3. Print Seller Ranking \n" +
" 4. Print Buyer Ranking \n" +
" 5. Print All Commissions \n" +
" 6. Release Coupon to User \n" +
" P. Go Back to Previous Menu"
);
try {
choice = scanner.next().charAt(0);;
scanner.nextLine();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
continue;
}
if (choice == '1') {
System.out.println("----Enter Category to search : ");
keyword = scanner.next();
scanner.nextLine();
/*TODO: Print Sold Items per Category */
System.out.println("sold item | sold date | seller ID | buyer ID | price | commissions");
System.out.println("-------------------------------------------------------------------------------------------");
String query = "SELECT i.item_name AS sold_item, " +
"b.selected_date AS sold_date, " +
"i.seller_id AS seller_id, " +
"b.bidder_id AS buyer_id, " +
"b.bid_price AS price, " +
"FLOOR(CASE " +
" WHEN i.seller_coupon_id IS NOT NULL THEN b.bid_price * 0.1 * (1 - c.percent / 100) " +
" ELSE b.bid_price * 0.1 " +
"END) AS commissions " +
"FROM item i " +
"JOIN bid b ON i.item_id = b.item_id " +
"LEFT JOIN coupon c ON i.seller_coupon_id = c.coupon_id " +
"WHERE i.category = ? " +
"AND i.bidding_status = 'TENDERED' " +
"AND b.selected = TRUE";
try {
PreparedStatement pStmt = conn.prepareStatement(query);
pStmt.setString(1, keyword);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-15s | %-19s | %-10s | %-10s | %-10.2f | %-10d\n",
rset.getString("sold_item"), rset.getTimestamp("sold_date").toString(), rset.getString("seller_id"), rset.getString("buyer_id"), rset.getDouble("price"), rset.getInt("commissions"));
}
} catch(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
continue;
} else if (choice == '2') {
/*TODO: Print Sold Items for Seller */
System.out.println("---- Enter Seller ID to search : ");
seller = scanner.next();
scanner.nextLine();
System.out.println("sold item | sold date | buyer ID | price | commissions");
System.out.println("-----------------------------------------------------------------------------");
String query = "SELECT i.item_name AS sold_item, " +
"b.selected_date AS sold_date, " +
"b.bidder_id AS buyer_id, " +
"b.bid_price AS price, " +
"FLOOR(CASE " +
" WHEN i.seller_coupon_id IS NOT NULL THEN b.bid_price * 0.1 * (1 - c.percent / 100) " +
" ELSE b.bid_price * 0.1 " +
"END) AS commissions " +
"FROM item i " +
"JOIN bid b ON i.item_id = b.item_id " +
"LEFT JOIN coupon c ON i.seller_coupon_id = c.coupon_id " +
"WHERE i.seller_id = ? " +
"AND i.bidding_status = 'TENDERED' " +
"AND b.selected = TRUE";
try {
PreparedStatement pStmt = conn.prepareStatement(query);
pStmt.setString(1, seller);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-15s | %-19s | %-10s | %-10.2f | %-10d\n",
rset.getString("sold_item"), rset.getTimestamp("sold_date").toString(), rset.getString("buyer_id"), rset.getDouble("price"), rset.getInt("commissions"));
}
} catch(SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
continue;
} else if (choice == '3') {
/*TODO: Print Seller Ranking */
System.out.println("seller ID | # of items sold | Total Profit (excluding commissions)");
System.out.println("--------------------------------------------------------------------");
String query = "SELECT u.user_id AS seller_id, " +
"COUNT(a.amount) AS items_sold, " +
"SUM(a.amount) AS total_profit " +
"FROM users u " +
"JOIN account a ON u.user_id = a.user_id " +
"WHERE a.amount > 0 " +
"GROUP BY u.user_id " +
"ORDER BY total_profit DESC";
try {
PreparedStatement pStmt = conn.prepareStatement(query);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-11s | %-15d | %-35.2f\n", rset.getString("seller_id"), rset.getInt("items_sold"), rset.getDouble("total_profit"));
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
continue;
} else if (choice == '4') {
/*TODO: Print Buyer Ranking */
System.out.println("buyer ID | # of items purchased | Total Money Spent ");
System.out.println("------------------------------------------------------");
String query = "SELECT u.user_id AS buyer_id, " +
"COUNT(a.amount) AS items_purchased, " +
"ABS(SUM(a.amount)) AS total_spent " +
"FROM users u " +
"JOIN account a ON u.user_id = a.user_id " +
"WHERE a.amount < 0 " +
"GROUP BY u.user_id " +
"ORDER BY total_spent DESC";
try {
PreparedStatement pStmt = conn.prepareStatement(query);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-10s | %-20d | %-17.2f\n", rset.getString("buyer_id"), rset.getInt("item_purchased"), rset.getDouble("total_spent"));
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
continue;
} else if (choice == '5') {
/*TODO: Print All Commissions */
System.out.println("bid ID | tendered date | commissions");
System.out.println("------------------------------------------");
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT bid_id, tendered_date, commission_amount FROM commission ORDER BY tendered_date DESC");
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-7d | %-19s | %-12d\n", rset.getInt("bid_id"), rset.getTimestamp("tendered_date").toString(), rset.getInt("commission_amount"));
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
continue;
} else if (choice == '6') {
/*TODO: Release Coupon to User */
System.out.println("---- Enter User ID to release coupon : ");
user = scanner.next();
scanner.nextLine();
System.out.println("---- Enter what percent coupon you will release : ");
percent = scanner.nextInt();
scanner.nextLine();
try {
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO coupon VALUES (DEFAULT, ?, ?, 'USABLE', CURRENT_DATE, CURRENT_DATE + INTERVAL '30 days')");
pStmt.setInt(1, percent);
pStmt.setString(2, user);
pStmt.executeUpdate();
System.out.println("Coupon has been successfully released.\n");
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
continue;
} else if (choice == 'P' || choice == 'p') {
return false;
} else {
System.out.println("Error: Invalid input is entered. Try again.");
continue;
}
} while(true);
}
public static void CheckSellStatus(){
/* TODO: Check the status of the item the current user is selling */
System.out.println("item listed in Auction | bidder (buyer ID) | bidding price | bidding date/time \n");
System.out.println("-------------------------------------------------------------------------------\n");
String query = "SELECT i.item_name, b.bidder_id, b.bid_price, b.bid_date FROM item i LEFT JOIN bid b ON i.item_id = b.item_id WHERE i.seller_id = ? AND i.bidding_status = 'OPENED'";
try {
PreparedStatement pStmt = conn.prepareStatement(query);
pStmt.setString(1, username);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-20s | %-15s | %-13s | %-20s\n", rset.getString("item_name"), rset.getString("bidder_id") != null ? rset.getString("bidder_id") : "NONE", rset.getObject("bid_price") != null ? String.format("%.2f", rset.getDouble("bid_price")) : "NONE", rset.getTimestamp("bid_date") != null ? rset.getTimestamp("bid_date").toString() : "NONE");
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
}
public static boolean BuyItem(){
Category category = null;
Condition condition = null;
char choice;
double price, priorPrice = 0, buynowPrice = 0;
String keyword, seller, datePosted, priorBidder = "NONE", itemname = null;
boolean flag_catg = true, flag_cond = true;
do {
System.out.println( "----< Select category > : \n" +
" 1. Electronics\n"+
" 2. Books\n" +
" 3. Home\n" +
" 4. Clothing\n" +
" 5. Sporting Goods\n" +
" 6. Other categories\n" +
" 7. Any category\n" +
" P. Go Back to Previous Menu"
);
try {
choice = scanner.next().charAt(0);;
scanner.nextLine();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
return false;
}
flag_catg = true;
switch (choice) {
case '1':
category = Category.ELECTRONICS;
break;
case '2':
category = Category.BOOKS;
break;
case '3':
category = Category.HOME;
break;
case '4':
category = Category.CLOTHING;
break;
case '5':
category = Category.SPORTINGGOODS;
break;
case '6':
category = Category.OTHERS;
break;
case '7':
break;
case 'p':
case 'P':
return false;
default:
System.out.println("Error: Invalid input is entered. Try again.");
flag_catg = false;
continue;
}
} while(!flag_catg);
do {
System.out.println(
"----< Select the condition > \n" +
" 1. New\n" +
" 2. Like-new\n" +
" 3. Used (Good)\n" +
" 4. Used (Acceptable)\n" +
" P. Go Back to Previous Menu"
);
try {
choice = scanner.next().charAt(0);;
scanner.nextLine();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
return false;
}
flag_cond = true;
switch (choice) {
case '1':
condition = Condition.NEW;
break;
case '2':
condition = Condition.LIKE_NEW;
break;
case '3':
condition = Condition.GOOD;
break;
case '4':
condition = Condition.ACCEPTABLE;
break;
case 'p':
case 'P':
return false;
default:
System.out.println("Error: Invalid input is entered. Try again.");
flag_cond = false;
continue;
}
} while(!flag_cond);
try {
System.out.println("---- Enter item keyword to search the name : ");
keyword = scanner.next();
scanner.nextLine();
System.out.println("---- Enter Seller ID to search : ");
System.out.println(" ** Enter 'any' if you want to see items from any seller. ");
seller = scanner.next();
scanner.nextLine();
System.out.println("---- Enter time posted (YYYY-MM-DD HH:MM:SS): ");
System.out.println(" ** This will search items that have been posted after the designated time.");
datePosted = scanner.next();
scanner.nextLine();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
return false;
}
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT i.item_id, i.item_name, i.seller_id, i.closing_date, i.bidding_status, b.bid_id FROM item i LEFT JOIN bid b ON i.item_id = b.item_id WHERE bidding_status = 'OPENED'");
ResultSet rset = pStmt.executeQuery();
while(rset.next()) {
LocalDateTime closingDate = rset.getTimestamp("closing_date").toLocalDateTime();
LocalDateTime now = LocalDateTime.now();
if(now.isAfter(closingDate)) {
if(rset.getInt("bid_id") == 0) {
PreparedStatement statusStmt = conn.prepareStatement("Update item SET bidding_status = 'CLOSED' WHERE item_id = ?");
statusStmt.setInt(1, rset.getInt("item_id"));
statusStmt.executeUpdate();
}
else {
closeBid(rset.getInt("item_id"), rset.getString("item_name"), rset.getString("seller_id"));
}
}
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
/* TODO: Query condition: item category */
/* TODO: Query condition: item condition */
/* TODO: Query condition: items whose name match the keyword (use LIKE operator) */
/* TODO: Query condition: items from a particular seller */
/* TODO: Query condition: posted time of item */
String selectQuery = "SELECT DISTINCT i.item_id AS item_id, i.item_name AS item_name, " +
"i.condition AS condition, i.seller_id AS seller, " +
"i.buy_now_price AS buy_it_now, b.prior_price AS current_bid, " +
"b.prior_bidder_id AS highest_bidder, TO_CHAR(JUSTIFY_HOURS(i.closing_date - NOW()), 'HH24:MI:SS') AS time_left, " +
"i.closing_date AS bid_close , i.bidding_status AS bid_status " +
"FROM item i LEFT JOIN bid b ON i.item_id = b.item_id " +
"WHERE (? = 'ANY' OR i.category = ?) AND i.condition = ? " +
"AND i.item_name ILIKE '%' || ? || '%' AND (? = 'any' OR i.seller_id = ?) " +
"AND i.posted_date >= TO_TIMESTAMP(?, 'YYYY-MM-DD HH24:MI:SS') AND i.bidding_status = 'OPENED' " +
"ORDER BY i.closing_date ASC";
/* TODO: List all items that match the query condition */
System.out.println("Item ID | Item name | Condition | Seller | Buy-It-Now | Current Bid | highest bidder | Time left | bid close ");
System.out.println("--------------------------------------------------------------------------------------------------------------------------------");
try {
PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
selectStmt.setString(1, category == null ? "ANY" : category.name());
selectStmt.setString(2, category == null ? "ANY" : category.name());
selectStmt.setString(3, condition.name());
selectStmt.setString(4, keyword);
selectStmt.setString(5, seller);
selectStmt.setString(6, seller);
selectStmt.setString(7, datePosted);
ResultSet rset = selectStmt.executeQuery();
while(rset.next()){
System.out.printf("%-7d | %-16s | %-9s | %-6s | %-10.2f | %-11.2f | %-14s | %-12s | %-19s\n", rset.getInt("item_id"), rset.getString("item_name"), rset.getString("condition"), rset.getString("seller"), rset.getDouble("buy_it_now"), rset.getDouble("current_bid"), rset.getString("highest_bidder"), rset.getString("time_left"), rset.getTimestamp("bid_close").toString());
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
System.out.println("---- Select Item ID to buy or bid: ");
int itemId;
try {
itemId = scanner.nextInt();
scanner.nextLine();
System.out.println(" Price: ");
price = scanner.nextDouble();
scanner.nextLine();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.");
return false;
}
/* TODO: Buy-it-now or bid: If the entered price is higher or equal to Buy-It-Now price, the bid ends. */
/* Even if the bid price is higher than the Buy-It-Now price, the buyer pays the B-I-N price. */
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT i.item_id, i.item_name, i.seller_id, i.closing_date, i.bidding_status, b.bid_id FROM item i LEFT JOIN bid b ON i.item_id = b.item_id WHERE bidding_status = 'OPENED' AND i.item_id = ?");
pStmt.setInt(1, itemId);
ResultSet rset = pStmt.executeQuery();
int closeFlag = 0;
while(rset.next()) {
LocalDateTime closingDate = rset.getTimestamp("closing_date").toLocalDateTime();
LocalDateTime now = LocalDateTime.now();
if(now.isAfter(closingDate)) {
if(rset.getInt("bid_id") == 0) {
PreparedStatement statusStmt = conn.prepareStatement("Update item SET bidding_status = 'CLOSED' WHERE item_id = ?");
statusStmt.setInt(1, rset.getInt("item_id"));
statusStmt.executeUpdate();
}
else {
closeBid(rset.getInt("item_id"), rset.getString("item_name"), rset.getString("seller_id"));
}
closeFlag = 1;
}
}
if (closeFlag == 1) {
System.out.println("Bid Ended.");
return false;
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
try {
PreparedStatement selectStmt = conn.prepareStatement("SELECT bid_price, bidder_id FROM bid WHERE item_id = ? ORDER BY bid_price DESC LIMIT 1");
selectStmt.setInt(1, itemId);
ResultSet rset = selectStmt.executeQuery();
if(rset.next()) {
priorPrice = rset.getDouble("bid_price");
priorBidder = rset.getString("bidder_id");
}
PreparedStatement insertStmt = conn.prepareStatement("INSERT INTO bid (item_id, bidder_id, bid_price, bid_date, prior_bidder_id, prior_price) VALUES (?, ?, ?, NOW(), ?, ?)");
insertStmt.setInt(1, itemId);
insertStmt.setString(2, username);
insertStmt.setDouble(3, price);
insertStmt.setString(4, priorBidder);
insertStmt.setDouble(5, priorPrice);
insertStmt.executeUpdate();
PreparedStatement checkStmt = conn.prepareStatement("SELECT item_name, buy_now_price FROM item WHERE item_id = ?");
checkStmt.setInt(1, itemId);
rset = checkStmt.executeQuery();
if (rset.next()) {
itemname = rset.getString("item_name");
buynowPrice = rset.getDouble("buy_now_price");
}
if(price > priorPrice) {
PreparedStatement updateStmt = conn.prepareStatement("UPDATE bid SET prior_price = ?, prior_bidder_id = ? WHERE item_id = ?");
updateStmt.setDouble(1, price);
updateStmt.setString(2, username);
updateStmt.setInt(3, itemId);
updateStmt.executeUpdate();
}
if(price > priorPrice && price < buynowPrice) {
/* TODO: if you are the current highest bidder, print the following */
System.out.println("Congratulations, you are the highest bidder.\n");
return true;
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
return false;
}
if(price >= buynowPrice) {
closeBid(itemId, itemname, seller);
/* TODO: if you won, print the following */
System.out.println("Congratulations, the item is yours now.\n");
}
return true;
}
public static void CheckBuyStatus(){
/* TODO: Check the status of the item the current buyer is bidding on */
/* Even if you are outbidded or the bid closing date has passed, all the items this user has bidded on must be displayed */
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT i.item_id, i.item_name, i.seller_id, i.closing_date, i.bidding_status, b.bid_id FROM item i LEFT JOIN bid b ON i.item_id = b.item_id WHERE bidding_status = 'OPENED'");
ResultSet rset = pStmt.executeQuery();
while(rset.next()) {
LocalDateTime closingDate = rset.getTimestamp("closing_date").toLocalDateTime();
LocalDateTime now = LocalDateTime.now();
if(now.isAfter(closingDate)) {
if(rset.getInt("bid_id") == 0) {
PreparedStatement statusStmt = conn.prepareStatement("Update item SET bidding_status = 'CLOSED' WHERE item_id = ?");
statusStmt.setInt(1, rset.getInt("item_id"));
statusStmt.executeUpdate();
}
else {
closeBid(rset.getInt("item_id"), rset.getString("item_name"), rset.getString("seller_id"));
}
}
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
System.out.println("item ID | item name | highest bidder | highest bidding price | your bidding price | bid closing date/time");
System.out.println("--------------------------------------------------------------------------------------------------------------------");
String query = "SELECT i.item_id AS item_id, i.item_name AS item_name, " +
"b.prior_bidder_id AS highest_bidder, b.prior_price AS highest_bidding_price, " +
"b.bid_price AS your_bidding_price, i.closing_date AS bid_closing_date " +
"FROM bid b " +
"JOIN item i ON b.item_id = i.item_id " +
"WHERE b.bidder_id = ? " +
"ORDER BY i.closing_date DESC";
try {
PreparedStatement pStmt = conn.prepareStatement(query);
pStmt.setString(1, username);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-10d | %-18s | %-15s | %-22.2f | %-18.2f | %-20s\n", rset.getInt("item_id"), rset.getString("item_name"), rset.getString("highest_bidder"), rset.getDouble("highest_bidding_price"), rset.getDouble("your_bidding_price"), rset.getTimestamp("bid_closing_date").toString());
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
}
public static void CheckAccount(){
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT i.item_id, i.item_name, i.seller_id, i.closing_date, i.bidding_status, b.bid_id FROM item i LEFT JOIN bid b ON i.item_id = b.item_id WHERE bidding_status = 'OPENED'");
ResultSet rset = pStmt.executeQuery();
while(rset.next()) {
LocalDateTime closingDate = rset.getTimestamp("closing_date").toLocalDateTime();
LocalDateTime now = LocalDateTime.now();
if(now.isAfter(closingDate)) {
if(rset.getInt("bid_id") == 0) {
PreparedStatement statusStmt = conn.prepareStatement("Update item SET bidding_status = 'CLOSED' WHERE item_id = ?");
statusStmt.setInt(1, rset.getInt("item_id"));
statusStmt.executeUpdate();
}
else {
closeBid(rset.getInt("item_id"), rset.getString("item_name"), rset.getString("seller_id"));
}
}
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
/* TODO: Check the balance of the current user. */
System.out.println("[Sold Items] \n");
System.out.println("item category | item name | sold date | sold price | buyer ID | commission ");
System.out.println("------------------------------------------------------------------------------");
String plusQuery = "SELECT i.category AS item_category, i.item_name AS item_name, " +
"c.tendered_date AS sold_date, b.prior_price AS sold_price, " +
"b.prior_bidder_id AS buyer_id, c.commission_amount AS commission " +
"FROM item i " +
"JOIN bid b ON i.item_id = b.item_id AND b.selected = TRUE " +
"INNER JOIN commission c ON b.bid_id = c.bid_id " +
"WHERE i.seller_id = ? AND i.bidding_status = 'TENDERED' " +
"ORDER BY b.selected_date DESC";
try {
PreparedStatement pStmt = conn.prepareStatement(plusQuery);
pStmt.setString(1, username);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-15s | %-10s | %-10s | %-19.2f | %-8s | %-10d\n", rset.getString("item_category"), rset.getString("item_name"), rset.getTimestamp("sold_date").toString(), rset.getDouble("sold_price"), rset.getString("buyer_id"), rset.getObject("commission") != null ? rset.getInt("commission") : 0);
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
System.out.println("[Purchased Items] \n");
System.out.println("item category | item name | purchased date | puchased price | seller ID ");
System.out.println("--------------------------------------------------------------------------");
String minusQuery = "SELECT DISTINCT i.category AS item_category, i.item_name AS item_name, " +
"b.selected_date AS purchased_date, b.prior_price AS purchased_price, " +
"i.seller_id AS seller_id " +
"FROM item i " +
"JOIN bid b ON i.item_id = b.item_id AND b.selected = TRUE " +
"WHERE b.prior_bidder_id = ? " +
"ORDER BY b.selected_date DESC";
try{
PreparedStatement pStmt = conn.prepareStatement(minusQuery);
pStmt.setString(1, username);
ResultSet rset = pStmt.executeQuery();
while(rset.next()){
System.out.printf("%-15s | %-10s | %-19s | %-15.2f | %-10s\n", rset.getString("item_category"), rset.getString("item_name"), rset.getTimestamp("purchased_date").toString(), rset.getDouble("purchased_price"), rset.getString("seller_id"));
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
}
}
public static void CheckCoupon() {
System.out.println("[Your Coupons] \n");
System.out.println("Percent of coupon | available | issue date | expire date ");
System.out.println("----------------------------------------------------------");
try {
PreparedStatement pStmt = conn.prepareStatement("SELECT percent, available, issue_date, expire_date FROM coupon WHERE owner_id = ? ");
pStmt.setString(1, username);
ResultSet rset = pStmt.executeQuery();
while(rset.next()) {
System.out.printf("%-17d | %-9s | %-10s | %-11s\n", rset.getInt("percent"), rset.getString("available"), rset.getDate("issue_date").toString(), rset.getDate("expire_date").toString());
}
} catch (SQLException e) {