-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
1016 lines (869 loc) · 64.2 KB
/
Main.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
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 javax.annotation.processing.SupportedOptions;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.function.DoubleToIntFunction;
import java.util.regex.*;
public class Main {
public static float formatPrice (String price) {
float result = -1;
String priceRegex = "\\d+";
Pattern pattern = Pattern.compile(priceRegex);
Matcher matcher = pattern.matcher(price);
if(matcher.matches()) {
float floatValue = Float.parseFloat(price);
int decimalPlaces = 2;
String formattedValue = String.format("%." + decimalPlaces + "f", floatValue);
Float parsed = Float.parseFloat(formattedValue);
result = parsed;
}
return result;
}
public static boolean formatIsbnCode (String isbnCode ) {
String regex = "\\d{3}-\\d{10}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(isbnCode);
if(matcher.matches()) return true;
return false;
}
public static boolean formatPublicationYear (String publicationYear) {
String datePattern = "^(\\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$";
Pattern pattern = Pattern.compile(datePattern);
Matcher matcher = pattern.matcher(publicationYear);
if(matcher.matches()) {
return true;
}
return false;
}
public static boolean formatPageNumber (int pageNumber) {
boolean result = false;
String convertToString = Integer.toString(pageNumber);
if(convertToString.startsWith("0")) {
result = false;
}
else {
result = true;
}
return result;
}
public static boolean formatEmail (String email) {
String emailRegex = "[a-zA-Z0-9]+@gmail\\.com";
Pattern pattern = Pattern.compile(emailRegex);
Matcher matcher = pattern.matcher(email);
if (matcher.matches()) return true;
return false;
}
public static boolean formatPublisher (String publisherName) {
// Define the regex pattern
String regex = "[a-zA-Z]+";
// Compile the pattern
Pattern pattern = Pattern.compile(regex);
// Create a matcher with the input string
Matcher matcher = pattern.matcher(publisherName);
if(matcher.matches()) return true;
return false;
}
public static boolean formatAlphaNumber (String text) {
String alphanumericRegex = "[a-zA-Z0-9\\s\\-\\.\\(\\)]+";
Pattern pattern = Pattern.compile(alphanumericRegex);
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) return true;
return false;
}
public static boolean formatPhoneNumber (String phoneNumber) {
String phoneNumberRegex = "\\d{10}";
Pattern pattern = Pattern.compile(phoneNumberRegex);
Matcher matcher = pattern.matcher(phoneNumber);
if (matcher.matches()) return true;
return false;
}
public static boolean limitDescription (String description) {
String[] countWords = description.split(" ");
int count = 0;
for (String word: countWords) {
count += 1;
}
if(count < 500) return true;
return false;
}
public static boolean formatString (String text) {
String lettersRegex = "[a-zA-Z]+";
Pattern pattern = Pattern.compile(lettersRegex);
Matcher matcher = pattern.matcher(text);
if (matcher.matches()) return true;
return false;
}
public static boolean backOne () {
Scanner scanner = new Scanner(System.in);
System.out.println("\n---------------select back or continue----------------");
System.out.println("0. Back");
System.out.println("1. Continue");
System.out.print(">User ");
boolean result = false;
try {
int back = scanner.nextInt();
scanner.nextLine();
if(back == 0) return result = true;
else if (back == 1) {
result = false;
}else {
logErrorSyntax();
result = true;
}
}
catch (InputMismatchException err) {
logErrorSyntax();
// clear buffer
clearBufferMemory(scanner);
result = true;
}
finally {
return result;
}
}
public static void clearBufferMemory (Scanner scanner) {
scanner.nextLine();
}
public static void showListFeatureForUser () {
System.out.println("-------------------Mode store-------------------\n");
System.out.println("To interact with the store, use the command below");
System.out.println("1. Show all books");
System.out.println("2. Find Book follow title");
System.out.println("3. Sort book follow author name");
System.out.println("4. Sort book follow price");
System.out.println("5. Filter book follow author name");
System.out.println("6. Filter book follow genre");
System.out.println("0. Back");
}
public static void logErrorSyntax () {
System.out.println(Support.redColor("Command is wrong!, only select available options"));
}
public static boolean checkEmpty (String field) {
if(field.trim().isEmpty()) {
return true;
}
else {
return false;
}
}
public static void main (String[] args) {
BookStore amazonBook = new BookStore("Amazon book", "2023-11-02");
if(!amazonBook.connectDB()) {
System.out.println("connect failed");
}
else {
// pre-get
amazonBook.getBooks();
amazonBook.getGenres();
amazonBook.getAuhors();
amazonBook.getNationalities();
amazonBook.getPublishers();
boolean menu = true;
Scanner scanner = new Scanner(System.in);
// loop1
while (menu) {
System.out.println("Choose your role");
System.out.println("1. User");
System.out.println("2. Admin");
try {
System.out.print(">");
int isWho = scanner.nextInt();
if(isWho == 1) {
// loop store
boolean userRun = true;
while(userRun) {
System.out.println("-------------------Welcome to book store-------------------\n");
System.out.println("Select options as shown");
System.out.println("1. Join store");
System.out.println("2. Show info of store");
System.out.println("0. Back");
System.out.print(">User ");
try {
int userCommand = scanner.nextInt();
if (userCommand == 1) {
boolean runCommand = true;
while(runCommand) {
showListFeatureForUser();
System.out.print(">User ");
try {
int command = scanner.nextInt();
scanner.nextLine();
if(command == 1) {
boolean runCommandOne = true;
while (runCommandOne) {
amazonBook.showBooks();
if(backOne()) break;
}
}
else if(command == 5) {
boolean runCommandFive = true;
while (runCommandFive) {
System.out.println("Enter author name to find books");
System.out.print(">User ");
String authorName = scanner.nextLine();
amazonBook.filterAuthorName(authorName);
if(Main.backOne()) break;
}
}
else if (command == 2) {
boolean runCommandTwo = true;
while (runCommandTwo) {
System.out.print("Enter book name to find book:");
String bookName = scanner.nextLine();
amazonBook.findBook(bookName);
if(Main.backOne()) break;
}
}
else if(command == 6) {
boolean runCommandSix = true;
while (runCommandSix) {
System.out.println("Enter genre of book to find book ");
System.out.print(">User ");
String genre = scanner.nextLine();
amazonBook.filterFollowGenre(genre);
if(Main.backOne()) break;
}
}
else if(command == 3) {
boolean runCommandThree = true;
while (runCommandThree) {
System.out.println("1. Sort author name from a-z (low - high)");
System.out.print(">User ");
try {
int sortAuthorCommand = scanner.nextInt();
if(sortAuthorCommand == 1) {
amazonBook.sortAuthor("a-z");
if(Main.backOne()) break;
}
else {
Main.logErrorSyntax();
}
}
catch (InputMismatchException err) {
Main.logErrorSyntax();
// clear buffer
clearBufferMemory(scanner);
}
}
}
else if (command == 4) {
boolean runSortAuthor = true;
while (runSortAuthor) {
try {
System.out.println("1. Sort price of book from a-z(high - low)");
System.out.print(">User ");
int commandPrice = scanner.nextInt();
if(commandPrice == 1) {
amazonBook.sortPrice("a-z");
if(Main.backOne()) break;
}
else {
Main.logErrorSyntax();
}
}
catch (InputMismatchException error) {
Main.logErrorSyntax();
// clear buffer
clearBufferMemory(scanner);
}
}
}
else if (command == 0) {
break;
}
else {
Main.logErrorSyntax();
}
}
catch (InputMismatchException error) {
Main.logErrorSyntax();
// clear buffer
clearBufferMemory(scanner);
}
}
}
else if (userCommand == 2) {
System.out.println("----------------Info store--------------");
System.out.println(amazonBook + "\n");
System.out.println("0. Back");
System.out.print(">User ");
try {
int back = scanner.nextInt();
if(back == 0) continue;
logErrorSyntax();
}
catch (InputMismatchException err) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
else if (userCommand == 0) {
break;
}
else {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
catch (InputMismatchException err){
logErrorSyntax();
// clear buffer
clearBufferMemory(scanner);
}
}
}
// menu for admin
else if (isWho == 2){
boolean run = true;
while (run) {
System.out.println("-------------------This is admin mode-------------------\n");
System.out.println("1. Add book");
System.out.println("2. Update book");
System.out.println("3. Delete book");
System.out.println("0. Back");
System.out.print(">Enter option: ");
try {
int option = scanner.nextInt();
scanner.nextLine();
String title = "", genre = "", authorName = "" , authorEmail ="", authorNationality ="" , authorPhone ="" , authorDescription="", isbnCode="" , publisher ="" , pubYear = "";
int pageNumber = 0;
int bookQuantity = 0;
float sellingPrice = 0 ;
if(option == 1) {
System.out.println("------------------Add book--------------");
boolean titleRun = true;
while (titleRun) {
System.out.println("Enter the book's information\n");
System.out.print("Title(required): ");
String bookT = scanner.nextLine();
if(checkEmpty(bookT)) {
System.out.println(Support.redColor("The field cannot be left blank"));
}
else if(!formatAlphaNumber(bookT)) {
System.out.println(Support.redColor("Field only can enter letters and numbers"));
}
else if(amazonBook.checkBook(bookT)) {
System.out.println(Support.redColor("Book is exist"));
}
else {
title = bookT;
System.out.println(Support.greenColor("Add title for book success"));
break;
}
}
boolean genreRun = true;
while (genreRun) {
System.out.println("Choose a genre for your book");
System.out.println("1. Choose from available genres");
System.out.println("2. Add a new genre");
System.out.print("Enter genre: ");
try {
int genreFeatureOptions = scanner.nextInt();
scanner.nextLine();
if(genreFeatureOptions == 1) {
boolean genreOptionOneRun = true;
while (genreOptionOneRun) {
if(amazonBook.genresList.isEmpty()) {
System.out.println("No any genres");
System.out.println("Do you want back and add a new genre?(Enter OK)");
String reply = scanner.nextLine();
if(reply.equals("OK")) {
break;
}
}
else {
System.out.println("All genres of books are available");
amazonBook.showGenres();
System.out.print("Enter genre: ");
try {
int genreOptions = scanner.nextInt();
scanner.nextLine();
String isExistGenre = amazonBook.findGenre(genreOptions);
if(isExistGenre.isEmpty()) {
System.out.println(Support.redColor("The field cannot be left blank"));
}
else {
genre = Support.capitalizeText(isExistGenre);
System.out.println(Support.greenColor("Add " + genre + " genre for book success"));
genreRun = false;
break;
}
}
catch (InputMismatchException err) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
}
}
else if (genreFeatureOptions == 2) {
System.out.println("Enter the book genre\n");
boolean genreOptionTwoRun = true;
while (genreOptionTwoRun) {
System.out.print("Genre(required): ");
String newGenre = scanner.nextLine();
if(checkEmpty(newGenre)) {
System.out.println(Support.redColor("The field cannot be left blank"));
}
else if(amazonBook.checkGenre(newGenre)) {
System.out.println(Support.redColor("Genre is exist"));
}
else {
genre = Support.capitalizeText(newGenre);
System.out.println(Support.greenColor("Add " + genre + " genre for book success"));
genreRun = false;
break;
}
}
}
else {
logErrorSyntax();
}
}
catch (InputMismatchException err) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
boolean quantityRun = true;
while (quantityRun) {
try {
System.out.print("Quantity(required): ");
String quantity = scanner.nextLine();
int convertQuantity = Integer.parseInt(quantity);
if(checkEmpty(quantity)) {
System.out.println(Support.redColor("The field cannot be left blank"));
}
else if(convertQuantity < 1) {
System.out.println(Support.redColor("Quantity must grater than 0"));
}
else {
bookQuantity = convertQuantity;
System.out.println(Support.greenColor("Added quantity for book success"));
break;
}
}
catch (NumberFormatException error) {
System.out.println(Support.redColor("Only can enter number"));
}
}
boolean priceRun = true;
while (priceRun) {
try {
System.out.print("Selling price(required): ");
String price = scanner.nextLine();
float checkAndConvert = formatPrice(price);
if(checkEmpty(price)) {
System.out.println(Support.redColor("The field cannot be left blank"));
}
else if (checkAndConvert == 0) {
System.out.println(Support.redColor("Price must be greater than 0 and only can be float numbers"));
}
else if(checkAndConvert != -1) {
System.out.println(Support.greenColor("Add price for book success"));
break;
}
else {
System.out.println(Support.redColor("Only can enter number"));
}
}
catch (InputMismatchException error) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
boolean authorRun = true;
while (authorRun) {
System.out.println("Add an author name to your book");
System.out.println("1. Select available authors");
System.out.println("2. Add new a author");
try {
System.out.print("Enter option: ");
int authorFeatureOptions = scanner.nextInt();
scanner.nextLine();
if(authorFeatureOptions == 1) {
boolean authorOptionOneRun = true;
while (authorOptionOneRun) {
if(amazonBook.authorsList.isEmpty()) {
System.out.println("No any authors");
System.out.println("Do you want back and add a new author?(Enter OK)");
String reply = scanner.nextLine();
if(reply.equals("OK")) {
break;
}
}
else {
System.out.println("All available authors");
amazonBook.showAuthors();
System.out.print("Choose author(enter number): ");
try {
int authorOption = scanner.nextInt();
String name = amazonBook.findAuthors(authorOption);
if(name.isEmpty()) {
System.out.println(Support.redColor("The field cannot be left blank"));
}else {
authorName = Support.capitalizeText(name);
System.out.println(Support.greenColor("Added " +authorName + " as author"));
authorRun = false;
break;
}
}
catch (InputMismatchException error) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
}
}
else if (authorFeatureOptions == 2) {
boolean authorOptionTwoRun = true;
while (authorOptionTwoRun) {
System.out.println("Enter the info of the author ");
//1. add author name
boolean authorNameRun = true;
while (authorNameRun) {
System.out.print("Enter author name(required): ");
String authorNameIn = scanner.nextLine();
boolean validateAuthorName = formatAlphaNumber(authorNameIn);
if(checkEmpty(authorNameIn)) {
System.out.println(Support.redColor("The field cannot be left blank"));
}
else if(!validateAuthorName) {
System.out.println(Support.redColor("Field only can enter letters and numbers"));
}
else if(!amazonBook.checkAuthor(authorNameIn)) {
authorName = authorNameIn;
System.out.println(Support.greenColor("Added " + authorName + " as author success"));
break;
}
else {
System.out.println(Support.redColor("Author is exist"));
}
}
//2. Add author email
boolean authorEmailRun = true;
while (authorEmailRun) {
System.out.print("Enter author email(optional): ");
String authorEmailIn = scanner.nextLine();
boolean validateAuthorEmail = formatEmail(authorEmailIn);
if(checkEmpty(authorEmailIn)) {
System.out.println(Support.greenColor("You have left this field blank"));
break;
}
else if (!validateAuthorEmail) {
System.out.println(Support.redColor("You must enter the correct email format [email protected]"));
}
else if(!amazonBook.checkAuthor(authorName, authorEmailIn)) {
authorEmail = authorEmailIn;
System.out.println(Support.greenColor("Added " + authorEmail + " as email of author success"));
break;
}
else {
System.out.println(Support.redColor("Author is exist"));
}
}
//3. add author phone
boolean authorPhoneRun = true;
while (authorPhoneRun) {
System.out.print("Enter author phone(optional): ");
String authorPhoneIn = scanner.nextLine();
boolean validatePhoneAuthor = formatPhoneNumber(authorPhoneIn);
if(checkEmpty(authorPhoneIn)) {
System.out.println(Support.greenColor("You have left this field blank"));
break;
}
else if(!validatePhoneAuthor) {
System.out.println(Support.redColor("Field only can enter numbers and must be 10 numbers "));
}
else if(!amazonBook.checkAuthor(authorName, authorEmail, authorPhoneIn)) {
authorPhone = authorPhoneIn;
System.out.println(Support.greenColor("Added " + authorPhone + " as phone number of author success"));
break;
}
else {
System.out.println(Support.redColor("Author is exist"));
}
}
// 4. author nationality
boolean authorNationalityRun = true;
System.out.println("Select your country");
amazonBook.showNationalities();
while (authorNationalityRun) {
System.out.print("Enter nationality(optional): ");
String countryStt = scanner.nextLine();
if(formatString(countryStt)) {
System.out.println(Support.greenColor("You have left this field blank"));
authorNationality = "None";
break;
}
else {
try {
int stt = Integer.parseInt(countryStt);
String nationalityName = amazonBook.findNationalities(stt);
if(nationalityName.isEmpty()) {
System.out.println(Support.redColor("Command is wrong!, try again"));
}
else {
authorNationality = Support.capitalizeText(nationalityName);
System.out.println(Support.greenColor("Add " + nationalityName + " country " + "success"));
break;
}
}
catch (NumberFormatException error) {
System.out.println(Support.greenColor("You have left this field blank"));
break;
}
}
}
// 5. add description
boolean authorDescriptionRun = true;
while (authorDescriptionRun) {
System.out.print("Enter author description(optional): ");
String authorDescriptionIn = scanner.nextLine();
boolean limitDescription= limitDescription(authorDescriptionIn);
if(checkEmpty(authorDescriptionIn)) {
System.out.println(Support.greenColor("You have left this field blank"));
break;
}
else if(!limitDescription) {
System.out.println(Support.redColor("Description limited to 500 words"));
}
else if(!amazonBook.checkAuthor(authorName, authorEmail, authorPhone, authorNationality, authorDescriptionIn)) {
authorDescription = authorDescriptionIn;
System.out.println(Support.greenColor("Added " + authorDescription + " as description of book success"));
authorRun = false;
break;
}
else {
System.out.println(Support.redColor("Author is exist"));
}
}
break;
}
break;
}
else {
logErrorSyntax();
}
}
catch (InputMismatchException error) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
boolean pageNumberRun = true;
while (pageNumberRun) {
try {
System.out.print("Page number(number):");
int pNumber = scanner.nextInt();
scanner.nextLine();
if(formatPageNumber(pNumber)) {
pageNumber = pNumber;
System.out.println(Support.greenColor("Add page number for book success"));
break;
}
else {
System.out.println(Support.redColor("Page number cannot start by 0 number, try again"));
}
}
catch (InputMismatchException error) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
boolean isbnCodeRun = true;
while (isbnCodeRun) {
System.out.print("ISBN code: ");
isbnCode = scanner.nextLine();
if(!formatIsbnCode(isbnCode)) {
System.out.println(Support.redColor("ISBN code only can contain 14 digits follow pattern : ***-**********"));
}
else {
System.out.println(Support.greenColor("Add ISBN code for book success"));
break;
}
}
boolean pubYearRun = true;
while (pubYearRun) {
System.out.print("Publication year(yyyy-mm-dd): ");
pubYear = scanner.nextLine();
if(!formatPublicationYear(pubYear)) {
System.out.println(Support.redColor("Publication year must follow pattern yyyy-mm-dd"));
}
else {
System.out.println(Support.greenColor("Add publication year success"));
break;
}
}
boolean publisherRun = true;
while (publisherRun) {
System.out.println("Add publisher for your book");
System.out.println("1. Choose available publishers");
System.out.println("2. Add a new publisher");
try {
System.out.print("Enter option: ");
int publisherOption = scanner.nextInt();
scanner.nextLine();
if(publisherOption == 1) {
boolean publisherOptionRun = true;
while (publisherOptionRun) {
if(amazonBook.publishersList.isEmpty()) {
System.out.println("No any publishers");
System.out.println("Do you want back and add a new publisher?(Enter OK)");
String reply = scanner.nextLine();
if(reply.equals("OK")) {
break;
}
}
else {
System.out.println("All available publishers");
amazonBook.showPublishers();
System.out.print("Choose publisher(enter number): ");
try {
int publisherStt = scanner.nextInt();
String name = amazonBook.findPublisher(publisherStt);
if(name.isEmpty()) {
System.out.println(Support.redColor("The field cannot be left blank"));
}else {
publisher = Support.capitalizeText(name);
System.out.println(Support.greenColor("Added " +publisher + " as publisher for book success"));
publisherRun = false;
break;
}
}
catch (InputMismatchException error) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
}
}
else if (publisherOption == 2) {
boolean publisherOptionTwo = true;
while (publisherOptionTwo) {
System.out.println("Enter a new publisher(required): ");
String publisherName = scanner.nextLine();
if(publisherName.isEmpty()) {
System.out.println(Support.redColor("The field cannot be left blank"));
break;
}
else {
publisher = publisherName;
System.out.println(Support.greenColor("Add " + publisher + " for book success"));
break;
}
}
break;
}
else {
logErrorSyntax();
}
}
catch (InputMismatchException error) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
// save info of author
System.out.println(authorName + "\n" + authorEmail + "\n" + authorPhone + "\n" + authorNationality + "\n" + authorDescription + "\n" + title + "\n" + genre + "\n" + sellingPrice + "\n" + pageNumber + "\n" + isbnCode + "\n" + pubYear + "\n" + publisher + "\n" + bookQuantity);
amazonBook.add(authorName,authorEmail, authorPhone, authorNationality, authorDescription, title, genre, sellingPrice, pageNumber, isbnCode, pubYear, publisher, bookQuantity);
}
// else if (option == 2) {
// System.out.println("----------------------Update book--------------------");
// System.out.println("Update the book's information, if you want to keep the previous data, leave it blank\n");
//
// System.out.print("Enter book id is needed update:");
// int bookId = scanner.nextInt();
// scanner.nextLine();
//
// System.out.print("Title:");
// title = scanner.nextLine();
//
// System.out.print("Genre:");
// genre = scanner.nextLine();
//
// System.out.print("Selling price:");
// float price = scanner.nextFloat();
// sellingPrice = formatPrice(price);
// scanner.nextLine();
//
// System.out.print("Author:");
// authorName = scanner.nextLine();
//
// System.out.print("Page number:");
// pageNumber = scanner.nextInt();
// scanner.nextLine();
//
// System.out.print("ISBN code:");
// isbnCode = scanner.nextLine();
//
// System.out.print("Publication year:");
// pubYear = scanner.nextLine();
//
// System.out.print("Publisher:");
// publisher = scanner.nextLine();
//
//
//// amazonBook.update(bookId, title, genre,sellingPrice, pageNumber, isbnCode, pubYear, publisher , author);
//
// }
else if (option == 3) {
System.out.println("---------------------Delete book----------------------");
System.out.println("To delete a book enter the id");
System.out.print("Enter id of the book:");
try {
int bookId = scanner.nextInt();
scanner.nextLine();
amazonBook.delete(bookId);
}
catch (InputMismatchException error) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}
else if (option == 0) {
break;
}
else {
logErrorSyntax();
}
// clean
title = "";
genre ="" ;
authorName="" ;
authorEmail="";
authorNationality="";
authorPhone="" ;
authorDescription="";
isbnCode ="" ;
publisher ="" ;
pubYear = "";
pageNumber = 0;
sellingPrice = 0;
amazonBook.getBooks();
amazonBook.getGenres();
amazonBook.getAuhors();
amazonBook.getNationalities();
amazonBook.getPublishers();
}
catch (InputMismatchException err) {
logErrorSyntax();
clearBufferMemory(scanner);
}
}