-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuction.java
More file actions
1376 lines (1226 loc) · 40.6 KB
/
Auction.java
File metadata and controls
1376 lines (1226 loc) · 40.6 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.*;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util. *;
public class Auction {
private static final Scanner scanner = new Scanner(System.in);
private static String username;
private static Connection conn;
enum Category {
ELECTRONICS,
BOOKS,
HOME,
CLOTHING,
SPORTINGGOODS,
OTHERS
}
enum Condition {
NEW,
LIKE_NEW,
GOOD,
ACCEPTABLE
}
private static void HandleAuctionClosure() {
/* 입찰 시간이 종료(즉, 경매 마감)되었지만, 경매 상태가 변경되지 않은 경매를 처리 */
// 1. 경매가 마감되었지만, 경매 상태가 'LISTED'거나 'BIDDING'인 경매 선택
// 2. 경매 상태를 각각 'LISTED'인 경우 'EXPIRED'로, 'BIDDING'인 경우 'SOLD'로 변경
// 3. 경매 상태를 'SOLD'로 변경한 경우, 최고가 입찰자의 상태('ACTIVE')를 'WON'으로 변경
// 4. 청구서 작성
String status, statement, seller_id, bidder_id;
long auction_id, item_id, bid_id;
int bid_price;
try {
conn.setAutoCommit(false);
try (PreparedStatement p = conn.prepareStatement(
"SELECT auction_id, item_id, bid_end_time, status " +
"FROM auctions " +
"WHERE bid_end_time < CURRENT_TIMESTAMP AND (status = 'LISTED' OR status = 'BIDDING')"
)) {
try (ResultSet auction_rset = p.executeQuery()) {
while (auction_rset.next()) {
auction_id = auction_rset.getLong("auction_id");
item_id = auction_rset.getLong("item_id");
status = auction_rset.getString("status");
if (status.equals("LISTED"))
statement = "UPDATE auctions SET status = 'EXPIRED' WHERE auction_id = ?";
else // 'BIDDING'
statement = "UPDATE auctions SET status = 'SOLD' WHERE auction_id = ?";
try (PreparedStatement pStmt = conn.prepareStatement(statement)) {
pStmt.setLong(1, auction_id);
if (pStmt.executeUpdate() == 0) throw new SQLException();
}
if (status.equals("LISTED")) continue;
// 최고가 입찰자의 입찰서
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT bid_id, bidder_id, bid_price " +
"FROM bids " +
"WHERE auction_id = ? " +
"ORDER BY bid_price DESC, bid_time ASC " +
"LIMIT 1"
)) {
pStmt.setLong(1, auction_id);
try (ResultSet bid_rset = pStmt.executeQuery()) {
if (!bid_rset.next()) throw new SQLException();
bid_id = bid_rset.getLong("bid_id");
bidder_id = bid_rset.getString("bidder_id");
bid_price = bid_rset.getInt("bid_price");
}
}
try (PreparedStatement pStmt = conn.prepareStatement(
"UPDATE bids SET bid_status = 'WON' WHERE bid_id = ?"
)) {
pStmt.setLong(1, bid_id);
if (pStmt.executeUpdate() == 0) throw new SQLException();
}
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT seller_id FROM items WHERE item_id = ?"
)) {
pStmt.setLong(1, item_id);
try (ResultSet item_ret = pStmt.executeQuery()) {
item_ret.next();
seller_id = item_ret.getString("seller_id");
}
}
// 청구서 작성
try (PreparedStatement pStmt = conn.prepareStatement(
"INSERT INTO billings (item_id, buyer_id, seller_id, final_price, transaction_time) " +
"VALUES (?, ?, ?, ?, ?)"
)) {
pStmt.setLong(1, item_id);
pStmt.setString(2, bidder_id);
pStmt.setString(3, seller_id);
pStmt.setInt(4, bid_price);
pStmt.setTimestamp(5, auction_rset.getTimestamp("bid_end_time"));
if (pStmt.executeUpdate() == 0) throw new SQLException();
}
conn.commit();
}
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
try {
conn.rollback();
} catch (SQLException rollbackEx) {
System.out.println(rollbackEx.getMessage());
}
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
private static void LoginMenu() {
String userpass;
System.out.print(
"----< User Login > \n" +
" ** To go back, enter 'back' in user ID. \n" +
" user ID: "
);
username = scanner.next();
scanner.nextLine();
if(username.equalsIgnoreCase("back")) {
System.out.println();
return;
}
System.out.print(" password: ");
userpass = scanner.next();
scanner.nextLine();
/* TODO: Your code should come here to check ID and password */
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT 1 " +
"FROM users " +
"WHERE user_id = ? AND password = ?"
)) {
pStmt.setString(1, username);
pStmt.setString(2, userpass);
try (ResultSet rset = pStmt.executeQuery()) {
if (!rset.next()) throw new SQLException();
}
} catch (SQLException e) {
System.out.println("Error: Incorrect user name or password\n");
username = null;
return;
}
System.out.println("You are successfully logged in.\n");
}
private static void SignupMenu() {
boolean is_admin;
String new_username, userpass, 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;
System.out.print("---- password: ");
userpass = scanner.next();
scanner.nextLine();
System.out.print("---- In this user an administrator? (Y/N): ");
isAdmin = scanner.next();
scanner.nextLine();
if (isAdmin.equalsIgnoreCase("Y") || isAdmin.equalsIgnoreCase("YES"))
is_admin = true;
else if (isAdmin.equalsIgnoreCase("N") || isAdmin.equalsIgnoreCase("NO"))
is_admin = false;
else throw new InputMismatchException();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Please select again.\n");
return;
}
/* TODO: Your code should come here to create a user account in your database */
try (PreparedStatement pStmt = conn.prepareStatement(
"INSERT INTO users VALUES(?, ?, ?)"
)) {
pStmt.setString(1, new_username);
pStmt.setString(2, userpass);
pStmt.setBoolean(3, is_admin);
if (pStmt.executeUpdate() == 0) throw new SQLException();
} catch (SQLException e) {
System.out.println("Error: Sign up failed. Please select again.\n");
return;
}
System.out.println("Your account has been successfully created.\n");
}
private static void AdminMenu() {
String adminname, adminpass;
System.out.print(
"----< Login as Administrator >\n" +
" ** To go back, enter 'back' in user ID.\n" +
"---- admin ID: "
);
adminname = scanner.next();
scanner.nextLine();
if (adminname.equalsIgnoreCase("back")) {
System.out.println();
return;
}
System.out.print("---- password: ");
adminpass = scanner.next();
scanner.nextLine();
/* TODO: check the admin's account and password. */
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT 1 " +
"FROM users " +
"WHERE user_id = ? AND password = ? AND is_admin = TRUE"
)) {
pStmt.setString(1, adminname);
pStmt.setString(2, adminpass);
try (ResultSet rset = pStmt.executeQuery()) {
if (!rset.next()) throw new SQLException();
}
} catch (SQLException e) {
System.out.println();
return; // login failed. go back to the previous menu.
}
System.out.println();
String category, seller;
char choice;
do {
System.out.println(
"----< Admin menu > \n" +
" 1. Print Sold Items per Category \n" +
" 2. Print Account Balance for Seller \n" +
" 3. Print Seller Ranking \n" +
" 4. Print Buyer Ranking \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.\n");
continue;
}
System.out.println();
LocalDateTime dateTime;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
if (choice == '1') {
/* TODO: Print Sold Items per Category */
HandleAuctionClosure(); // 경매 마감 확인
System.out.print("----Enter Category to search : ");
category = scanner.next();
scanner.nextLine();
try {
Category.valueOf(category);
} catch (IllegalArgumentException e) {
System.out.println("Error: Invalid input is entered. Try again.\n");
continue;
}
System.out.println();
System.out.println("sold item | sold date | seller ID | buyer ID | price");
System.out.println("--------------------------------------------------------------------");
String description, buyer_id, seller_id;
long item_id;
int final_price;
try (PreparedStatement p = conn.prepareStatement(
"SELECT item_id, description " +
"FROM items NATURAL JOIN auctions " +
"WHERE status = 'SOLD' AND category = ?"
)) {
p.setString(1, category);
try (ResultSet item_rset = p.executeQuery()) {
while (item_rset.next()) {
item_id = item_rset.getLong("item_id");
description = item_rset.getString("description");
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT buyer_id, seller_id, final_price, transaction_time " +
"FROM billings " +
"WHERE item_id = ?"
)) {
pStmt.setLong(1, item_id);
try (ResultSet rset = pStmt.executeQuery()) {
if (!rset.next()) throw new SQLException();
buyer_id = rset.getString("buyer_id");
seller_id = rset.getString("seller_id");
final_price = rset.getInt("final_price");
dateTime = rset.getTimestamp("transaction_time").toLocalDateTime();
}
}
System.out.println(
description + " | " +
dateTime.format(formatter) + " | " +
seller_id + " | " +
buyer_id + " | " +
final_price
);
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
return;
}
System.out.println();
} else if (choice == '2') {
/* TODO: Print Account Balance for Seller */
HandleAuctionClosure(); // 경매 마감 확인
System.out.print("---- Enter Seller ID to search : ");
seller = scanner.next();
scanner.nextLine();
System.out.println();
System.out.println("sold item | sold date | buyer ID | price");
System.out.println("------------------------------------------------------");
String description, buyer_id;
int final_price;
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT description, buyer_id, final_price, transaction_time " +
"FROM items NATURAL JOIN billings " +
"WHERE seller_id = ?"
)) {
pStmt.setString(1, seller);
try (ResultSet rset = pStmt.executeQuery()) {
while (rset.next()) {
description = rset.getString("description");
buyer_id = rset.getString("buyer_id");
final_price = rset.getInt("final_price");
dateTime = rset.getTimestamp("transaction_time").toLocalDateTime();
System.out.println(
description + " | " +
dateTime.format(formatter) + " | " +
buyer_id + " | " +
final_price
);
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
return;
}
System.out.println();
} else if (choice == '3') {
/* TODO: Print Seller Ranking */
HandleAuctionClosure(); // 경매 마감 확인
System.out.println("seller ID | # of items sold | Total Profit");
System.out.println("--------------------------------------------");
String seller_id;
long item_num, total_profit;
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT seller_id, COUNT(*) AS item_num, SUM(final_price) AS total_profit " +
"FROM billings " +
"GROUP BY seller_id " +
"ORDER BY total_profit DESC, item_num DESC"
)) {
try (ResultSet rset = pStmt.executeQuery()) {
while (rset.next()) {
seller_id = rset.getString("seller_id");
item_num = rset.getLong("item_num");
total_profit = rset.getLong("total_profit");
System.out.println(
seller_id + " | " +
item_num + " | " +
total_profit
);
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
return;
}
// 판매 기록이 없는 유저도 출력
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT u.user_id " +
"FROM users u LEFT JOIN billings b ON u.user_id = b.seller_id " +
"WHERE b.seller_id IS NULL"
)) {
try (ResultSet rset = pStmt.executeQuery()) {
while (rset.next()) {
seller_id = rset.getString("user_id");
System.out.println(seller_id + " | 0 | 0");
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
return;
}
System.out.println();
} else if (choice == '4') {
/* TODO: Print Buyer Ranking */
HandleAuctionClosure(); // 경매 마감 확인
System.out.println("buyer ID | # of items purchased | Total Money Spent");
System.out.println("-----------------------------------------------------");
String buyer_id;
long item_num, total_spent;
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT buyer_id, COUNT(*) AS item_num, SUM(final_price) AS total_spent " +
"FROM billings " +
"GROUP BY buyer_id " +
"ORDER BY total_spent DESC, item_num DESC"
)) {
try (ResultSet rset = pStmt.executeQuery()) {
while (rset.next()) {
buyer_id = rset.getString("buyer_id");
item_num = rset.getLong("item_num");
total_spent = rset.getLong("total_spent");
System.out.println(
buyer_id + " | " +
item_num + " | " +
total_spent + " | "
);
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
return;
}
// 구매 기록이 없는 유저도 출력
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT u.user_id " +
"FROM users u LEFT JOIN billings b ON u.user_id = b.buyer_id " +
"WHERE b.buyer_id IS NULL"
)) {
try (ResultSet rset = pStmt.executeQuery()) {
while (rset.next()) {
buyer_id = rset.getString("user_id");
System.out.println(buyer_id + " | 0 | 0");
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
return;
}
System.out.println();
} else if (choice == 'P' || choice == 'p') {
System.out.println();
return;
} else
System.out.println("Error: Invalid input is entered. Try again.\n");
} while (true);
}
private static void SellMenu() {
Category category = null;
Condition condition = null;
LocalDateTime dateTime;
String description;
int start_price, BIN_price;
char choice;
boolean flag_catg = true, flag_cond = true;
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.\n");
continue;
}
flag_catg = true;
switch ((int) 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 'p':
case 'P':
return;
default:
System.out.println("Error: Invalid input is entered. Try again.\n");
flag_catg = false;
}
} while (!flag_catg);
System.out.println();
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.\n");
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;
default:
System.out.println("Error: Invalid input is entered. Try again.\n");
flag_cond = false;
}
} while (!flag_cond);
System.out.println();
try {
System.out.print("---- Description of the item (one line): ");
description = scanner.nextLine();
System.out.print("---- Starting price: ");
while (!scanner.hasNextInt()) {
scanner.next();
System.out.println("Invalid input is entered. Please enter Starting price: ");
}
start_price = scanner.nextInt();
scanner.nextLine();
System.out.print("---- Buy-It-Now price: ");
while (!scanner.hasNextInt()) {
scanner.next();
System.out.println("Invalid input is entered. Please enter Buy-It-Now price: ");
}
BIN_price = scanner.nextInt();
scanner.nextLine();
if (start_price > BIN_price) throw new Exception();
System.out.print("---- Bid closing date and time (YYYY-MM-DD HH:MM): ");
// you may assume users always enter valid date/time
String date = scanner.nextLine(); // "2023-03-04 11:30"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
dateTime = LocalDateTime.parse(date, formatter);
if (dateTime.isBefore(LocalDateTime.now())) throw new Exception();
} catch (Exception e) {
System.out.println("Error: Invalid input is entered. Going back to the previous menu.\n");
return;
}
/* TODO: Your code should come here to store the user inputs in your database */
long item_id, auction_id;
try {
conn.setAutoCommit(false);
try (PreparedStatement pStmt = conn.prepareStatement(
"INSERT INTO items (category, description, condition, seller_id, auction_id) " +
"VALUES (?, ?, ?, ?, NULL)",
Statement.RETURN_GENERATED_KEYS // for auto-generated item_id
)) {
pStmt.setString(1, category.name());
pStmt.setString(2, description);
pStmt.setString(3, condition.name());
pStmt.setString(4, username);
pStmt.executeUpdate();
try (ResultSet rset = pStmt.getGeneratedKeys()) {
if (rset.next()) item_id = rset.getLong(1);
else throw new SQLException();
}
}
try (PreparedStatement pStmt = conn.prepareStatement(
"INSERT INTO auctions (item_id, starting_price, current_price, buy_it_now_price, bid_end_time) " +
"VALUES (?, ?, ?, ?, ?)",
Statement.RETURN_GENERATED_KEYS // for auto-generated item_id
)) {
pStmt.setLong(1, item_id);
pStmt.setInt(2, start_price);
pStmt.setInt(3, start_price);
pStmt.setInt(4, BIN_price);
pStmt.setTimestamp(5, Timestamp.valueOf(dateTime));
pStmt.executeUpdate();
try (ResultSet rset = pStmt.getGeneratedKeys()) {
if (rset.next()) auction_id = rset.getLong(1);
else throw new SQLException();
}
}
try (PreparedStatement pStmt = conn.prepareStatement(
"UPDATE items SET auction_id = ? WHERE item_id = ?"
)) {
pStmt.setLong(1, auction_id);
pStmt.setLong(2, item_id);
if (pStmt.executeUpdate() == 0) throw new SQLException();
}
conn.commit();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException rollbackEx) {
System.out.println(rollbackEx.getMessage());
}
System.out.println("Error: Sell item failed. Please select again.\n");
return;
} finally {
try {
conn.setAutoCommit(true);
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
System.out.println("Your item has been successfully listed.\n");
}
public static void CheckSellStatus() {
/* TODO: Check the status of the item the current user is selling */
HandleAuctionClosure(); // 경매 마감 확인
System.out.println("item listed in Auction | status | bidder (buyer ID) | bidding price | bidding date/time");
System.out.println("---------------------------------------------------------------------------------------");
Timestamp bid_time;
String description, status, bidder_id;
long auction_id;
int bid_price;
try (PreparedStatement p = conn.prepareStatement(
"SELECT description, auction_id, status " +
"FROM items NATURAL JOIN auctions " +
"WHERE seller_id = ?"
)) {
p.setString(1, username);
try (ResultSet item_rset = p.executeQuery()) {
while (item_rset.next()) {
description = item_rset.getString("description");
auction_id = item_rset.getLong("auction_id");
status = item_rset.getString("status");
System.out.print(description + " | " + status);
if (status.equals("LISTED") || status.equals("EXPIRED")) {
System.out.println();
continue;
}
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT bidder_id, bid_price, bid_time " +
"FROM bids " +
"WHERE auction_id = ? " +
"ORDER BY bid_price DESC, bid_time ASC " +
"LIMIT 1"
)) {
pStmt.setLong(1, auction_id);
try (ResultSet rset = pStmt.executeQuery()) {
rset.next();
bidder_id = rset.getString("bidder_id");
bid_price = rset.getInt("bid_price");
bid_time = rset.getTimestamp("bid_time");
}
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = bid_time.toLocalDateTime();
System.out.println(" | " + bidder_id + " | " + bid_price + " | " + dateTime.format(formatter));
}
System.out.println();
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
}
}
public static void BuyItem() {
Category category = null;
Condition condition = null;
LocalDateTime date;
String keyword, seller, datePosted;
char choice;
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.\n");
return;
}
System.out.println();
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': // any category
break;
case 'p':
case 'P':
return;
default:
System.out.println("Error: Invalid input is entered. Try again.\n");
flag_catg = false;
}
} 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.\n");
return;
}
System.out.println();
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;
default:
System.out.println("Error: Invalid input is entered. Try again.\n");
flag_cond = false;
}
} while (!flag_cond);
System.out.print("---- Enter keyword to search the description : ");
keyword = scanner.nextLine();
System.out.println();
System.out.println(" ** Enter 'any' if you want to see items from any seller. ");
System.out.print("---- Enter Seller ID to search : ");
seller = scanner.next();
scanner.nextLine();
System.out.println();
System.out.println(" ** This will search items that have been posted after the designated date.");
System.out.print("---- Enter date posted (YYYY-MM-DD): ");
datePosted = scanner.next();
scanner.nextLine();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
date = LocalDate.parse(datePosted, formatter).atStartOfDay();
System.out.println();
/* TODO: Query condition: item category */
/* TODO: Query condition: item condition */
/* TODO: Query condition: items whose description match the keyword (use LIKE operator) */
/* TODO: Query condition: items from a particular seller */
/* TODO: Query condition: posted date of item */
/* TODO: List all items that match the query condition */
HandleAuctionClosure(); // 경매 마감 확인
System.out.println("Item ID | Item description | Condition | Seller | Buy-It-Now | Current Bid | highest bidder | Time left | bid close");
System.out.println("-------------------------------------------------------------------------------------------------------------------");
boolean any_category = false, any_seller = false;
if (category == null)
any_category = true;
if (seller.equalsIgnoreCase("any"))
any_seller = true;
Timestamp bid_end_time;
String description, seller_id, highest_bidder_id;
long item_id, auction_id;
int BIN_price, current_price;
try (PreparedStatement p = conn.prepareStatement(
"SELECT item_id, auction_id, description, seller_id, current_price, buy_it_now_price, bid_end_time " +
"FROM items NATURAL JOIN auctions " +
"WHERE condition = ? AND description LIKE ? AND bid_start_time > ? AND " +
"(status = 'LISTED' OR status = 'BIDDING') " +
(any_category ? "" : "AND category = ? ") +
(any_seller ? "" : "AND seller_id = ?")
)) {
p.setString(1, condition.name());
p.setString(2, "%" + keyword + "%");
p.setTimestamp(3, Timestamp.valueOf(date));
if (!any_category) p.setString(4, category.name());
if (any_category && !any_seller) p.setString(4, seller);
else if (!any_seller) p.setString(5, seller);
try (ResultSet item_rset = p.executeQuery()) {
while (item_rset.next()) {
item_id = item_rset.getLong("item_id");
auction_id = item_rset.getLong("auction_id");
description = item_rset.getString("description");
seller_id = item_rset.getString("seller_id");
current_price = item_rset.getInt("current_price");
BIN_price = item_rset.getInt("buy_it_now_price");
bid_end_time = item_rset.getTimestamp("bid_end_time");
System.out.print(
item_id + " | " +
description + " | " +
condition.name() + " | " +
seller_id + " | " +
BIN_price + " | " +
current_price + " | "
);
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT bidder_id " +
"FROM bids " +
"WHERE auction_id = ? " +
"ORDER BY bid_price DESC, bid_time ASC " +
"LIMIT 1"
)) {
pStmt.setLong(1, auction_id);
try (ResultSet rset = pStmt.executeQuery()) {
if (!rset.next())
System.out.print(" | ");
else {
highest_bidder_id = rset.getString("bidder_id");
System.out.print(highest_bidder_id + " | ");
}
}
}
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = bid_end_time.toLocalDateTime();
LocalDateTime now = LocalDateTime.now();
Duration duration = Duration.between(now, dateTime);
long totalMinutes = duration.toMinutes();
long days = totalMinutes / (24 * 60);
long hours = (totalMinutes % (24 * 60)) / 60;
long minutes = totalMinutes % 60;
StringBuilder timeLeft = new StringBuilder();
if (days > 0) timeLeft.append(days).append("d ");
if (hours > 0 || days > 0) timeLeft.append(hours).append("h ");
timeLeft.append(minutes).append("m");
System.out.println(
timeLeft.toString().trim() + " | " +
dateTime.format(formatter)
);
}
}
} catch (SQLException e) {
System.out.println("SQLException : " + e);
}
System.out.println();
System.out.print("---- Select Item ID to buy or bid: ");
int price;
try {
item_id = scanner.nextLong();
scanner.nextLine();
System.out.print(" Price: ");
price = scanner.nextInt();
scanner.nextLine();
} catch (java.util.InputMismatchException e) {
System.out.println("Error: Invalid input is entered. Try again.\n");
return;
}
/* TODO: Buy-it-now or bid: If the entered price is higher or equal to Buy-It-Now price, the bid ends and the following needs to be printed. */
HandleAuctionClosure(); // 경매 마감 확인
String status;
try (PreparedStatement pStmt = conn.prepareStatement(
"SELECT seller_id, auction_id, current_price, buy_it_now_price, status " +
"FROM items NATURAL JOIN auctions " +
"WHERE item_id = ?"
)) {
pStmt.setLong(1, item_id);
try (ResultSet rset = pStmt.executeQuery()) {
if (!rset.next()) {