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