-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineUi.java
More file actions
657 lines (468 loc) · 20.2 KB
/
CommandLineUi.java
File metadata and controls
657 lines (468 loc) · 20.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
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
package oopprojecthalilkayra;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Scanner;
public class CommandLineUi {
private static final Database db = new Database();
private static final Scanner input = new Scanner(System.in);
public static void start() {
System.out.println("Weloceme\n\n1 - Register\n2 - Login");
int inputVar = input.nextInt();
input.nextLine();
switch (inputVar) {
case 1 -> register();
case 2 -> login();
default -> {
System.out.println("Invalid input");
start(); // Geçersiz girişte tekrar menüyü göster
}
}
}
private static void login() {
System.out.println("\nLogin menu\nPlease enter your username");
String usernameInput = input.nextLine();
System.out.println("\nPlease enter your password");
String passwordInput = input.nextLine();
System.out.println("please wait...");
if (AuthManager.login(usernameInput, passwordInput)) {
System.out.println("Welcome " + OopProjectHalilKayra.activeUser.getName());
db.getMarketplace();
Database.usernameOfActiveUser = OopProjectHalilKayra.activeUser.getUsername();
mainMenu();
} else {
System.out.println("Your username or password incorrect.");
tryAgainLogin();
}
}
private static void tryAgainLogin() {
System.out.println("1 - Try Again\n 2 - Return");
int inputVar = input.nextInt();
input.nextLine();
switch (inputVar) {
case 1 -> login();
case 2 -> start();
default -> {
System.out.println("You should input 1 or 2!!");
tryAgainLogin();
}
}
}
private static void register() {
System.out.println("\nRegiste Menu\n");
String username = takeUsernameForRegister();
System.out.println("\nPlease enter your name");
String name = input.nextLine();
System.out.println("\nPlease enter your surname");
String surname = input.nextLine();
System.out.println("\nPlease enter your email");
String email = input.nextLine();
System.out.println("\nPlease enter your password");
String password = input.nextLine();
System.out.println("\nPlease enter your home address");
String homeAddress = input.nextLine();
System.out.println("\nPlease enter your workd address");
String workAddress = input.nextLine();
java.util.Date birthDate = takeInputBirthDate();
if (AuthManager.register(username, name, surname, email, password,
homeAddress, workAddress, birthDate)) {
System.out.println("Register completed");
login();
} else {
System.out.println("An error occured do you want try again\n1 - yes\n2 - no");
String inputVar = input.nextLine();
if (inputVar.equals("1")) {
register();
} else {
start();
}
}
}
private static String takeUsernameForRegister() {
System.out.println("Please enter your username");
String username = input.nextLine();
if (AuthManager.isUserExist(username)) {
System.out.println("\nThis username is invalid");
takeUsernameForRegister();
}
if (username.contains(" ")) {
System.out.println("Your username can not be involved space( ) character");
takeUsernameForRegister();
}
return username;
}
private static java.util.Date takeInputBirthDate() {
SimpleDateFormat birthDateStr = new SimpleDateFormat("dd-MM-yyyy");
birthDateStr.setLenient(false); // date check
while (true) {
System.out.print("\nPlease enter your birth date as (dd-MM-yyyy): ");
String dateStr = input.nextLine();
try {
java.util.Date realDate = birthDateStr.parse(dateStr);
return realDate;
} catch (ParseException e) {
System.out.println("Invalid format or non-existent date.\nPlease try again.");
}
}
}
private static void mainMenu() {
System.out.println("\n=== Main Menu ===");
System.out.println("1. Show all products");
System.out.println("2. Show product by category");
System.out.println("3. show cart");
System.out.println("4. Show favorites");
System.out.println("5. Show credit cards ");
System.out.println("6. Add credit Card");
System.out.println("0. Log out and exıt");
System.out.print("\nYour choise: ");
while (true) {
int choice = input.nextInt();
input.nextLine(); // Buffer temizle
switch (choice) {
case 1 -> showAllProducts();
case 2 -> showProductsByCategory();
case 3 -> showCart();
case 4 -> showFavorites();
case 5 -> showCreditCards();
case 6 -> addCreditCard();
case 0 -> {
System.out.println("\nLogging out........");
System.exit(0);
}
default -> {
System.out.println("Invalid selection");
mainMenu();
}
}
}
}
private static void showAllProducts() {
for (Product product : OopProjectHalilKayra.marketplace) {
System.out.println(
"ID: " + product.getId()
+ " | " + product.getProductName()
+ " | Price:" + product.getPrice()
+ " | Stock: " + product.getProductStock()
+ " | " + product.getProductCategory()
);
}
showProductDetail();
}
private static void showProductDetail() {
System.out.print("Please enter porduct Id: ");
System.out.println("Enter 0 for return main menu");
int productId = input.nextInt();
input.nextLine();
if (productId == 0) {
mainMenu();
}
Product product = getProductbyProductId(productId);
if (product != null) {
System.out.println("\n=== Product Detail ===");
System.out.println("ID: " + product.getId());
System.out.println("Name: " + product.getProductName());
System.out.println("Description: " + product.getProductDescription());
System.out.println("Price: ₺" + product.getPrice());
System.out.println("Category: " + product.getProductCategory());
System.out.println("Stock: " + product.getProductStock());
} else {
System.out.println("Invalid product id!");
showProductDetail();
}
takeInputForProduct(product);
}
private static Product getProductbyProductId(int productId) {
for (Product product : OopProjectHalilKayra.marketplace) {
if (product.getId() == productId) {
return product;
}
}
return null;
}
private static void takeInputForProduct(Product product) {
System.out.println("1 - add to favorites\n2 - add to cart");
int inputVar = input.nextInt();
input.nextLine();
switch (inputVar) {
case 1 -> {
OopProjectHalilKayra.activeUser.addToFavorites(product);
System.out.println(product.getProductName() + " added to your favorites");
mainMenu();
}
case 2 -> {
OopProjectHalilKayra.activeUser.addToCart(product);
System.out.println(product.getProductName() + " added to your cart");
mainMenu();
}
default -> {
System.out.println("invalid input\nReturnin to the main menu");
mainMenu();
}
}
}
private static void showProductsByCategory() {
System.out.println("\nPlease enter a product category");
String category = input.nextLine();
for (Product product : OopProjectHalilKayra.marketplace) {
if (product.getProductCategory().equalsIgnoreCase(category)) {
System.out.println(
"ID: " + product.getId()
+ " | " + product.getProductName()
+ " | price:" + product.getPrice()
+ " | Stock: " + product.getProductStock()
+ " | " + product.getProductCategory()
);
}
}
showProductDetail();
}
private static void showCart() {
for (Product product : OopProjectHalilKayra.activeUser.getCart()) {
System.out.println(
"ID: " + product.getId()
+ " | " + product.getProductName()
+ " | price:" + product.getPrice()
+ " | Stock: " + product.getProductStock()
+ " | " + product.getProductCategory()
);
}
System.out.println("Total price:" + getTotalPriceOfCart());
System.out.println("1 - check out cart\n2 - clear cart\n3 - return to main menu");
int inputVar = input.nextInt();
input.nextLine();
switch (inputVar) {
case 1 -> checkOutCart();
case 2 -> clearCart();
case 3 -> mainMenu();
default -> {
System.out.println("Invalid input");
showCart();
}
}
}
private static void clearCart() {
db.clearCartDatabase();
OopProjectHalilKayra.activeUser.getCart().clear();
System.out.println("Your cart cleared");
}
private static void checkOutCart() {
if (isStockEnoughForCart()) {
java.util.Date now = new java.util.Date();
CreditCard cardForCheckOut = selectCreditCardForCheckOut();
if (now.before(cardForCheckOut.getExpirationDate())) {
for (Product product : OopProjectHalilKayra.activeUser.getCart()) {
Order order = new Order(OopProjectHalilKayra.activeUser, product, 1, cardForCheckOut);
order.createOrder();
}
System.out.println("Your orders created");
clearCart();
} else {
System.out.println("Your card's expiration date has passed.");
mainMenu();
}
} else {
System.out.println("Not enogh stock for your cart");
mainMenu();
}
}
private static boolean isStockEnoughForCart() {
for (Product product : OopProjectHalilKayra.activeUser.getCart()) {
if (product.getProductStock() <= 0) {
return false;
}
}
return true;
}
private static CreditCard selectCreditCardForCheckOut() {
for (int i = 1; i <= OopProjectHalilKayra.activeUser.getCreditCards().size(); i++) {
System.out.println(i + " - " + OopProjectHalilKayra.activeUser.getCreditCards().get(i - 1).getCardNumber());
}
System.out.println("Please select credit card");
int inputVar = input.nextInt();
input.nextLine();
if (inputVar >= 0 && inputVar <= OopProjectHalilKayra.activeUser.getCreditCards().size()
&& OopProjectHalilKayra.activeUser.getCreditCards().get(inputVar - 1) != null) {
return OopProjectHalilKayra.activeUser.getCreditCards().get(inputVar - 1);
} else {
System.out.println("Invalid selection");
mainMenu();
}
return null;
}
private static void showFavorites() {
for (Product product : OopProjectHalilKayra.activeUser.getFavoriteProducts()) {
System.out.println(
"ID: " + product.getId()
+ " | " + product.getProductName()
+ " | Price" + product.getPrice()
+ " | Stock: " + product.getProductStock()
+ " | " + product.getProductCategory()
);
}
System.out.println("1 - Add to cart \n2 - Remove from favorites\n3 - clear favorites\n4 - return to main menu");
int inputVar = input.nextInt();
input.nextLine();
switch (inputVar) {
case 1 -> addToCartFromFavorites();
case 2 -> removeFromFavorites();
case 3 -> clearFavorites();
case 4 -> mainMenu();
default -> {
System.out.println("Invalid input");
showFavorites();
}
}
}
private static void clearFavorites(){
for(Product product : OopProjectHalilKayra.activeUser.getFavoriteProducts()){
db.removeFromFavoritesDatabase(product);
//System.out.println(product.getProductName() + "removed from favoirtes");
}
OopProjectHalilKayra.activeUser.getFavoriteProducts().clear();
System.out.println("Your favorites cleared");
mainMenu();
}
private static void addToCartFromFavorites() {
System.out.println("Please enter prdocut Id for add to cart");
int inputVar = input.nextInt();
input.nextLine();
if (OopProjectHalilKayra.activeUser.getFavoriteProducts().contains(getProductbyProductId(inputVar))) {
OopProjectHalilKayra.activeUser.addToCart(getProductbyProductId(inputVar));
System.out.println("Selected product add to cart");
mainMenu();
} else {
System.out.println("Selected product isn't in your favorites");
mainMenu();
}
}
private static void removeFromFavorites() {
System.out.println("Please enter prdocut Id for remove from favorites");
int inputVar = input.nextInt();
input.nextLine();
if (OopProjectHalilKayra.activeUser.getFavoriteProducts().contains(getProductbyProductId(inputVar))) {
OopProjectHalilKayra.activeUser.removeFromFavorites(getProductbyProductId(inputVar));
System.out.println("Selected product removed from your favorites");
showFavorites();
} else {
System.out.println("Selected product isn't in your favorites");
mainMenu();
}
}
private static double getTotalPriceOfCart() {
double totalPrice = 0;
for (Product product : OopProjectHalilKayra.activeUser.getCart()) {
totalPrice += product.getPrice();
}
return totalPrice;
}
private static void showCreditCards() {
for (CreditCard card : OopProjectHalilKayra.activeUser.getCreditCards()) {
System.out.println(
"\nCard Number: " + card.getCardNumber()
+ " | Security Code: " + card.getSecurityCode()
+ " | Expiration Date" + card.getExpirationDate().toString()
);
}
System.out.println("\n1 - Remove credit card\n2 - Add credit card\n3 - Main menu");
int inputVar = input.nextInt();
input.nextLine();
switch (inputVar) {
case 1 -> removeCreditCard();
case 2 -> addCreditCard();
case 3 -> mainMenu();
}
}
private static void removeCreditCard() {
System.out.println("Pleaase enter card number for remove card");
String cardNumber = input.nextLine();
CreditCard cardForRemove = OopProjectHalilKayra.activeUser.getCreditCardByCardNumber(cardNumber);
if (cardForRemove != null) {
OopProjectHalilKayra.activeUser.removeCreditCard(cardForRemove);
System.out.println("Selected credit card removed");
mainMenu();
} else {
System.out.println("The credit card isn't exist");
// System.out.println("This credit card doesn't exist");
mainMenu();
}
}
private static void addCreditCard() {
String cardNumber = takeCreditCardNumber();
if (cardNumber == null) {
System.out.println("An error occured while setting card number");
mainMenu();
}
System.out.println("Please enter name of the card owner(Written on the card)");
String cardOwnerName = input.nextLine();
String securityCode = takeCardSecurityCode();
if (securityCode == null) {
System.out.println("An error occured while setting card security code");
mainMenu();
}
java.util.Date expirationDate = takeExpirationDateForAddCard();
if (OopProjectHalilKayra.activeUser.addCreditCard(new CreditCard(cardNumber, cardOwnerName, securityCode, expirationDate))) {
System.out.println("Added to your credit Cards");
mainMenu();
} else {
System.out.println("Something Went Wrong Returning to Main Menu...");
mainMenu();
}
}
private static String takeCreditCardNumber() {
System.out.println("Please enter card number");
String cardNumber = input.nextLine();
if (cardNumber.length() != 16 || !cardNumber.matches("\\d{16}")) { // also check are all chasrs digit
System.out.println("Card number should be exactly 16 digits");
return takeCreditCardNumber();
}
return cardNumber;
}
private static String takeCardSecurityCode() {
System.out.println("Please enter Security code");
String securityCode = input.nextLine();
if (securityCode.length() != 3) {
System.out.println("Security code should be 3 digits");
takeCardSecurityCode();
} else {
return securityCode;
}
return null;
}
private static java.util.Date takeExpirationDateForAddCard() {
int yearOfExpiration = takeCreditMonth();
int monthOfExpiration = takeCreditCardMonth();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, yearOfExpiration);
calendar.set(Calendar.MONTH, monthOfExpiration - 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
java.util.Date expirationDate = calendar.getTime();
return expirationDate;
}
private static int takeCreditCardMonth() {
System.out.println("Please enter expiration date's MONTH");
int monthOfExpiration = input.nextInt();
input.nextLine();
if (monthOfExpiration < 1 || monthOfExpiration > 12) {
System.out.println("Please enter expiration date's MONTH shold be between 1 and 12 (including 1 and 12)");
monthOfExpiration = takeCreditCardMonth();
}
return monthOfExpiration;
}
private static int takeCreditMonth() {
System.out.println("Please enter expiration date's YEAR");
int yearOfExpiration = input.nextInt();
input.nextLine();
if (String.valueOf(Math.abs(yearOfExpiration)).length() != 4) {
System.out.println("expiration date's YEAR should be 4 digits");
yearOfExpiration = takeCreditMonth();
}
return yearOfExpiration;
}
}