· 5 years ago · Mar 10, 2020, 08:38 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 high){
131 if (high < 0){
132 return;
133 }
134 else if (genre.equals(movies[high].getGenre())){
135 movies[high].displayRow();
136 givenGenre(genre, high-1);
137 }
138 else {
139 givenGenre(genre, high-1);
140 }
141 }
142
143 //Display movies that has the same AgeRating
144 static void givenAgeRating(String ageRating, int high){
145 if (high < 0){
146 return;
147 }
148 else if (ageRating.equals(movies[high].getAgeRating())){
149 movies[high].displayRow();
150 givenAgeRating(ageRating, high-1);
151 }
152 else {
153 givenAgeRating(ageRating, high-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 high){
173 if (high < 0){
174 return;
175 }
176 else if (name.equals(movies[high].getName())){
177 movies[high].displayRow();
178 givenName(name, high-1);
179 }
180 else {
181 givenName(name, high-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 number (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 number (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 number (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 //find the number of movies with the given name
648 int sumOfSameName = findTotalByName(name, total-1, 0);
649
650 if(sumOfSameName == 0){
651 //If there are no movies with the given name
652 System.out.println("\nThere are no movies named " + name + ".\n");
653
654 //Pause screen before clearing
655 System.out.print("\nPress enter to continue...");
656 input.skip("\n");
657
658 clearScreen();
659 Update();
660
661 }else if(sumOfSameName > 1){
662 //If there are more than 1 movie with the same name
663 clearScreen();
664
665 int idNum = -1;
666
667 do{
668 //Table Name
669 System.out.printf("%49s %n","Update by Name");
670
671 tableTop();
672
673 //Search and display movies with the chosen Name
674 givenName(name, total-1);
675
676 tableBottom();
677
678 do{
679 valid = true;
680
681 System.out.print("Please enter the ID of the movie you want to update : ");
682 numString=input.nextLine();
683
684 if(isNumeric(numString)){
685 idNum = Integer.parseInt(numString);
686
687 }else{
688 System.out.println("\nERROR : Please enter a number (characters & symbols are not accepted).\n");
689
690 valid = false;
691
692 }
693
694 }while(!valid);
695
696 //find the index of the movie that has the entered ID
697 index = searchId(idNum);
698
699 if(index != -1 && name.equals(movies[index].getName())){
700 //if there is a movie with the same ID and name
701 clearScreen();
702
703 found = true;
704
705 updateAttributes(index);
706
707 }else{
708 //if there is no movie with the same ID and name
709
710 found = false;
711
712 System.out.print("'" + idNum +"' is an invalid choice.\n");
713
714 //Pause screen before clearing
715 System.out.print("\nPress enter to continue...");
716 input.skip("\n");
717
718 clearScreen();
719
720 }
721
722 }while(!found);
723
724 }else{
725 //There is only one movie with the same name
726 index = searchName(name);
727
728 updateAttributes(index);
729
730 }
731
732 break;
733
734 case '0':
735 return;
736
737 default:
738 System.out.println("'" + choice + "' is an invalid choice.\n");
739 Update();
740
741 }
742
743 }while(goBackToUpdate);
744
745 return;
746
747 }
748
749 static char updateAttributes(int index){
750 String numString; //Temporarily store user input to check if its numeric
751 boolean valid;
752 boolean updateMore; //check if user wants to update other attributes of the same movie
753
754 char choice;
755 char updateChoice; //choice of movie attribute
756 String choiceName = ""; //name of the selected attribute
757
758 String name = "";
759 String genre = "";
760 int duration = 0;
761 String ageRating = "";
762 String description = "";
763
764 do{ //loops if user wants to update other attributes of the same movie
765 updateMore = false;
766
767 do{ //checks if user entered
768 valid = true;
769
770 //If a movie with the same ID is found
771 movies[index].displayDetails();
772
773 //Ask user what you want to update
774 System.out.println("\n*---------------<Update '" + movies[index].getName() + "'>----------------------*");
775 System.out.println(" 1. Name 2. Genre");
776 System.out.println(" 3. Duration 4. Age Rating");
777 System.out.println(" 5. Description 0. Go Back");
778 System.out.println("-----------------------------<******>---------------------------*\n");
779
780 System.out.print("Please enter your choice : ");
781 updateChoice= input.next().charAt(0);
782
783 //switch case
784 switch(updateChoice){
785 case '1' :
786 choiceName = "Name";
787
788 //user input the name
789 input.skip("\n");
790 do{
791 valid = true;
792
793 System.out.println("\nPlease enter the name of the movie: ");
794 name = input.nextLine();
795
796 if(!haveString(name)){
797 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
798
799 valid = false;
800
801 }
802
803 }while(!valid);
804
805 break;
806
807 case '2':
808 choiceName = "Genre";
809
810 //Genre
811 genre = genreSelection();
812 if(genre.equals("EXIT")){
813 valid = false;
814 break;
815
816 }else{
817 movies[total].setGenre(genre);
818
819 }
820
821 break;
822
823 case '3':
824 choiceName = "Duration";
825
826 input.skip("\n");
827
828 do{
829 //Duration
830 valid = true;
831
832 System.out.println("\nPlease enter the duration of the movie: ");
833 numString=input.nextLine();
834
835 if(isNumeric(numString)){
836 duration = Integer.parseInt(numString);
837
838 }else{
839 System.out.println("\nERROR : Please enter a number (characters & symbols are not accepted).");
840
841 valid = false;
842
843 }
844
845 }while(!valid);
846
847 break;
848
849 case '4':
850 choiceName = "Age Rating";
851
852 //Age Rating
853 ageRating = ageRatingSelection();
854 if(ageRating.equals("EXIT")){
855 valid = false;
856 break;
857
858 }else{
859 movies[total].setAgeRating(ageRating);
860
861 }
862
863 break;
864
865 case '5':
866 choiceName = "Description";
867 input.skip("\n");
868 System.out.println("\nPlease enter the description of the movie: ");
869 description = input.nextLine();
870
871 if(!haveString(description)){
872 description = "N/A";
873
874 }
875
876 break;
877
878 case '0':
879 clearScreen();
880 return updateChoice;
881
882 default:
883 System.out.println("'" + updateChoice + "' is an invalid choice.");
884 valid = false;
885
886 System.out.print("\nPress enter to continue...");
887 input.skip("\n");
888 input.skip("\n");
889
890 clearScreen();
891
892 }
893
894 }while(!valid);
895
896 clearScreen();
897 System.out.println("*--------------------<Updating " + choiceName + ">--------------------*\n");
898
899 if(updateChoice == '1'){
900 System.out.println(movies[index].getName() + "\t> Change To >\t" + name);
901
902 }else if(updateChoice == '2'){
903 System.out.println(movies[index].getGenre() + "\t> Change To >\t" + genre);
904
905 }else if(updateChoice == '3'){
906 System.out.println(movies[index].getDuration() + "\t> Change To >\t" + duration);
907
908 }else if(updateChoice == '4'){
909 System.out.println(movies[index].getAgeRating() + "\t> Change To >\t" + ageRating);
910
911 }else if(updateChoice == '5'){
912 System.out.println(movies[index].getDescription());
913 System.out.println("\nV Change To V\n");
914 System.out.println(description);
915 }
916
917 System.out.println("\n*--------------------<****************>--------------------*\n");
918
919 System.out.println("\nAre you sure you want to update this?(Y/N)");
920 choice=input.next().charAt(0);
921
922 if(choice == 'Y' || choice == 'y'){
923 if(updateChoice == '1'){
924 movies[index].setName(name);
925
926 }else if(updateChoice == '2'){
927 movies[index].setGenre(genre);
928
929 }else if(updateChoice == '3'){
930 movies[index].setDuration(duration);
931
932 }else if(updateChoice == '4'){
933 movies[index].setAgeRating(ageRating);
934
935 }else if(updateChoice == '5'){
936 movies[index].setDescription(description);
937
938 }
939
940 System.out.println("\nMovie succesfully updated.");
941
942 System.out.println("\nDo you want to update any other attributes of the same movie?(Y/N)");
943 choice = input.next().charAt(0);
944
945 if(choice == 'Y' || choice == 'y'){
946 updateMore = true;
947
948 System.out.print("\nPress enter to continue...");
949 input.skip("\n");
950 input.skip("\n");
951
952 clearScreen();
953
954 }else{
955 clearScreen();
956 return updateChoice;
957
958 }
959
960 }else{
961 clearScreen();
962 Update();
963
964 }
965
966 }while(updateMore);
967
968 return updateChoice;
969
970 }
971
972 //(END)Main Menu Option 3 : Update -----------------------------------------------------/
973
974 //Main Menu Option 4 : Delete ----------------------------------------------------------\
975 public static void Delete(){
976 boolean valid;
977
978 char choice;
979 int index;
980
981 //Temporarily store user input to check if its numeric
982 String numString;
983
984 System.out.println("*-----------------------<Delete>----------------------*");
985 System.out.println(" 1. Delete by ID 2. Delete by Name");
986 System.out.println(" 3. Delete All");
987 System.out.println(" 0. Go Back");
988 System.out.println("------------------------<******>----------------------*\n");
989
990 System.out.print("Please enter your choice : ");
991 choice= input.next().charAt(0);
992
993 clearScreen();
994
995 switch(choice){
996 case '1':
997 //Delete by ID
998 int id = -1;
999
1000 input.skip("\n");
1001
1002 do{
1003 valid = true;
1004
1005 System.out.print("Please enter the ID of the movie : ");
1006 numString = input.nextLine();
1007
1008 if(isNumeric(numString)){
1009 id = Integer.parseInt(numString);
1010
1011 }else{
1012 System.out.println("\nERROR : Please enter a number (characters & symbols are not accepted).\n");
1013
1014 valid = false;
1015
1016 }
1017 }while(!valid);
1018
1019 clearScreen();
1020
1021 index = searchId(id);
1022
1023 if(index == -1){
1024 //If a movie with that ID cannot be found
1025 System.out.println("\nThere are no movies that have the entered ID.");
1026
1027 //Pause screen before clearing
1028 System.out.print("\nPress enter to continue...");
1029 input.skip("\n");
1030
1031 clearScreen();
1032
1033 Delete();
1034
1035 }else{
1036 deleteMovie(index);
1037
1038 }
1039
1040 break;
1041
1042 case '2':
1043 //Delete by Name
1044 String name;
1045 boolean found = false;
1046
1047 input.skip("\n");
1048
1049 do{
1050 valid = true;
1051
1052 System.out.println("\nPlease enter the name of the movie you want to delete: ");
1053 name = input.nextLine();
1054
1055 if(!haveString(name)){
1056 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
1057
1058 valid = false;
1059
1060 }
1061
1062 }while(!valid);
1063
1064 //find the number of movies with the given name
1065 int sumOfSameName = findTotalByName(name, total-1, 0);
1066
1067 if(sumOfSameName == 0){
1068 //If there are no movies with the given name
1069 System.out.println("\nThere are no movies named " + name + ".\n");
1070
1071 //Pause screen before clearing
1072 System.out.print("\nPress enter to continue...");
1073 input.skip("\n");
1074
1075 clearScreen();
1076 Delete();
1077
1078 }else if(sumOfSameName > 1){
1079 //If there are more than 1 movie with the same name
1080 clearScreen();
1081
1082 int idNum = -1;
1083
1084 do{
1085 //Table Name
1086 System.out.printf("%49s %n","Delete by Name");
1087
1088 tableTop();
1089
1090 //Search and display movies with the chosen Name
1091 givenName(name, total-1);
1092
1093 tableBottom();
1094
1095 do{
1096 valid = true;
1097
1098 System.out.print("Please enter the ID of the movie you want to delete : ");
1099 numString=input.nextLine();
1100
1101 if(isNumeric(numString)){
1102 idNum = Integer.parseInt(numString);
1103
1104 }else{
1105 System.out.println("\nERROR : Please enter a number (characters & symbols are not accepted).\n");
1106
1107 valid = false;
1108
1109 }
1110
1111 }while(!valid);
1112
1113 //find the index of the movie that has the entered ID
1114 index = searchId(idNum);
1115
1116 if(index != -1 && name.equals(movies[index].getName())){
1117 //if there is a movie with the same ID and name
1118 clearScreen();
1119
1120 found = true;
1121
1122 deleteMovie(index);
1123
1124 }else{
1125 //if there is no movie with the same ID and name
1126
1127 found = false;
1128
1129 System.out.print("'" + idNum +"' is an invalid choice.\n");
1130
1131 //Pause screen before clearing
1132 System.out.print("\nPress enter to continue...");
1133 input.skip("\n");
1134
1135 clearScreen();
1136
1137 }
1138
1139 }while(!found);
1140
1141 }else{
1142 //There is only one movie with the same name
1143 index = searchName(name);
1144
1145 deleteMovie(index);
1146
1147 }
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 index) {
1219 for (int j=index;j<total;j++){
1220 if(j<total-1){
1221 movies[j]=movies[j+1];
1222 }else{
1223 movies[j]=null;
1224 }
1225 }
1226 total--;
1227 }
1228
1229 //(END) Main Menu Option 4 : Delete ----------------------------------------------------/
1230
1231 //Main Menu Option 5 : View ------------------------------------------------------------\
1232 public static void View(){
1233 char choice;
1234
1235 System.out.println("*---------------------------------<View>--------------------------------*");
1236 System.out.println(" 1. View all(Table) 2. List all, given Genre(Table)");
1237 System.out.println(" 3. List all, given Age Rating(Table) 4. Search by Name(Detailed)");
1238 System.out.println(" 0. Go Back");
1239 System.out.println("*---------------------------------<****>--------------------------------*\n");
1240
1241 System.out.print("Please enter your choice : ");
1242 choice = input.next().charAt(0);
1243
1244 clearScreen();
1245
1246 switch(choice){
1247 //View All
1248 case '1':
1249 //Border
1250 for(int i = 0; i<91; i++){
1251 System.out.print("=");
1252 }
1253
1254 System.out.println("");
1255
1256 //Table Name
1257 System.out.printf("%49s %n","View All");
1258
1259 viewAll();
1260
1261 //Pause screen before clearing
1262 System.out.print("\nPress enter to continue...");
1263 input.skip("\n");
1264 input.skip("\n");
1265
1266 break;
1267
1268 //List all by Genre
1269 case '2':
1270 String genre;
1271
1272 do{
1273 //Genre
1274 genre = genreSelection();
1275 if(genre.equals("EXIT")){
1276 View();
1277 break;
1278
1279 }
1280
1281 clearScreen();
1282
1283 if(searchGenre(genre)){
1284 //If there are movies with the chosen genre
1285 //Border
1286 for(int i = 0; i<91; i++){
1287 System.out.print("=");
1288 }
1289
1290 //Table Name
1291 System.out.printf("%49s %n",genre + " Movie(s)");
1292
1293 tableTop();
1294
1295 //Search and display movies with the chosen genre
1296 givenGenre(genre, total-1);
1297
1298 tableBottom();
1299
1300 //Pause screen before clearing
1301 System.out.print("\nPress enter to continue...");
1302 input.skip("\n");
1303 input.skip("\n");
1304
1305 clearScreen();
1306
1307 }else{
1308 //If there are no movies with the chosen genre
1309 System.out.println("\nThere are no " + genre + " movies.");
1310
1311 //Pause screen before clearing
1312 System.out.print("\nPress enter to continue...");
1313 input.skip("\n");
1314 input.skip("\n");
1315
1316 clearScreen();
1317 View();
1318 }
1319
1320 }while(!searchGenre(genre));
1321
1322 break;
1323
1324 //List all by Age Rating
1325 case '3':
1326 String ageRating;
1327 do{
1328 //Age Rating
1329 ageRating = ageRatingSelection();
1330 if(ageRating.equals("EXIT")){
1331 View();
1332 return;
1333
1334 }
1335 movies[total].setAgeRating(ageRating);
1336
1337 clearScreen();
1338
1339 if(searchAgeRating(ageRating)){
1340 //If there are movies with the chosen age rating
1341 //Table Name
1342 System.out.printf("%49s %n",ageRating + " Movie(s)");
1343
1344 tableTop();
1345
1346 //Search and display movies with the chosen Age Rating
1347 givenAgeRating(ageRating, total-1);
1348
1349 tableBottom();
1350
1351 }else{
1352 //If there are no movies with the chosen age rating
1353 System.out.println("\nThere are no " + ageRating + " movies.");
1354
1355 //Pause screen before clearing
1356 System.out.print("\nPress enter to continue...");
1357 input.skip("\n");
1358 input.skip("\n");
1359
1360 clearScreen();
1361 View();
1362
1363 }
1364
1365 }while(!searchAgeRating(ageRating));
1366
1367 //Pause screen before clearing
1368 System.out.print("\nPress enter to continue...");
1369 input.skip("\n");
1370 input.skip("\n");
1371
1372 break;
1373
1374 //Search by Name
1375 case '4':
1376 boolean valid;
1377 String name;
1378
1379 input.skip("\n");
1380
1381 do{
1382 valid = true;
1383
1384 System.out.println("\nPlease enter the name of the movie you want to view in detail: ");
1385 name = input.nextLine();
1386
1387 if(!haveString(name)){
1388 System.out.println("\nERROR : Empty spaces are not accepted as the name of a movie.");
1389
1390 valid = false;
1391
1392 }
1393
1394 }while(!valid);
1395
1396 //find the number of movies with the given name
1397 int sumOfSameName = findTotalByName(name, total-1, 0);
1398
1399 if(sumOfSameName == 0){
1400 //If there are no movies with the given name
1401 System.out.println("\nThere are no movies named " + name + ".\n");
1402
1403 //Pause screen before clearing
1404 System.out.print("\nPress enter to continue...");
1405 input.skip("\n");
1406
1407 clearScreen();
1408 View();
1409
1410 }else if(sumOfSameName > 1){
1411 //If there are more than 1 movie with the same name
1412
1413 clearScreen();
1414
1415 int idNum;
1416 int index;
1417
1418 do{
1419 //Table Name
1420 System.out.printf("%49s %n","Search by Name");
1421
1422 tableTop();
1423
1424 //Search and display movies with the chosen Name
1425 givenName(name, total-1);
1426
1427 tableBottom();
1428
1429 System.out.print("Please enter the ID of the movie you want to view in detail : ");
1430 idNum = input.nextInt();
1431
1432 //find the index of the movie that has the entered ID
1433 index = searchId(idNum);
1434
1435 if(index != -1 && name.equals(movies[index].getName())){
1436 //if there is a movie with the same ID and name
1437 movies[index].displayDetails();
1438
1439 //Pause screen before clearing
1440 System.out.print("\nPress enter to continue...");
1441 input.skip("\n");
1442 input.skip("\n");
1443
1444 clearScreen();
1445
1446 }else{
1447 //if there is no movie with the same ID and name
1448 System.out.print("'" + idNum +"' is an invalid choice.\n");
1449
1450 //Pause screen before clearing
1451 System.out.print("\nPress enter to continue...");
1452 input.skip("\n");
1453 input.skip("\n");
1454
1455 clearScreen();
1456
1457 }
1458
1459 }while(!(index != -1 && name.equals(movies[index].getName())));
1460
1461 }else{
1462 //There is only one movie with the same name
1463 movies[searchName(name)].displayDetails();
1464 //Pause screen before clearing
1465 System.out.print("\nPress enter to continue...");
1466 input.skip("\n");
1467 }
1468
1469 break;
1470
1471 //Go Back to Main Menu
1472 case '0':
1473 break;
1474
1475 default:
1476 System.out.println("'" + choice + "' is an invalid choice.\n");
1477 View();
1478 }
1479
1480 clearScreen();
1481
1482 return;
1483
1484 }
1485
1486 //View Menu Option 1 : View All
1487 public static void viewAll(){
1488 tableTop();
1489
1490 //Display each row of recorded movies
1491 for(int i = 0; i<total; i++){
1492 movies[i].displayRow();
1493 }
1494
1495 tableBottom();
1496 }
1497
1498 //(END) Main Menu Option 5 : View ------------------------------------------------------/
1499
1500 //Main Menu Option 6 : Create Text File ------------------------------------------------\
1501 static void CreateTextFileMenu(){
1502 char choice;
1503
1504 System.out.println("*------------------<Create Text File>------------------*");
1505 System.out.println(" 1. Table Format 2. Detailed Format");
1506 System.out.println(" 0. Go Back");
1507 System.out.println("*------------------<****************>------------------*\n");
1508
1509 System.out.print("Please enter your choice : ");
1510
1511 choice = input.next().charAt(0);
1512
1513 switch(choice){
1514 case '1':
1515 CreateTextFileFunction(1);
1516
1517 break;
1518
1519 case '2':
1520 CreateTextFileFunction(2);
1521
1522 break;
1523
1524 case '0':
1525 clearScreen();
1526
1527 return;
1528
1529 default:
1530 System.out.print("\n'" + choice + "' is an invalid choice.\n");
1531
1532 System.out.print("\nPress enter to continue...");
1533 input.skip("\n");
1534 input.skip("\n");
1535
1536 clearScreen();
1537
1538 CreateTextFileMenu();
1539 break;
1540 }
1541 }
1542
1543 static void CreateTextFileFunction(int textFileFormat){
1544 boolean valid;
1545 String fileName;
1546
1547 input.skip("\n");
1548
1549 do{
1550 valid = true;
1551
1552 System.out.print("\nPlease enter what you want to name your file : ");
1553 fileName = input.nextLine();
1554
1555 if(!haveString(fileName)){
1556 System.out.println("\nERROR : Empty spaces are not accepted as file names.");
1557
1558 valid = false;
1559
1560 }
1561
1562 }while(!valid);
1563
1564 fileName += ".txt";
1565
1566 try {
1567 File myFile = new File(fileName);
1568 if(myFile.createNewFile()){
1569 System.out.println("File succesfully created : " + myFile.getName());
1570
1571 FileWriter fileWriter = new FileWriter(fileName);
1572 PrintWriter printWriter = new PrintWriter(fileWriter);
1573
1574 if(textFileFormat == 1){
1575 //Text File : Table Format
1576
1577 //Border
1578 for(int i = 0; i<91; i++){
1579 printWriter.print("=");
1580 }
1581
1582 printWriter.println("\nMovies Management System : Movie Records in Table Format");
1583
1584 //Table Border
1585 for(int i = 0; i<91; i++){
1586 printWriter.print("=");
1587 }
1588
1589 printWriter.println("");
1590
1591 //Table Header
1592 printWriter.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
1593
1594 //Table Divider
1595 printWriter.print("|");
1596 for(int i = 0; i<89; i++){
1597 printWriter.print("-");
1598 }
1599 printWriter.println("|");
1600
1601 //Display each row of recorded movies
1602 for(int i = 0; i<total; i++){
1603 movies[i].writeTxtFileRow(printWriter);
1604 }
1605
1606 //Table Bottom Border
1607 for(int i = 0; i<91; i++){
1608 printWriter.print("=");
1609 }
1610
1611 }else{
1612 //Text File : Detailed Format
1613
1614 //Border
1615 for(int i = 0; i<91; i++){
1616 printWriter.print("=");
1617 }
1618
1619 printWriter.println("\nMovies Management System : Movie Records in Detailed Format");
1620
1621 //Border
1622 for(int i = 0; i<91; i++){
1623 printWriter.print("=");
1624 }
1625
1626 for(int i = 0; i<total; i++){
1627 movies[i].writeTxtFileDetails(printWriter);
1628 //Border
1629 for(int j = 0; j<91; j++){
1630 printWriter.print("=");
1631 }
1632 }
1633 }
1634
1635 printWriter.close();
1636
1637 System.out.print("\nPress enter to continue...");
1638 input.skip("\n");
1639
1640 }else{
1641 System.out.println("File already exists.");
1642
1643 System.out.print("\nPress enter to continue...");
1644 input.skip("\n");
1645
1646 clearScreen();
1647 CreateTextFileMenu();
1648
1649 }
1650
1651 }catch(Exception e){
1652 System.out.println("ERROR : " + e);
1653
1654 }
1655 }
1656
1657 //(END) Main Menu Option 6 : Create Text File ------------------------------------------/
1658
1659 //Main Menu Option 6 : Import Text File Movies -----------------------------------------\
1660 static void ReadTextFile(){
1661 boolean valid;
1662
1663 String fileName;
1664
1665 //used to count and skip the first 3 lines
1666 int countSkip = 0;
1667
1668 //used to count the lines after the first 3 lines
1669 int countLines = 0;
1670
1671 //used to count how many movie records are in the .txt file
1672 int totalTxtFileMovies = 0;
1673
1674 //class attributes
1675 String name = "";
1676
1677 String genre = "";
1678
1679 String durationString = ""; //temporarily store the substring of the duration
1680 int duration = 0;
1681
1682 String ageRating = "";
1683
1684 String description = "";
1685
1686 input.skip("\n");
1687
1688 do{
1689 valid = true;
1690
1691 System.out.println("NOTE : Can only import movies from the DETAILED format text files that were created by this program.");
1692 System.out.println("\nPlase enter the name of the text file (without the .txt extension) : ");
1693 fileName = input.nextLine();
1694
1695 if(!haveString(fileName)){
1696 System.out.println("\nERROR : Empty spaces are not accepted as file names.");
1697
1698 valid = false;
1699
1700 }
1701
1702 }while(!valid);
1703
1704 clearScreen();
1705
1706 try{
1707 File myFile = new File(fileName + ".txt");
1708 Scanner myReader = new Scanner( myFile);
1709
1710 String fileLine;
1711
1712 while(myReader.hasNextLine()){
1713 fileLine = myReader.nextLine();
1714 countSkip++;
1715
1716 if(countSkip == 2 && !fileLine.equals("Movies Management System : Movie Records in Detailed Format")){
1717 throw new WrongFormatException("Only the detailed format text file that was created from this program can be used.");
1718
1719 }
1720
1721 //skip first 3 lines
1722 if(countSkip>3){
1723 countLines++;
1724 if(countLines%15 == 4){
1725 //Get the Name
1726 name = fileLine.substring(14);
1727
1728 }else if(countLines%15 == 6){
1729 //Get the Genre
1730 genre = fileLine.substring(10);
1731
1732 }else if(countLines%15 == 8){
1733 //Get the Duration
1734 durationString = fileLine.substring(12);
1735 duration = Integer.parseInt(durationString);
1736
1737 }else if(countLines%15 == 10){
1738 //Get the Age Rating
1739 ageRating = fileLine.substring(14);
1740
1741 }else if(countLines%15 == 13){
1742 //Get the Description
1743 description = fileLine;
1744
1745 }else if(countLines%15 == 0){
1746 //After going through the details of a movie record
1747 //Add the movie record
1748 movies[total] = new MovieClass(movieID, name, genre, duration, ageRating, description);
1749
1750 total++;
1751 movieID++;
1752
1753 totalTxtFileMovies++;
1754
1755 }
1756
1757 }
1758
1759 }
1760
1761 System.out.println("Movies succesfully imported.\n\nTotal movies imported : " + totalTxtFileMovies);
1762
1763 myReader.close();
1764
1765 }catch(Exception e){
1766 System.out.println("ERROR : " + e);
1767
1768 }
1769
1770 System.out.print("\nPress enter to continue...");
1771 input.skip("\n");
1772
1773 }
1774
1775 //(END) Main Menu Option 6 : Import Text File Movies ---------------------------------------/
1776
1777}