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