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