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