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