· 5 years ago · Mar 11, 2020, 06:28 AM
1import java.util.Scanner; // Import this class to get user input and read .txt files
2
3import java.io.File; // Import this class to create .txt file
4
5import java.io.FileWriter; // Import this class to write in .txt files
6
7import java.io.PrintWriter; // Import this class to write int files with .print/.println/.printf
8
9class MovieFunctions extends MovieClass{
10 //Variables ----------------------------------------------------------------------------\
11 public final static int size = 10; //Size of array/maximum number of movies that can be recorded
12
13 public static int total = 0; //total = total number of movies
14
15 public static int movieID = 1; //unique number for each movie
16
17 public static MovieClass movies[] = new MovieClass[size]; //array of MovieClass class object
18
19 public static boolean isInitialised = false; //variable condition to initialise movies[] at the start of the program
20
21 public static Scanner input = new Scanner(System.in);
22
23 //(END) Variables ----------------------------------------------------------------------/
24
25 //Manipulate Objects Functions ---------------------------------------------------------\
26 //Initialise array of objects
27 public static void initialiseArray(){
28 for(int i = 0; i<size; i++){
29 movies[i] = new MovieClass();
30
31 }
32
33 }
34
35 //(END) Manipulate Objects Functions ---------------------------------------------------/
36
37 //User Interface Functions -------------------------------------------------------------\
38 //Clear Command Line Console
39 public static void clearScreen(){
40 for(int i = 0; i<100; i++){
41 System.out.println("");
42 }
43
44 }
45
46 //Print first 3 lines of the table
47 public static void tableTop(){
48 //Table Border
49 for(int i = 0; i<91; i++){
50 System.out.print("=");
51 }
52
53 System.out.println("");
54
55 //Table Header
56 System.out.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
57
58 //Table Divider
59 System.out.print("|");
60 for(int i = 0; i<89; i++){
61 System.out.print("-");
62 }
63 System.out.println("|");
64 }
65
66 public static void tableBottom(){
67 //Table Bottom Border
68 for(int i = 0; i<91; i++){
69 System.out.print("=");
70 }
71
72 System.out.println("");
73 }
74
75 //(END) User Interface Functions -------------------------------------------------------/
76
77 //Search Functions ---------------------------------------------------------------------\
78 //Check if there is any movie with the same Genre
79 static boolean searchGenre(String genre){
80 for(int i = 0; i < size; i++){
81 //found
82 if(genre.equals(movies[i].getGenre())){
83 return true;
84 }
85 }
86 //not found
87 return false;
88 }
89
90 //Check if there is any movie with the same Age Rating
91 static boolean searchAgeRating(String ageRating){
92 for(int i = 0; i < total; i++){
93 //found
94 if(ageRating.equals(movies[i].getAgeRating())){
95 return true;
96 }
97 }
98 //not found
99 return false;
100 }
101
102 //Check if there is any movie with the same Name
103 static int searchName(String name){
104 for(int i = 0; i < total; i++){
105 //found
106 if(name.equals(movies[i].getName())){
107 return i;
108 }
109 }
110 //not found
111 return -1;
112 }
113
114 //Check if there is any movie with the same Id
115 static int searchId(int id){
116 for(int i = 0; i < total; i++){
117 //found
118 if(id == movies[i].getId()){
119 return i;
120 }
121 }
122 //not found
123 return -1;
124 }
125
126 //(END) Search Functions ---------------------------------------------------------------/
127
128 //Recursive Functions ------------------------------------------------------------------\
129 //Display movies that has the same Genre
130 static void givenGenre(String genre, int low){
131 if (low > total-1){
132 return;
133 }
134 else if (genre.equals(movies[low].getGenre())){
135 movies[low].displayRow();
136 givenGenre(genre, low+1);
137 }
138 else {
139 givenGenre(genre, low+1);
140 }
141 }
142
143 //Display movies that has the same AgeRating
144 static void givenAgeRating(String ageRating, int low){
145 if (low > total-1){
146 return;
147 }
148 else if (ageRating.equals(movies[low].getAgeRating())){
149 movies[low].displayRow();
150 givenAgeRating(ageRating, low+1);
151 }
152 else {
153 givenAgeRating(ageRating, low+1);
154 }
155 }
156
157 //Calculate total movies that has the same Name
158 static int findTotalByName(String name, int high, int sum){
159 if (high < 0){
160 return sum;
161 }
162 else if (name.equals(movies[high].getName())){
163 sum++;
164 return findTotalByName(name, high-1, sum);
165 }
166 else {
167 return findTotalByName(name, high-1, sum);
168 }
169 }
170
171 //Display movies that has the same Name
172 static void givenName(String name, int low){
173 if (low > total-1){
174 return;
175 }
176 else if (name.equals(movies[low].getName())){
177 movies[low].displayRow();
178 givenName(name, low+1);
179 }
180 else {
181 givenName(name, low+1);
182 }
183 }
184
185 //(END) Recursive Functions ------------------------------------------------------------/
186
187 //Error Checking Functions -------------------------------------------------------------\
188 //used to check if the input is numeric
189 public static boolean isNumeric(String num){
190 num = num.trim();
191
192 if(num.matches("^[0-9]+") && !num.equals("")){
193 return true;
194
195 }
196 return false;
197
198 }
199
200 //Checks if the string has characters in it instead of just an empty space
201 public static boolean haveString(String string){
202 string = string.trim();
203
204 if(!string.equals("")){
205 return true;
206
207 }
208
209 return false;
210
211 }
212
213 //(END) Error Checking Functions -------------------------------------------------------/
214
215 //Selection Functions ------------------------------------------------------------------\
216 //Selecting genre
217 static String genreSelection(){
218 char choice;
219 String genre;
220
221 do{
222 genre = "";
223 System.out.println("*-------------------<Genres>------------------*");
224 System.out.println(" 1. Action 2. Adventure");
225 System.out.println(" 3. Comedy 4. Drama");
226 System.out.println(" 5. Horror 6. Sci-Fi");
227 System.out.println(" 7. Romance 8. Others");
228 System.out.println(" 0. Go Back");
229 System.out.println("*-------------------<******>------------------*\n");
230
231 System.out.print("Please enter your choice : ");
232
233 choice = input.next().charAt(0);
234
235 switch(choice){
236 case '1':
237 genre = "Action";
238 break;
239
240 case '2':
241 genre = "Adventure";
242 break;
243
244 case '3':
245 genre = "Comedy";
246 break;
247
248 case '4':
249 genre = "Drama";
250 break;
251
252 case '5':
253 genre = "Horror";
254 break;
255
256 case '6':
257 genre = "Sci-Fi";
258 break;
259
260 case '7':
261 genre = "Romance";
262 break;
263
264 case '8':
265 genre = "Others";
266 break;
267
268 case '0':
269 genre = "EXIT";
270 clearScreen();
271
272 break;
273
274 default:
275 System.out.print("\n'" + choice + "' is an invalid choice.\n");
276
277 System.out.print("\nPress enter to continue...");
278 input.skip("\n");
279 input.skip("\n");
280
281 clearScreen();
282 genre = "INVALID";
283
284 break;
285
286 }
287
288 }while(genre.equals("INVALID"));
289
290 return genre;
291 }
292
293 //Selecting age rating
294 static String ageRatingSelection(){
295 char choice;
296 String ageRating;
297 do{
298 ageRating = "";
299 System.out.println("*-----------------<Age Rating>----------------*");
300 System.out.println(" 1. G 2. PG");
301 System.out.println(" 3. PG-13 4. R");
302 System.out.println(" 0. Go Back");
303 System.out.println("*-----------------<**********>----------------*\n");
304
305 System.out.print("Please enter your choice : ");
306
307 choice = input.next().charAt(0);
308
309 switch(choice){
310 case '1':
311 ageRating = "G";
312 break;
313
314 case '2':
315 ageRating = "PG";
316 break;
317
318 case '3':
319 ageRating = "PG-13";
320 break;
321
322 case '4':
323 ageRating = "R";
324 break;
325
326 case '0':
327 ageRating = "EXIT";
328 clearScreen();
329
330 break;
331
332 default:
333 System.out.print("\n'" + choice + "' is an invalid choice.\n");
334
335 System.out.print("\nPress enter to continue...");
336 input.skip("\n");
337 input.skip("\n");
338
339 clearScreen();
340 ageRating = "INVALID";
341 break;
342 }
343 }while(ageRating.equals("INVALID"));
344
345 return ageRating;
346 }
347
348 //(END) Selection Functions ------------------------------------------------------------/
349
350 //Main Menu Option 1 : Add Default Records ---------------------------------------------\
351 public static void AddDefaultRecords(){
352 boolean valid;
353
354 //Temporarily store user input to check if its numeric
355 String numString;
356
357 int numOfDefaultMovies = 0;
358
359 input.skip("\n");
360
361 do{
362 valid = true;
363
364 System.out.println("Enter 0 to return to Main Menu.");
365
366 System.out.print("Please enter the number of default movies you want to add (Maximum : 5) : ");
367 numString = input.nextLine();
368
369 if(isNumeric(numString)){
370 numOfDefaultMovies = Integer.parseInt(numString);
371
372 if(numOfDefaultMovies == 0){
373 clearScreen();
374 return;
375
376 }else if(numOfDefaultMovies > 5){
377 System.out.println("\nThe maximum number of default movies that can be added at a time is 5.");
378
379 }else if((numOfDefaultMovies+total) > size){
380 System.out.println("\nERROR : Cannot have more than " + size + " movie records total.\n\nNumber of movies that can still be added : " + (size - total) + "\n");
381
382 }else{
383 switch(numOfDefaultMovies){
384 case 5:
385 movies[total] = new MovieClass(movieID, "Frozen 2", "Adventure", 103, "PG", "Anna, Elsa, Kristoff, Olaf and Sven leave Arendelle to travel to an ancient, autumn-bound forest of an enchanted land. They set out to find the origin of Elsa's powers in order to save their kingdom.");
386 total++;
387 movieID++;
388
389 case 4:
390 movies[total] = new MovieClass(movieID, "The Avengers", "Action", 143, "PG-13", "Earth's mightiest heroes must come together and learn to fight as a team if they are going to stop the mischievous Loki and his alien army from enslaving humanity.");
391 total++;
392 movieID++;
393
394 case 3:
395 movies[total] = new MovieClass(movieID, "Annabelle", "Horror", 99, "R", "A couple begins to experience terrifying supernatural occurrences involving a vintage doll shortly after their home is invaded by satanic cultists.");
396 total++;
397 movieID++;
398
399 case 2:
400 movies[total] = new MovieClass(movieID, "BoiBoiBoy: The Movie", "Action", 100, "G", "The movie follows BoBoiBoy and his friends on an adventure on a mysterious island to find Ochobot. The Ochobot has been kidnapped by a group of alien treasure hunters so that they could locate an ancient Power Sphere older than Ochobot. The quest leads BoBoiBoy to meet his toughest foe yet, an alien treasure hunter who is looking to harness the power from this sphere for his greedy needs.");
401 total++;
402 movieID++;
403
404 case 1:
405 movies[total] = new MovieClass(movieID, "Titanic", "Romance", 194, "PG-13", "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.");
406 total++;
407 movieID++;
408
409 }
410 System.out.println("\nDefault movies succesfully added.");
411
412 }
413
414 //Pause screen before clearing
415 System.out.print("\nPress enter to continue...");
416 input.skip("\n");
417
418 if(numOfDefaultMovies > 5){
419 clearScreen();
420 valid = false;
421
422 }
423
424 }else{
425 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).\n");
426
427 valid = false;
428
429 }
430
431 }while(!valid);
432
433 }
434
435 //(END) Main Menu Option 1 : Add Default Records ---------------------------------------/
436
437 //Main Menu Option 2 : Add Record ------------------------------------------------------\
438 public static void AddRecord(){
439 boolean valid;
440
441 //Temporarily store user input to check if its numeric
442 String numString;
443
444 String name;
445 String genre;
446 int duration;
447 String ageRating;
448 String description;
449
450 System.out.println("*--------------------<Add New Record>--------------------*");
451
452 System.out.println("\nMovie ID : " + movieID);
453 movies[total].setId(movieID);
454
455 input.skip("\n");
456
457 do{
458 //Name
459 valid = true;
460
461 System.out.println("\nPlease enter the name of the movie: ");
462 name = input.nextLine();
463
464 if(!haveString(name)){
465 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
466
467 valid = false;
468
469 }else{
470 movies[total].setName(name);
471
472 }
473
474 }while(!valid);
475
476 System.out.println("");
477
478 //Genre
479 genre = genreSelection();
480 if(genre.equals("EXIT")){
481 return;
482
483 }
484 movies[total].setGenre(genre);
485
486 System.out.println("");
487
488 //Age Rating
489 ageRating = ageRatingSelection();
490 if(ageRating.equals("EXIT")){
491 return;
492
493 }
494 movies[total].setAgeRating(ageRating);
495
496
497 input.skip("\n");
498
499 do{
500 //Duration
501 valid = true;
502
503 System.out.println("\nPlease enter the duration of the movie: ");
504 numString=input.nextLine();
505
506 if(isNumeric(numString)){
507 duration = Integer.parseInt(numString);
508 movies[total].setDuration(duration);
509
510 }else{
511 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).");
512
513 valid = false;
514
515 }
516
517 }while(!valid);
518
519 //Description
520 System.out.println("\nPlease enter the description of the movie: ");
521 description=input.nextLine();
522
523 if(!haveString(description)){
524 movies[total].setDescription("N/A");
525
526 }else{
527 movies[total].setDescription(description);
528
529 }
530
531 total++;
532 movieID++;
533
534 System.out.println("\n*--------------------<**************>--------------------*");
535
536 System.out.println("\nMovie succesfully added.");
537
538 System.out.println("\nTotal Movies : " + total);
539
540 //Pause screen before clearing
541 System.out.print("\nPress enter to continue...");
542 input.skip("\n");
543
544 clearScreen();
545
546 }
547
548 //(END) Main Menu Option 2 : Add Record ------------------------------------------------/
549
550 //Main Menu Option 3 : Update ----------------------------------------------------------\
551 public static void Update(){
552 boolean valid;
553 boolean goBackToUpdate; //to check wether to go back to Update Main Menu
554
555 char choice;
556 char updateChoice;
557 int index;
558
559 String numString; //Temporarily store user input to check if its numeric
560
561 do{
562 goBackToUpdate = false;
563
564 System.out.println("*-----------------------<Update>----------------------*");
565 System.out.println(" 1. Update by ID 2. Update by Name");
566 System.out.println(" 0. Go Back");
567 System.out.println("------------------------<******>----------------------*\n");
568
569 System.out.print("Please enter your choice : ");
570 choice= input.next().charAt(0);
571
572 clearScreen();
573
574 switch(choice){
575 case '1':
576 //Update by ID
577 int id = -1;
578
579 input.skip("\n");
580
581 do{
582 valid = true;
583
584 System.out.print("Please enter the ID of the movie : ");
585 numString=input.nextLine();
586
587 if(isNumeric(numString)){
588 id = Integer.parseInt(numString);
589
590 }else{
591 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).\n");
592
593 valid = false;
594
595 }
596 }while(!valid);
597
598 clearScreen();
599
600 index = searchId(id);
601
602 if(index == -1){
603 //If a movie with that ID cannot be found
604 System.out.println("\nThere are no movies that have the entered ID.");
605
606 //Pause screen before clearing
607 System.out.print("\nPress enter to continue...");
608 input.skip("\n");
609
610 clearScreen();
611
612 Update();
613
614 }else{
615 updateChoice = updateAttributes(index);
616 if(updateChoice == '0'){
617 goBackToUpdate = true;
618
619 }
620
621 }
622
623 break;
624
625 case '2':
626 //Update by Name
627 String name;
628 boolean found = false;
629
630 input.skip("\n");
631
632 do{
633 valid = true;
634
635 System.out.println("\nPlease enter the name of the movie you want to update: ");
636 name = input.nextLine();
637
638 if(!haveString(name)){
639 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
640
641 valid = false;
642
643 }
644
645 }while(!valid);
646
647 clearScreen();
648
649 //find the number of movies with the given name
650 int sumOfSameName = findTotalByName(name, total-1, 0);
651
652 if(sumOfSameName == 0){
653 //If there are no movies with the given name
654 System.out.println("\nThere are no movies named " + name + ".\n");
655
656 //Pause screen before clearing
657 System.out.print("\nPress enter to continue...");
658 input.skip("\n");
659
660 clearScreen();
661 Update();
662
663 }else if(sumOfSameName > 1){
664 //If there are more than 1 movie with the same name
665 clearScreen();
666
667 int idNum = -1;
668
669 do{
670 tableBottom();
671
672 //Table Name
673 System.out.printf("%49s %n","Update by Name");
674
675 tableTop();
676
677 //Search and display movies with the chosen Name
678 givenName(name, 0);
679
680 tableBottom();
681
682 do{
683 valid = true;
684
685 System.out.print("Please enter the ID of the movie you want to update : ");
686 numString=input.nextLine();
687
688 if(isNumeric(numString)){
689 idNum = Integer.parseInt(numString);
690
691 }else{
692 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).\n");
693
694 valid = false;
695
696 }
697
698 }while(!valid);
699
700 //find the index of the movie that has the entered ID
701 index = searchId(idNum);
702
703 if(index != -1 && name.equals(movies[index].getName())){
704 //if there is a movie with the same ID and name
705 clearScreen();
706
707 found = true;
708
709 updateAttributes(index);
710
711 }else{
712 //if there is no movie with the same ID and name
713
714 found = false;
715
716 System.out.print("'" + idNum +"' is an invalid choice.\n");
717
718 //Pause screen before clearing
719 System.out.print("\nPress enter to continue...");
720 input.skip("\n");
721
722 clearScreen();
723
724 }
725
726 }while(!found);
727
728 }else{
729 //There is only one movie with the same name
730 index = searchName(name);
731
732 updateAttributes(index);
733
734 }
735
736 break;
737
738 case '0':
739 return;
740
741 default:
742 System.out.println("'" + choice + "' is an invalid choice.\n");
743 Update();
744
745 }
746
747 }while(goBackToUpdate);
748
749 return;
750
751 }
752
753 static char updateAttributes(int index){
754 String numString; //Temporarily store user input to check if its numeric
755 boolean valid;
756 boolean updateMore; //check if user wants to update other attributes of the same movie
757
758 char choice;
759 char updateChoice; //choice of movie attribute
760 String choiceName = ""; //name of the selected attribute
761
762 String name = "";
763 String genre = "";
764 int duration = 0;
765 String ageRating = "";
766 String description = "";
767
768 do{ //loops if user wants to update other attributes of the same movie
769 updateMore = false;
770
771 do{ //checks if user entered
772 valid = true;
773
774 //If a movie with the same ID is found
775 movies[index].displayDetails();
776
777 //Ask user what you want to update
778 System.out.println("\n*---------------<Update '" + movies[index].getName() + "'>----------------------*");
779 System.out.println(" 1. Name 2. Genre");
780 System.out.println(" 3. Duration 4. Age Rating");
781 System.out.println(" 5. Description 0. Go Back");
782 System.out.println("-----------------------------<******>---------------------------*\n");
783
784 System.out.print("Please enter your choice : ");
785 updateChoice= input.next().charAt(0);
786
787 //switch case
788 switch(updateChoice){
789 case '1' :
790 choiceName = "Name";
791
792 //user input the name
793 input.skip("\n");
794 do{
795 valid = true;
796
797 System.out.println("\nPlease enter the name of the movie: ");
798 name = input.nextLine();
799
800 if(!haveString(name)){
801 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
802
803 valid = false;
804
805 }
806
807 }while(!valid);
808
809 break;
810
811 case '2':
812 choiceName = "Genre";
813
814 //Genre
815 genre = genreSelection();
816 if(genre.equals("EXIT")){
817 valid = false;
818 break;
819
820 }else{
821 movies[total].setGenre(genre);
822
823 }
824
825 break;
826
827 case '3':
828 choiceName = "Duration";
829
830 input.skip("\n");
831
832 do{
833 //Duration
834 valid = true;
835
836 System.out.println("\nPlease enter the duration of the movie: ");
837 numString=input.nextLine();
838
839 if(isNumeric(numString)){
840 duration = Integer.parseInt(numString);
841
842 }else{
843 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).");
844
845 valid = false;
846
847 }
848
849 }while(!valid);
850
851 break;
852
853 case '4':
854 choiceName = "Age Rating";
855
856 //Age Rating
857 ageRating = ageRatingSelection();
858 if(ageRating.equals("EXIT")){
859 valid = false;
860 break;
861
862 }else{
863 movies[total].setAgeRating(ageRating);
864
865 }
866
867 break;
868
869 case '5':
870 choiceName = "Description";
871 input.skip("\n");
872 System.out.println("\nPlease enter the description of the movie: ");
873 description = input.nextLine();
874
875 if(!haveString(description)){
876 description = "N/A";
877
878 }
879
880 break;
881
882 case '0':
883 clearScreen();
884 return updateChoice;
885
886 default:
887 System.out.println("'" + updateChoice + "' is an invalid choice.");
888 valid = false;
889
890 System.out.print("\nPress enter to continue...");
891 input.skip("\n");
892 input.skip("\n");
893
894 clearScreen();
895
896 }
897
898 }while(!valid);
899
900 clearScreen();
901 System.out.println("*--------------------<Updating " + choiceName + ">--------------------*\n");
902
903 if(updateChoice == '1'){
904 System.out.println(movies[index].getName() + "\t> Change To >\t" + name);
905
906 }else if(updateChoice == '2'){
907 System.out.println(movies[index].getGenre() + "\t> Change To >\t" + genre);
908
909 }else if(updateChoice == '3'){
910 System.out.println(movies[index].getDuration() + "\t> Change To >\t" + duration);
911
912 }else if(updateChoice == '4'){
913 System.out.println(movies[index].getAgeRating() + "\t> Change To >\t" + ageRating);
914
915 }else if(updateChoice == '5'){
916 System.out.println(movies[index].getDescription());
917 System.out.println("\nV Change To V\n");
918 System.out.println(description);
919 }
920
921 System.out.println("\n*--------------------<****************>--------------------*\n");
922
923 System.out.println("\nAre you sure you want to update this?(Y/N)");
924 choice=input.next().charAt(0);
925
926 if(choice == 'Y' || choice == 'y'){
927 if(updateChoice == '1'){
928 movies[index].setName(name);
929
930 }else if(updateChoice == '2'){
931 movies[index].setGenre(genre);
932
933 }else if(updateChoice == '3'){
934 movies[index].setDuration(duration);
935
936 }else if(updateChoice == '4'){
937 movies[index].setAgeRating(ageRating);
938
939 }else if(updateChoice == '5'){
940 movies[index].setDescription(description);
941
942 }
943
944 System.out.println("\nMovie succesfully updated.");
945
946 System.out.println("\nDo you want to update any other attributes of the same movie?(Y/N)");
947 choice = input.next().charAt(0);
948
949 if(choice == 'Y' || choice == 'y'){
950 updateMore = true;
951
952 System.out.print("\nPress enter to continue...");
953 input.skip("\n");
954 input.skip("\n");
955
956 clearScreen();
957
958 }else{
959 clearScreen();
960 return updateChoice;
961
962 }
963
964 }else{
965 clearScreen();
966 Update();
967
968 }
969
970 }while(updateMore);
971
972 return updateChoice;
973
974 }
975
976 //(END)Main Menu Option 3 : Update -----------------------------------------------------/
977
978 //Main Menu Option 4 : Delete ----------------------------------------------------------\
979 public static void Delete(){
980 boolean valid;
981
982 char choice;
983 int index;
984
985 //Temporarily store user input to check if its numeric
986 String numString;
987
988 System.out.println("*-----------------------<Delete>----------------------*");
989 System.out.println(" 1. Delete by ID 2. Delete by Name");
990 System.out.println(" 3. Delete All");
991 System.out.println(" 0. Go Back");
992 System.out.println("------------------------<******>----------------------*\n");
993
994 System.out.print("Please enter your choice : ");
995 choice= input.next().charAt(0);
996
997 clearScreen();
998
999 switch(choice){
1000 case '1':
1001 //Delete by ID
1002 int id = -1;
1003
1004 input.skip("\n");
1005
1006 do{
1007 valid = true;
1008
1009 System.out.print("Please enter the ID of the movie : ");
1010 numString = input.nextLine();
1011
1012 if(isNumeric(numString)){
1013 id = Integer.parseInt(numString);
1014
1015 }else{
1016 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).\n");
1017
1018 valid = false;
1019
1020 }
1021 }while(!valid);
1022
1023 clearScreen();
1024
1025 index = searchId(id);
1026
1027 if(index == -1){
1028 //If a movie with that ID cannot be found
1029 System.out.println("\nThere are no movies that have the entered ID.");
1030
1031 //Pause screen before clearing
1032 System.out.print("\nPress enter to continue...");
1033 input.skip("\n");
1034
1035 clearScreen();
1036
1037 Delete();
1038
1039 }else{
1040 deleteMovie(index);
1041
1042 }
1043
1044 break;
1045
1046 case '2':
1047 //Delete by Name
1048 String name;
1049 boolean found = false;
1050
1051 input.skip("\n");
1052
1053 do{
1054 valid = true;
1055
1056 System.out.println("\nPlease enter the name of the movie you want to delete: ");
1057 name = input.nextLine();
1058
1059 if(!haveString(name)){
1060 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
1061
1062 valid = false;
1063
1064 }
1065
1066 }while(!valid);
1067
1068 clearScreen();
1069
1070 //find the number of movies with the given name
1071 int sumOfSameName = findTotalByName(name, total-1, 0);
1072
1073 if(sumOfSameName == 0){
1074 //If there are no movies with the given name
1075 System.out.println("\nThere are no movies named " + name + ".\n");
1076
1077 //Pause screen before clearing
1078 System.out.print("\nPress enter to continue...");
1079 input.skip("\n");
1080
1081 clearScreen();
1082 Delete();
1083
1084 }else if(sumOfSameName > 1){
1085 //If there are more than 1 movie with the same name
1086 int idNum = -1;
1087
1088 do{
1089 tableBottom();
1090
1091 //Table Name
1092 System.out.printf("%49s %n","Delete by Name");
1093
1094 tableTop();
1095
1096 //Search and display movies with the chosen Name
1097 givenName(name, 0);
1098
1099 tableBottom();
1100
1101 do{
1102 valid = true;
1103
1104 System.out.print("Please enter the ID of the movie you want to delete : ");
1105 numString=input.nextLine();
1106
1107 if(isNumeric(numString)){
1108 idNum = Integer.parseInt(numString);
1109
1110 }else{
1111 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).\n");
1112
1113 valid = false;
1114
1115 }
1116
1117 }while(!valid);
1118
1119 //find the index of the movie that has the entered ID
1120 index = searchId(idNum);
1121
1122 if(index != -1 && name.equals(movies[index].getName())){
1123 //if there is a movie with the same ID and name
1124 clearScreen();
1125
1126 found = true;
1127
1128 deleteMovie(index);
1129
1130 }else{
1131 //if there is no movie with the same ID and name
1132
1133 found = false;
1134
1135 System.out.print("'" + idNum +"' is an invalid choice.\n");
1136
1137 //Pause screen before clearing
1138 System.out.print("\nPress enter to continue...");
1139 input.skip("\n");
1140
1141 clearScreen();
1142
1143 }
1144
1145 }while(!found);
1146
1147 }else{
1148 //There is only one movie with the same name
1149 index = searchName(name);
1150
1151 deleteMovie(index);
1152
1153 }
1154
1155 return;
1156
1157 case '3':
1158 System.out.println("\nAre you sure you want to delete all the movies?(Y/N)");
1159 choice=input.next().charAt(0);
1160
1161 if(choice == 'Y' || choice == 'y'){
1162 for(int i = 0;i<total;i++){
1163 movies[i] = new MovieClass();
1164
1165 }
1166
1167 total = 0;
1168
1169 System.out.println("\nAll Movies succesfully deleted.");
1170
1171 //Pause screen before clearing
1172 System.out.print("\nPress enter to continue...");
1173 input.skip("\n");
1174 input.skip("\n");
1175
1176 }else{
1177 clearScreen();
1178 Delete();
1179
1180 }
1181
1182 break;
1183
1184 case '0':
1185 return;
1186
1187 default:
1188 System.out.println("'" + choice + "' is an invalid choice.\n");
1189 Delete();
1190
1191 }
1192
1193 }
1194
1195 static void deleteMovie(int index){
1196 char choice;
1197
1198 //If a movie with the same ID is found
1199 movies[index].displayDetails();
1200
1201 System.out.println("\nAre you sure you want to delete this movie?(Y/N)");
1202 choice=input.next().charAt(0);
1203
1204 if(choice=='Y' || choice == 'y'){
1205 deleteById(index);
1206 total--;
1207
1208 System.out.println("Movie succesfully deleted.");
1209
1210 //Pause screen before clearing
1211 System.out.print("\nPress enter to continue...");
1212 input.skip("\n");
1213 input.skip("\n");
1214
1215 clearScreen();
1216 return;
1217
1218 }else{
1219 clearScreen();
1220 Delete();
1221
1222 }
1223 }
1224
1225 public static void deleteById (int index) {
1226 for (int j=index;j<total;j++){
1227 if(j<total-1){
1228 movies[j]=movies[j+1];
1229 }else{
1230 movies[j]=null;
1231 }
1232 }
1233 total--;
1234 }
1235
1236 //(END) Main Menu Option 4 : Delete ----------------------------------------------------/
1237
1238 //Main Menu Option 5 : View ------------------------------------------------------------\
1239 public static void View(){
1240 char choice;
1241
1242 System.out.println("*---------------------------------<View>--------------------------------*");
1243 System.out.println(" 1. View all(Table) 2. List all, given Genre(Table)");
1244 System.out.println(" 3. List all, given Age Rating(Table) 4. Search by Name(Detailed)");
1245 System.out.println(" 0. Go Back");
1246 System.out.println("*---------------------------------<****>--------------------------------*\n");
1247
1248 System.out.print("Please enter your choice : ");
1249 choice = input.next().charAt(0);
1250
1251 clearScreen();
1252
1253 switch(choice){
1254 //View All
1255 case '1':
1256 //Border
1257 for(int i = 0; i<91; i++){
1258 System.out.print("=");
1259 }
1260
1261 System.out.println("");
1262
1263 //Table Name
1264 System.out.printf("%49s %n","View All");
1265
1266 viewAll();
1267
1268 //Pause screen before clearing
1269 System.out.print("\nPress enter to continue...");
1270 input.skip("\n");
1271 input.skip("\n");
1272
1273 break;
1274
1275 //List all by Genre
1276 case '2':
1277 String genre;
1278
1279 do{
1280 //Genre
1281 genre = genreSelection();
1282 if(genre.equals("EXIT")){
1283 View();
1284 break;
1285
1286 }
1287
1288 clearScreen();
1289
1290 if(searchGenre(genre)){
1291 //If there are movies with the chosen genre
1292 //Border
1293 for(int i = 0; i<91; i++){
1294 System.out.print("=");
1295 }
1296
1297 System.out.println("");
1298
1299 //Table Name
1300 System.out.printf("%49s %n",genre + " Movie(s)");
1301
1302 tableTop();
1303
1304 //Search and display movies with the chosen genre
1305 givenGenre(genre, 0);
1306
1307 tableBottom();
1308
1309 //Pause screen before clearing
1310 System.out.print("\nPress enter to continue...");
1311 input.skip("\n");
1312 input.skip("\n");
1313
1314 clearScreen();
1315
1316 }else{
1317 //If there are no movies with the chosen genre
1318 System.out.println("\nThere are no " + genre + " movies.");
1319
1320 //Pause screen before clearing
1321 System.out.print("\nPress enter to continue...");
1322 input.skip("\n");
1323 input.skip("\n");
1324
1325 clearScreen();
1326 View();
1327 }
1328
1329 }while(!searchGenre(genre));
1330
1331 break;
1332
1333 //List all by Age Rating
1334 case '3':
1335 String ageRating;
1336 do{
1337 //Age Rating
1338 ageRating = ageRatingSelection();
1339 if(ageRating.equals("EXIT")){
1340 View();
1341 return;
1342
1343 }
1344
1345 clearScreen();
1346
1347 if(searchAgeRating(ageRating)){
1348 //If there are movies with the chosen age rating
1349 //Border
1350 for(int i = 0; i<91; i++){
1351 System.out.print("=");
1352 }
1353
1354 System.out.println("");
1355
1356 //Table Name
1357 System.out.printf("%49s %n",ageRating + " Movie(s)");
1358
1359 tableTop();
1360
1361 //Search and display movies with the chosen Age Rating
1362 givenAgeRating(ageRating, 0);
1363
1364 tableBottom();
1365
1366 }else{
1367 //If there are no movies with the chosen age rating
1368 System.out.println("\nThere are no " + ageRating + " movies.");
1369
1370 //Pause screen before clearing
1371 System.out.print("\nPress enter to continue...");
1372 input.skip("\n");
1373 input.skip("\n");
1374
1375 clearScreen();
1376 View();
1377
1378 }
1379
1380 }while(!searchAgeRating(ageRating));
1381
1382 //Pause screen before clearing
1383 System.out.print("\nPress enter to continue...");
1384 input.skip("\n");
1385 input.skip("\n");
1386
1387 break;
1388
1389 //Search by Name
1390 case '4':
1391 boolean valid;
1392 String name;
1393
1394 input.skip("\n");
1395
1396 do{
1397 valid = true;
1398
1399 System.out.println("\nPlease enter the name of the movie you want to view in detail: ");
1400 name = input.nextLine();
1401
1402 if(!haveString(name)){
1403 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
1404
1405 valid = false;
1406
1407 }
1408
1409 }while(!valid);
1410
1411 //find the number of movies with the given name
1412 int sumOfSameName = findTotalByName(name, total-1, 0);
1413
1414 if(sumOfSameName == 0){
1415 //If there are no movies with the given name
1416 System.out.println("\nThere are no movies named " + name + ".\n");
1417
1418 //Pause screen before clearing
1419 System.out.print("\nPress enter to continue...");
1420 input.skip("\n");
1421
1422 clearScreen();
1423 View();
1424
1425 }else if(sumOfSameName > 1){
1426 //If there are more than 1 movie with the same name
1427
1428 clearScreen();
1429
1430 String numString;
1431 int idNum = -1;
1432 int index;
1433
1434 do{
1435 tableBottom();
1436 //Table Name
1437 System.out.printf("%49s %n","Search by Name");
1438
1439 tableTop();
1440
1441 //Search and display movies with the chosen Name
1442 givenName(name, 0);
1443
1444 tableBottom();
1445
1446 do{
1447 valid = true;
1448
1449 System.out.print("Please enter the ID of the movie you want to update : ");
1450 numString=input.nextLine();
1451
1452 if(isNumeric(numString)){
1453 idNum = Integer.parseInt(numString);
1454
1455 }else{
1456 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).\n");
1457
1458 valid = false;
1459
1460 }
1461
1462 }while(!valid);
1463
1464 //find the index of the movie that has the entered ID
1465 index = searchId(idNum);
1466
1467 if(index != -1 && name.equals(movies[index].getName())){
1468 //if there is a movie with the same ID and name
1469 movies[index].displayDetails();
1470
1471 //Pause screen before clearing
1472 System.out.print("\nPress enter to continue...");
1473 input.skip("\n");
1474 input.skip("\n");
1475
1476 clearScreen();
1477
1478 }else{
1479 //if there is no movie with the same ID and name
1480 System.out.print("'" + idNum +"' is an invalid choice.\n");
1481
1482 //Pause screen before clearing
1483 System.out.print("\nPress enter to continue...");
1484 input.skip("\n");
1485
1486 clearScreen();
1487
1488 }
1489
1490 }while(!(index != -1 && name.equals(movies[index].getName())));
1491
1492 }else{
1493 //There is only one movie with the same name
1494 movies[searchName(name)].displayDetails();
1495 //Pause screen before clearing
1496 System.out.print("\nPress enter to continue...");
1497 input.skip("\n");
1498 }
1499
1500 break;
1501
1502 //Go Back to Main Menu
1503 case '0':
1504 break;
1505
1506 default:
1507 System.out.println("'" + choice + "' is an invalid choice.\n");
1508 View();
1509 }
1510
1511 clearScreen();
1512
1513 return;
1514
1515 }
1516
1517 //View Menu Option 1 : View All
1518 public static void viewAll(){
1519 tableTop();
1520
1521 //Display each row of recorded movies
1522 for(int i = 0; i<total; i++){
1523 movies[i].displayRow();
1524 }
1525
1526 tableBottom();
1527 }
1528
1529 //(END) Main Menu Option 5 : View ------------------------------------------------------/
1530
1531 //Main Menu Option 6 : Create Text File ------------------------------------------------\
1532 static void CreateTextFileMenu(){
1533 char choice;
1534
1535 System.out.println("*------------------<Create Text File>------------------*");
1536 System.out.println(" 1. Table Format 2. Detailed Format");
1537 System.out.println(" 0. Go Back");
1538 System.out.println("*------------------<****************>------------------*\n");
1539
1540 System.out.print("Please enter your choice : ");
1541
1542 choice = input.next().charAt(0);
1543
1544 clearScreen();
1545
1546 switch(choice){
1547 case '1':
1548 CreateTextFileFunction(1);
1549
1550 break;
1551
1552 case '2':
1553 CreateTextFileFunction(2);
1554
1555 break;
1556
1557 case '0':
1558 return;
1559
1560 default:
1561 System.out.print("\n'" + choice + "' is an invalid choice.\n");
1562
1563 System.out.print("\nPress enter to continue...");
1564 input.skip("\n");
1565 input.skip("\n");
1566
1567 clearScreen();
1568
1569 CreateTextFileMenu();
1570 break;
1571 }
1572 }
1573
1574 static void CreateTextFileFunction(int textFileFormat){
1575 boolean valid;
1576 String fileName;
1577
1578 input.skip("\n");
1579
1580 do{
1581 valid = true;
1582
1583 System.out.print("\nPlease enter what you want to name your file : ");
1584 fileName = input.nextLine();
1585
1586 if(!haveString(fileName)){
1587 System.out.println("\nERROR : Empty spaces are not accepted as file names.");
1588
1589 valid = false;
1590
1591 }
1592
1593 }while(!valid);
1594
1595 fileName += ".txt";
1596
1597 try {
1598 File myFile = new File(fileName);
1599 if(myFile.createNewFile()){
1600 System.out.println("File succesfully created : " + myFile.getName());
1601
1602 FileWriter fileWriter = new FileWriter(fileName);
1603 PrintWriter printWriter = new PrintWriter(fileWriter);
1604
1605 if(textFileFormat == 1){
1606 //Text File : Table Format
1607
1608 //Border
1609 for(int i = 0; i<91; i++){
1610 printWriter.print("=");
1611 }
1612
1613 printWriter.println("\nMovies Management System : Movie Records in Table Format");
1614
1615 //Table Border
1616 for(int i = 0; i<91; i++){
1617 printWriter.print("=");
1618 }
1619
1620 printWriter.println("");
1621
1622 //Table Header
1623 printWriter.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
1624
1625 //Table Divider
1626 printWriter.print("|");
1627 for(int i = 0; i<89; i++){
1628 printWriter.print("-");
1629 }
1630 printWriter.println("|");
1631
1632 //Display each row of recorded movies
1633 for(int i = 0; i<total; i++){
1634 movies[i].writeTxtFileRow(printWriter);
1635 }
1636
1637 //Table Bottom Border
1638 for(int i = 0; i<91; i++){
1639 printWriter.print("=");
1640 }
1641
1642 }else{
1643 //Text File : Detailed Format
1644
1645 //Border
1646 for(int i = 0; i<91; i++){
1647 printWriter.print("=");
1648 }
1649
1650 printWriter.println("\nMovies Management System : Movie Records in Detailed Format");
1651
1652 //Border
1653 for(int i = 0; i<91; i++){
1654 printWriter.print("=");
1655 }
1656
1657 for(int i = 0; i<total; i++){
1658 movies[i].writeTxtFileDetails(printWriter);
1659 //Border
1660 for(int j = 0; j<91; j++){
1661 printWriter.print("=");
1662 }
1663 }
1664 }
1665
1666 printWriter.close();
1667
1668 System.out.print("\nPress enter to continue...");
1669 input.skip("\n");
1670
1671 }else{
1672 System.out.println("File already exists.");
1673
1674 System.out.print("\nPress enter to continue...");
1675 input.skip("\n");
1676
1677 clearScreen();
1678 CreateTextFileMenu();
1679
1680 }
1681
1682 }catch(Exception e){
1683 System.out.println("ERROR : " + e);
1684
1685 }
1686 }
1687
1688 //(END) Main Menu Option 6 : Create Text File ------------------------------------------/
1689
1690 //Main Menu Option 6 : Import Text File Movies -----------------------------------------\
1691 static void ReadTextFile(){
1692 boolean valid;
1693
1694 String fileName;
1695
1696 //used to count and skip the first 3 lines
1697 int countSkip = 0;
1698
1699 //used to count the lines after the first 3 lines
1700 int countLines = 0;
1701
1702 //used to count how many movie records are in the .txt file
1703 int totalTxtFileMovies = 0;
1704
1705 //class attributes
1706 String name = "";
1707
1708 String genre = "";
1709
1710 String durationString = ""; //temporarily store the substring of the duration
1711 int duration = 0;
1712
1713 String ageRating = "";
1714
1715 String description = "";
1716
1717 input.skip("\n");
1718
1719 do{
1720 valid = true;
1721
1722 System.out.println("NOTE : Can only import movies from the DETAILED format text files that were created by this program.");
1723 System.out.println("\nPlase enter the name of the text file (without the .txt extension) : ");
1724 fileName = input.nextLine();
1725
1726 if(!haveString(fileName)){
1727 System.out.println("\nERROR : Empty spaces are not accepted as file names.");
1728
1729 valid = false;
1730
1731 }
1732
1733 }while(!valid);
1734
1735 clearScreen();
1736
1737 try{
1738 File myFile = new File(fileName + ".txt");
1739 Scanner myReader = new Scanner( myFile);
1740
1741 String fileLine;
1742
1743 while(myReader.hasNextLine()){
1744 fileLine = myReader.nextLine();
1745 countSkip++;
1746
1747 if(countSkip == 2 && !fileLine.equals("Movies Management System : Movie Records in Detailed Format")){
1748 throw new WrongFormatException("Only the detailed format text file that was created from this program can be used.");
1749
1750 }
1751
1752 //skip first 3 lines
1753 if(countSkip>3){
1754 countLines++;
1755 if(countLines%15 == 4){
1756 //Get the Name
1757 name = fileLine.substring(14);
1758
1759 }else if(countLines%15 == 6){
1760 //Get the Genre
1761 genre = fileLine.substring(10);
1762
1763 }else if(countLines%15 == 8){
1764 //Get the Duration
1765 durationString = fileLine.substring(12);
1766 duration = Integer.parseInt(durationString);
1767
1768 }else if(countLines%15 == 10){
1769 //Get the Age Rating
1770 ageRating = fileLine.substring(14);
1771
1772 }else if(countLines%15 == 13){
1773 //Get the Description
1774 description = fileLine;
1775
1776 }else if(countLines%15 == 0){
1777 //After going through the details of a movie record
1778 //Add the movie record
1779 movies[total] = new MovieClass(movieID, name, genre, duration, ageRating, description);
1780
1781 total++;
1782 movieID++;
1783
1784 totalTxtFileMovies++;
1785
1786 }
1787
1788 }
1789
1790 }
1791
1792 System.out.println("Movies succesfully imported.\n\nTotal movies imported : " + totalTxtFileMovies);
1793
1794 myReader.close();
1795
1796 }catch(Exception e){
1797 System.out.println("ERROR : " + e);
1798
1799 }
1800
1801 System.out.print("\nPress enter to continue...");
1802 input.skip("\n");
1803
1804 }
1805
1806 //(END) Main Menu Option 6 : Import Text File Movies ---------------------------------------/
1807
1808}