· 5 years ago · Mar 11, 2020, 08:16 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 Movie movies[] = new Movie[size]; //array of Movie 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 Movie();
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 Movie(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 Movie(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 Movie(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 Movie(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 Movie(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 Movie();
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 boolean notFound;
1242
1243
1244 do{
1245 notFound = false;
1246 System.out.println("*---------------------------------<View>--------------------------------*");
1247 System.out.println(" 1. View all(Table) 2. List all, given Genre(Table)");
1248 System.out.println(" 3. List all, given Age Rating(Table) 4. Search by Name(Detailed)");
1249 System.out.println(" 0. Go Back");
1250 System.out.println("*---------------------------------<****>--------------------------------*\n");
1251
1252 System.out.print("Please enter your choice : ");
1253 choice = input.next().charAt(0);
1254
1255 clearScreen();
1256
1257 switch(choice){
1258 //View All
1259 case '1':
1260 //Border
1261 for(int i = 0; i<91; i++){
1262 System.out.print("=");
1263 }
1264
1265 System.out.println("");
1266
1267 //Table Name
1268 System.out.printf("%49s %n","View All");
1269
1270 viewAll();
1271
1272 //Pause screen before clearing
1273 System.out.print("\nPress enter to continue...");
1274 input.skip("\n");
1275 input.skip("\n");
1276
1277 break;
1278
1279 //List all by Genre
1280 case '2':
1281 String genre;
1282
1283 do{
1284 //Genre
1285 genre = genreSelection();
1286 if(genre.equals("EXIT")){
1287 notFound = true;
1288 break;
1289
1290 }
1291
1292 clearScreen();
1293
1294 if(searchGenre(genre)){
1295 //If there are movies with the chosen genre
1296 //Border
1297 for(int i = 0; i<91; i++){
1298 System.out.print("=");
1299 }
1300
1301 System.out.println("");
1302
1303 //Table Name
1304 System.out.printf("%49s %n",genre + " Movie(s)");
1305
1306 tableTop();
1307
1308 //Search and display movies with the chosen genre
1309 givenGenre(genre, 0);
1310
1311 tableBottom();
1312
1313 //Pause screen before clearing
1314 System.out.print("\nPress enter to continue...");
1315 input.skip("\n");
1316 input.skip("\n");
1317
1318 clearScreen();
1319
1320 }else{
1321 //If there are no movies with the chosen genre
1322 System.out.println("\nThere are no " + genre + " movies.");
1323
1324 //Pause screen before clearing
1325 System.out.print("\nPress enter to continue...");
1326 input.skip("\n");
1327 input.skip("\n");
1328
1329 clearScreen();
1330
1331 notFound = true;
1332 break;
1333 }
1334
1335 }while(!searchGenre(genre));
1336
1337 break;
1338
1339 //List all by Age Rating
1340 case '3':
1341 String ageRating;
1342 do{
1343 //Age Rating
1344 ageRating = ageRatingSelection();
1345 if(ageRating.equals("EXIT")){
1346 notFound = true;
1347 break;
1348
1349 }
1350
1351 clearScreen();
1352
1353 if(searchAgeRating(ageRating)){
1354 //If there are movies with the chosen age rating
1355 //Border
1356 for(int i = 0; i<91; i++){
1357 System.out.print("=");
1358 }
1359
1360 System.out.println("");
1361
1362 //Table Name
1363 System.out.printf("%49s %n",ageRating + " Movie(s)");
1364
1365 tableTop();
1366
1367 //Search and display movies with the chosen Age Rating
1368 givenAgeRating(ageRating, 0);
1369
1370 tableBottom();
1371
1372 }else{
1373 //If there are no movies with the chosen age rating
1374 System.out.println("\nThere are no " + ageRating + " movies.");
1375
1376 notFound = true;
1377 break;
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 clearScreen();
1389
1390 break;
1391
1392 //Search by Name
1393 case '4':
1394 boolean valid;
1395 String name;
1396
1397 input.skip("\n");
1398
1399 do{
1400 valid = true;
1401
1402 System.out.println("\nPlease enter the name of the movie you want to view in detail: ");
1403 name = input.nextLine();
1404
1405 if(!haveString(name)){
1406 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
1407
1408 valid = false;
1409
1410 }
1411
1412 }while(!valid);
1413
1414 //find the number of movies with the given name
1415 int sumOfSameName = findTotalByName(name, total-1, 0);
1416
1417 if(sumOfSameName == 0){
1418 //If there are no movies with the given name
1419 System.out.println("\nThere are no movies named " + name + ".\n");
1420
1421 //Pause screen before clearing
1422 System.out.print("\nPress enter to continue...");
1423 input.skip("\n");
1424
1425 clearScreen();
1426 notFound = true;
1427 break;
1428
1429 }else if(sumOfSameName > 1){
1430 //If there are more than 1 movie with the same name
1431
1432 clearScreen();
1433
1434 String numString;
1435 int idNum = -1;
1436 int index;
1437
1438 do{
1439 tableBottom();
1440 //Table Name
1441 System.out.printf("%49s %n","Search by Name");
1442
1443 tableTop();
1444
1445 //Search and display movies with the chosen Name
1446 givenName(name, 0);
1447
1448 tableBottom();
1449
1450 do{
1451 valid = true;
1452
1453 System.out.print("Please enter the ID of the movie you want to update : ");
1454 numString=input.nextLine();
1455
1456 if(isNumeric(numString)){
1457 idNum = Integer.parseInt(numString);
1458
1459 }else{
1460 System.out.println("\nERROR : Please enter a positive integer (characters & symbols are not accepted).\n");
1461
1462 valid = false;
1463
1464 }
1465
1466 }while(!valid);
1467
1468 //find the index of the movie that has the entered ID
1469 index = searchId(idNum);
1470
1471 if(index != -1 && name.equals(movies[index].getName())){
1472 //if there is a movie with the same ID and name
1473 movies[index].displayDetails();
1474
1475 //Pause screen before clearing
1476 System.out.print("\nPress enter to continue...");
1477 input.skip("\n");
1478 input.skip("\n");
1479
1480 clearScreen();
1481
1482 }else{
1483 //if there is no movie with the same ID and name
1484 System.out.print("'" + idNum +"' is an invalid choice.\n");
1485
1486 //Pause screen before clearing
1487 System.out.print("\nPress enter to continue...");
1488 input.skip("\n");
1489
1490 clearScreen();
1491
1492 }
1493
1494 }while(!(index != -1 && name.equals(movies[index].getName())));
1495
1496 }else{
1497 //There is only one movie with the same name
1498 movies[searchName(name)].displayDetails();
1499 //Pause screen before clearing
1500 System.out.print("\nPress enter to continue...");
1501 input.skip("\n");
1502 }
1503
1504 break;
1505
1506 //Go Back to Main Menu
1507 case '0':
1508 break;
1509
1510 default:
1511 System.out.println("'" + choice + "' is an invalid choice.\n");
1512 notFound = true;
1513 break;
1514
1515 }
1516
1517 }while(notFound);
1518
1519 clearScreen();
1520
1521 return;
1522
1523 }
1524
1525 //View Menu Option 1 : View All
1526 public static void viewAll(){
1527 tableTop();
1528
1529 //Display each row of recorded movies
1530 for(int i = 0; i<total; i++){
1531 movies[i].displayRow();
1532 }
1533
1534 tableBottom();
1535 }
1536
1537 //(END) Main Menu Option 5 : View ------------------------------------------------------/
1538
1539 //Main Menu Option 6 : Create Text File ------------------------------------------------\
1540 static void CreateTextFileMenu(){
1541 char choice;
1542
1543 System.out.println("*------------------<Create Text File>------------------*");
1544 System.out.println(" 1. Table Format 2. Detailed Format");
1545 System.out.println(" 0. Go Back");
1546 System.out.println("*------------------<****************>------------------*\n");
1547
1548 System.out.print("Please enter your choice : ");
1549
1550 choice = input.next().charAt(0);
1551
1552 clearScreen();
1553
1554 switch(choice){
1555 case '1':
1556 CreateTextFileFunction(1);
1557
1558 break;
1559
1560 case '2':
1561 CreateTextFileFunction(2);
1562
1563 break;
1564
1565 case '0':
1566 return;
1567
1568 default:
1569 System.out.print("\n'" + choice + "' is an invalid choice.\n");
1570
1571 System.out.print("\nPress enter to continue...");
1572 input.skip("\n");
1573 input.skip("\n");
1574
1575 clearScreen();
1576
1577 CreateTextFileMenu();
1578 break;
1579 }
1580 }
1581
1582 static void CreateTextFileFunction(int textFileFormat){
1583 boolean valid;
1584 String fileName;
1585
1586 input.skip("\n");
1587
1588 do{
1589 valid = true;
1590
1591 System.out.print("\nPlease enter what you want to name your file : ");
1592 fileName = input.nextLine();
1593
1594 if(!haveString(fileName)){
1595 System.out.println("\nERROR : Empty spaces are not accepted as file names.");
1596
1597 valid = false;
1598
1599 }
1600
1601 }while(!valid);
1602
1603 fileName += ".txt";
1604
1605 try {
1606 File myFile = new File(fileName);
1607 if(myFile.createNewFile()){
1608 System.out.println("File succesfully created : " + myFile.getName());
1609
1610 FileWriter fileWriter = new FileWriter(fileName);
1611 PrintWriter printWriter = new PrintWriter(fileWriter);
1612
1613 if(textFileFormat == 1){
1614 //Text File : Table Format
1615
1616 //Border
1617 for(int i = 0; i<91; i++){
1618 printWriter.print("=");
1619 }
1620
1621 printWriter.println("\nMovies Management System : Movie Records in Table Format");
1622
1623 //Table Border
1624 for(int i = 0; i<91; i++){
1625 printWriter.print("=");
1626 }
1627
1628 printWriter.println("");
1629
1630 //Table Header
1631 printWriter.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
1632
1633 //Table Divider
1634 printWriter.print("|");
1635 for(int i = 0; i<89; i++){
1636 printWriter.print("-");
1637 }
1638 printWriter.println("|");
1639
1640 //Display each row of recorded movies
1641 for(int i = 0; i<total; i++){
1642 movies[i].writeTxtFileRow(printWriter);
1643 }
1644
1645 //Table Bottom Border
1646 for(int i = 0; i<91; i++){
1647 printWriter.print("=");
1648 }
1649
1650 }else{
1651 //Text File : Detailed Format
1652
1653 //Border
1654 for(int i = 0; i<91; i++){
1655 printWriter.print("=");
1656 }
1657
1658 printWriter.println("\nMovies Management System : Movie Records in Detailed Format");
1659
1660 //Border
1661 for(int i = 0; i<91; i++){
1662 printWriter.print("=");
1663 }
1664
1665 for(int i = 0; i<total; i++){
1666 movies[i].writeTxtFileDetails(printWriter);
1667 //Border
1668 for(int j = 0; j<91; j++){
1669 printWriter.print("=");
1670 }
1671 }
1672 }
1673
1674 printWriter.close();
1675
1676 System.out.print("\nPress enter to continue...");
1677 input.skip("\n");
1678
1679 }else{
1680 System.out.println("File already exists.");
1681
1682 System.out.print("\nPress enter to continue...");
1683 input.skip("\n");
1684
1685 clearScreen();
1686 CreateTextFileMenu();
1687
1688 }
1689
1690 }catch(Exception e){
1691 System.out.println("ERROR : " + e);
1692
1693 }
1694 }
1695
1696 //(END) Main Menu Option 6 : Create Text File ------------------------------------------/
1697
1698 //Main Menu Option 6 : Import Text File Movies -----------------------------------------\
1699 static void ReadTextFile(){
1700 boolean valid;
1701
1702 String fileName;
1703
1704 //used to count and skip the first 3 lines
1705 int countSkip = 0;
1706
1707 //used to count the lines after the first 3 lines
1708 int countLines = 0;
1709
1710 //used to count how many movie records are in the .txt file
1711 int totalTxtFileMovies = 0;
1712
1713 //class attributes
1714 String name = "";
1715
1716 String genre = "";
1717
1718 String durationString = ""; //temporarily store the substring of the duration
1719 int duration = 0;
1720
1721 String ageRating = "";
1722
1723 String description = "";
1724
1725 input.skip("\n");
1726
1727 do{
1728 valid = true;
1729
1730 System.out.println("NOTE : Can only import movies from the DETAILED format text files that were created by this program.");
1731 System.out.println("\nPlase enter the name of the text file (without the .txt extension) : ");
1732 fileName = input.nextLine();
1733
1734 if(!haveString(fileName)){
1735 System.out.println("\nERROR : Empty spaces are not accepted as file names.");
1736
1737 valid = false;
1738
1739 }
1740
1741 }while(!valid);
1742
1743 clearScreen();
1744
1745 try{
1746 File myFile = new File(fileName + ".txt");
1747 Scanner myReader = new Scanner( myFile);
1748
1749 String fileLine;
1750
1751 while(myReader.hasNextLine()){
1752 fileLine = myReader.nextLine();
1753 countSkip++;
1754
1755 if(countSkip == 2 && !fileLine.equals("Movies Management System : Movie Records in Detailed Format")){
1756 throw new WrongFormatException("Only the detailed format text file that was created from this program can be used.");
1757
1758 }
1759
1760 //skip first 3 lines
1761 if(countSkip>3){
1762 countLines++;
1763 if(countLines%15 == 4){
1764 //Get the Name
1765 name = fileLine.substring(14);
1766
1767 }else if(countLines%15 == 6){
1768 //Get the Genre
1769 genre = fileLine.substring(10);
1770
1771 }else if(countLines%15 == 8){
1772 //Get the Duration
1773 durationString = fileLine.substring(12);
1774 duration = Integer.parseInt(durationString);
1775
1776 }else if(countLines%15 == 10){
1777 //Get the Age Rating
1778 ageRating = fileLine.substring(14);
1779
1780 }else if(countLines%15 == 13){
1781 //Get the Description
1782 description = fileLine;
1783
1784 }else if(countLines%15 == 0){
1785 //After going through the details of a movie record
1786 //Add the movie record
1787 movies[total] = new Movie(movieID, name, genre, duration, ageRating, description);
1788
1789 total++;
1790 movieID++;
1791
1792 totalTxtFileMovies++;
1793
1794 }
1795
1796 }
1797
1798 }
1799
1800 System.out.println("Movies succesfully imported.\n\nTotal movies imported : " + totalTxtFileMovies);
1801
1802 myReader.close();
1803
1804 }catch(Exception e){
1805 System.out.println("ERROR : " + e);
1806
1807 }
1808
1809 System.out.print("\nPress enter to continue...");
1810 input.skip("\n");
1811
1812 }
1813
1814 //(END) Main Menu Option 6 : Import Text File Movies ---------------------------------------/
1815}