· 5 years ago · Feb 05, 2020, 07:44 AM
1import java.util.Scanner;
2
3import java.io.File;
4
5import java.io.FileWriter;
6
7import java.io.IOException;
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 System.out.println("Movie Id : " + id);
82 System.out.println("\nMovie Name : " + name);
83 System.out.println("\nGenre : " + genre);
84 System.out.println("\nDuration : " + duration);
85 System.out.println("\nAge Rating : " + ageRating);
86 System.out.println("\nDescription : \n\n" + description);
87 }
88
89 // public void displayTxtFileRow(){
90 // printWriter.printf("%-1s %-3s %-50s %-23s %-8s %-1s %n", "|", id, name, genre, ageRating, "|");
91 // }
92
93 // public void displayTxtFileDetails(PrintWriter printWriter){
94 // printWriter.println("Movie Id : " + id);
95 // printWriter.println("\nMovie Name : " + name);
96 // printWriter.println("\nGenre : " + genre);
97 // printWriter.println("\nDuration : " + duration);
98 // printWriter.println("\nAge Rating : " + ageRating);
99 // printWriter.println("\nDescription : \n\n" + description);
100 // }
101
102//////////////////////////////////////////////////////////////////////////////////////////////
103//////////////////////////////////////////////////////////////////////////////////////////////
104 //In Functions File
105
106 //Variables ----------------------------------------------------------------------------\
107 public final static int size = 10; //Size of array/maximum number of movies that can be recorded
108
109 public static int total = 0; //total = total number of movies
110
111 public static int movieID = 1; //unique number for each movie
112
113 public static Movie movies[] = new Movie[size]; //array of Movie class object
114
115 public static boolean isInitialised = false;
116
117 public static Scanner input = new Scanner(System.in);
118
119 //(END) Variables ----------------------------------------------------------------------/
120
121 //Manipulate Objects Functions ---------------------------------------------------------\
122 //Initialise array of objects
123 public static void initialiseArray(){
124 for(int i = 0; i<size; i++){
125 movies[i] = new Movie();
126 }
127 }
128
129 //Creates 5 default objects
130 public static void fillObjects(){
131 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.");
132 movies[total+1] = new Movie(movieID+1, "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.");
133 movies[total+2] = new Movie(movieID+2, "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.");
134 movies[total+3] = new Movie(movieID+3, "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.");
135 movies[total+4] = new Movie(movieID+4, "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.");
136 total += 5;
137 movieID += 5;
138 }
139
140 //(END) Manipulate Objects Functions ---------------------------------------------------/
141
142 //User Interface Functions -------------------------------------------------------------\
143 //Clear Command Line Console
144 public static void clearScreen(){
145 // System.out.print("\033[H\033[2J");
146 // System.out.flush();
147 // }
148
149 public static void tableTop(){
150 //Table Border
151 for(int i = 0; i<91; i++){
152 System.out.print("=");
153 }
154
155 System.out.println("");
156
157 //Table Header
158 System.out.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
159
160 //Table Divider
161 System.out.print("|");
162 for(int i = 0; i<89; i++){
163 System.out.print("-");
164 }
165 System.out.println("|");
166 }
167
168 public static void tableBottom(){
169 //Table Bottom Border
170 for(int i = 0; i<91; i++){
171 System.out.print("=");
172 }
173
174 System.out.println("");
175 }
176
177 //(END) User Interface Functions -------------------------------------------------------/
178
179 //Search Functions ---------------------------------------------------------------------\
180 //Check if there is any movie with the same Genre
181 static boolean searchGenre(String genre){
182 for(int i = 0; i < size; i++){
183 //found
184 if(genre.equals(movies[i].getGenre())){
185 return true;
186 }
187 }
188 //not found
189 return false;
190 }
191
192 //Check if there is any movie with the same Age Rating
193 static boolean searchAgeRating(String ageRating){
194 for(int i = 0; i < total; i++){
195 //found
196 if(ageRating.equals(movies[i].getAgeRating())){
197 return true;
198 }
199 }
200 //not found
201 return false;
202 }
203
204 //Check if there is any movie with the same Name
205 static int searchName(String name){
206 for(int i = 0; i < total; i++){
207 //found
208 if(name.equals(movies[i].getName())){
209 return i;
210 }
211 }
212 //not found
213 return -1;
214 }
215
216 //Check if there is any movie with the same Id
217 static int searchId(int id){
218 for(int i = 0; i < total; i++){
219 //found
220 if(id == movies[i].getId()){
221 return i;
222 }
223 }
224 //not found
225 return -1;
226 }
227
228 //(END) Search Functions ---------------------------------------------------------------/
229
230 //Recursive Functions ------------------------------------------------------------------\
231 //Display movies that has the same Genre
232 static void givenGenre(String genre, int high){
233 if (high < 0){
234 return;
235 }
236 else if (genre.equals(movies[high].getGenre())){
237 movies[high].displayRow();
238 givenGenre(genre, high-1);
239 }
240 else {
241 givenGenre(genre, high-1);
242 }
243 }
244
245 //Display movies that has the same AgeRating
246 static void givenAgeRating(String ageRating, int high){
247 if (high < 0){
248 return;
249 }
250 else if (ageRating.equals(movies[high].getAgeRating())){
251 movies[high].displayRow();
252 givenAgeRating(ageRating, high-1);
253 }
254 else {
255 givenAgeRating(ageRating, high-1);
256 }
257 }
258
259 //Calculate total movies that has the same Name
260 static int findTotalByName(String name, int high, int sum){
261 if (high < 0){
262 return sum;
263 }
264 else if (name.equals(movies[high].getName())){
265 sum++;
266 return findTotalByName(name, high-1, sum);
267 }
268 else {
269 return findTotalByName(name, high-1, sum);
270 }
271 }
272
273 //Display movies that has the same Name
274 static void givenName(String name, int high){
275 if (high < 0){
276 return;
277 }
278 else if (name.equals(movies[high].getName())){
279 movies[high].displayRow();
280 givenName(name, high-1);
281 }
282 else {
283 givenName(name, high-1);
284 }
285 }
286
287 //(END) Recursive Functions ------------------------------------------------------------/
288
289 //Main Menu Option 2 : Add Record ------------------------------------------------------\
290 public static void AddRecord(){
291 String name;
292 int duration;
293 String description;
294
295 System.out.println("*--------------------<Add New Record>--------------------*");
296
297 System.out.println("\nMovie ID : " + movieID);
298 movies[total].setId(movieID);
299
300 System.out.println("\nPlease enter the name of the movie: ");
301 input.skip("\n");
302 name=input.nextLine();
303 movies[total].setName(name);
304
305 System.out.println("");
306
307 //Genre
308 movies[total].setGenre(genreSelection(1));
309
310 System.out.println("");
311
312 //Age Rating
313 movies[total].setAgeRating(ageRatingSelection(1));
314
315 //Duration
316 System.out.println("\nPlease enter the duration of the movie: ");
317 duration=input.nextInt();
318 movies[total].setDuration(duration);
319
320 //Description
321 System.out.println("\nPlease enter the description of the movie: ");
322 input.skip("\n");
323 description=input.nextLine();
324 movies[total].setDescription(description);
325
326 System.out.println("\n\nMovie succesfully added.");
327
328 total++;
329 movieID++;
330
331 System.out.println("Total Movies : " + total);
332
333 //Pause screen before clearing
334 System.out.print("\nPress enter to continue...");
335 input.skip("\n");
336
337 // clearScreen();
338
339 }
340
341 //(END) Main Menu Option 2 : Add Record ------------------------------------------------/
342
343 //Main Menu Option 5 : View ------------------------------------------------------------\
344 public static void View(){
345 char choice;
346 System.out.println("*--------------------<View>--------------------*");
347 System.out.println(" 1. View all.....................(Table Format)");
348 System.out.println(" 2. List all, given Genre........(Table Format)");
349 System.out.println(" 3. List all, given Age Rating...(Table Format)");
350 System.out.println(" 4. Search by Name............(Detailed Format)");
351 System.out.println(" 0. Go Back......................(To Main Menu)");
352 System.out.println("*--------------------<****>--------------------*\n");
353
354 System.out.print("Please enter your choice : ");
355 choice = input.next().charAt(0);
356
357 // clearScreen();
358
359 switch(choice){
360 //View All
361 case '1':
362 //Table Name
363 System.out.printf("%49s %n","View All");
364
365 viewAll();
366
367 //Pause screen before clearing
368 System.out.print("\nPress enter to continue...");
369 input.skip("\n");
370 input.skip("\n");
371
372 break;
373
374 //List all by Genre
375 case '2':
376 String genre;
377
378 do{
379 genre = genreSelection(4);
380
381 // clearScreen();
382
383 if(searchGenre(genre)){
384 //If there are movies with the chosen genre
385 //Table Name
386 System.out.printf("%49s %n",genre + " Movie(s)");
387
388 tableTop();
389
390 //Search and display movies with the chosen genre
391 givenGenre(genre, total-1);
392
393 tableBottom();
394 }else{
395 //If there are no movies with the chosen genre
396 System.out.println("\nThere are no " + genre + " movies.");
397
398 //Pause screen before clearing
399 System.out.print("\nPress enter to continue...");
400 input.skip("\n");
401 input.skip("\n");
402
403 // clearScreen();
404 View();
405 }
406 }while(!searchGenre(genre));
407
408 //Pause screen before clearing
409 System.out.print("\nPress enter to continue...");
410 input.skip("\n");
411 input.skip("\n");
412
413 break;
414
415 //List all by Age Rating
416 case '3':
417 String ageRating;
418 do{
419 ageRating = ageRatingSelection(4);
420
421 // clearScreen();
422
423 if(searchAgeRating(ageRating)){
424 //If there are movies with the chosen age rating
425 //Table Name
426 System.out.printf("%49s %n",ageRating + " Movie(s)");
427
428 tableTop();
429
430 //Search and display movies with the chosen Age Rating
431 givenAgeRating(ageRating, total-1);
432
433 tableBottom();
434 }else{
435 //If there are no movies with the chosen age rating
436 System.out.println("\nThere are no " + ageRating + " movies.");
437
438 //Pause screen before clearing
439 System.out.print("\nPress enter to continue...");
440 input.skip("\n");
441 input.skip("\n");
442
443 // clearScreen();
444 View();
445 }
446 }while(!searchAgeRating(ageRating));
447
448 //Pause screen before clearing
449 System.out.print("\nPress enter to continue...");
450 input.skip("\n");
451 input.skip("\n");
452
453 break;
454
455 //Search by Name
456 case '4':
457 String name;
458 input.skip("\n");
459 System.out.println("Please enter the name of the movie : ");
460 name = input.nextLine();
461
462 //find the number of movies with the given name
463 int sumOfSameName = findTotalByName(name, total-1, 0);
464
465 if(sumOfSameName == 0){
466 //If there are no movies with the given name
467 System.out.println("\nThere are no movies named " + name + ".\n");
468
469 //Pause screen before clearing
470 System.out.print("\nPress enter to continue...");
471 input.skip("\n");
472
473 // clearScreen();
474 View();
475
476 }else if(sumOfSameName > 1){
477 //If there are more than 1 movie with the same name
478 // clearScreen();
479
480 int idNum;
481 int index;
482
483 do{
484 //Table Name
485 System.out.printf("%49s %n","Search by Name");
486
487 tableTop();
488
489 //Search and display movies with the chosen Name
490 givenName(name, total-1);
491
492 tableBottom();
493
494 System.out.print("Please enter the ID of the movie you want to view in detail : ");
495 idNum = input.nextInt();
496
497 //find the index of the movie that has the entered ID
498 index = searchId(idNum);
499
500 if(index != -1 && name.equals(movies[index].getName())){
501 //if there is a movie with the same ID and name
502 movies[index].displayDetails();
503
504 //Pause screen before clearing
505 System.out.print("\nPress enter to continue...");
506 input.skip("\n");
507 input.skip("\n");
508
509 // clearScreen();
510
511 }else{
512 //if there is no movie with the same ID and name
513 System.out.print("'" + idNum +"' is an invalid choice.\n");
514
515 //Pause screen before clearing
516 System.out.print("\nPress enter to continue...");
517 input.skip("\n");
518 input.skip("\n");
519
520 // clearScreen();
521
522 }
523
524 }while(!(index != -1 && name.equals(movies[index].getName())));
525
526 }else{
527 //There is only one movie with the same name
528 movies[searchName(name)].displayDetails();
529 //Pause screen before clearing
530 System.out.print("\nPress enter to continue...");
531 input.skip("\n");
532 }
533
534 break;
535
536 //Go Back to Main Menu
537 case '0':
538 mainCaller();
539
540 default:
541 System.out.println("'" + choice + "' is an invalid choice.\n");
542 View();
543 }
544
545 // clearScreen();
546
547 mainCaller();
548
549 }
550
551 //View Menu Option 1 : View All
552 public static void viewAll(){
553 tableTop();
554
555 //Display each row of recorded movies
556 for(int i = 0; i<total; i++){
557 movies[i].displayRow();
558 }
559
560 tableBottom();
561 }
562
563 //View Menu Option 2 : List all, given Genre
564 //Selecting genre
565 //selection is the number of the main menu option that was chosen
566 static String genreSelection(int selection){
567 char choice;
568 String genre;
569 do{
570 genre = "";
571 System.out.println("*-------------------<Genres>-------------------*");
572 System.out.println(" 1. Action 2. Adventure");
573 System.out.println(" 3. Comedy 4. Drama");
574 System.out.println(" 5. Horror 6. Sci-Fi");
575 System.out.println(" 7. Romance 8. Others");
576 System.out.println(" 0. Go Back");
577 System.out.println("*-------------------<******>-------------------*\n");
578
579 System.out.print("Please enter your choice : ");
580
581 choice = input.next().charAt(0);
582
583 switch(choice){
584 case '1':
585 genre = "Action";
586 break;
587
588 case '2':
589 genre = "Adventure";
590 break;
591
592 case '3':
593 genre = "Comedy";
594 break;
595
596 case '4':
597 genre = "Drama";
598 break;
599
600 case '5':
601 genre = "Horror";
602 break;
603
604 case '6':
605 genre = "Sci-Fi";
606 break;
607
608 case '7':
609 genre = "Romance";
610 break;
611
612 case '8':
613 genre = "Others";
614 break;
615
616 case '0':
617 // clearScreen();
618
619 if(selection==5){
620 //if genreSelection(...) was called from under the view menu.
621 View();
622
623 }else{
624 //if genreSelection(...) was called from other Main Menu options. E.g : Add New Record, Update, Delete etc
625 mainCaller();
626 }
627
628 break;
629
630 default:
631 System.out.print("\n'" + choice + "' is an invalid choice.\n");
632
633 System.out.print("\nPress enter to continue...");
634 input.skip("\n");
635 input.skip("\n");
636
637 // clearScreen();
638 genre = "INVALID";
639 break;
640 }
641 }while(genre.equals("INVALID"));
642
643 return genre;
644 }
645
646 //View Menu Option 3 : List all, given Age Rating
647 //Selecting age rating
648 //selection is the number of the main menu option that was chosen
649 static String ageRatingSelection(int selection){
650 char choice;
651 String ageRating;
652 do{
653 ageRating = "";
654 System.out.println("*-----------------<Age Rating>-----------------*");
655 System.out.println(" 1. G 2. PG");
656 System.out.println(" 3. PG-13 4. R");
657 System.out.println(" 0. Go Back");
658 System.out.println("*-----------------<**********>-----------------*\n");
659
660 System.out.print("Please enter your choice : ");
661
662 choice = input.next().charAt(0);
663
664 switch(choice){
665 case '1':
666 ageRating = "G";
667 break;
668
669 case '2':
670 ageRating = "PG";
671 break;
672
673 case '3':
674 ageRating = "PG-13";
675 break;
676
677 case '4':
678 ageRating = "R";
679 break;
680
681 case '0':
682 // clearScreen();
683
684 if(selection==5){
685 //if genreSelection(...) was called from under the view menu.
686 View();
687
688 }else{
689 //if genreSelection(...) was called from other Main Menu options. E.g : Add New Record, Update, Delete etc
690 mainCaller();
691 }
692
693 break;
694
695 default:
696 System.out.print("\n'" + choice + "' is an invalid choice.\n");
697
698 System.out.print("\nPress enter to continue...");
699 input.skip("\n");
700 input.skip("\n");
701
702 // clearScreen();
703 ageRating = "INVALID";
704 break;
705 }
706 }while(ageRating.equals("INVALID"));
707
708 return ageRating;
709 }
710
711 //(END) Main Menu Option 5 : View ------------------------------------------------------/
712
713 //Main Menu Option 6 : Create Text File ------------------------------------------------\
714 // static void CreateTextFile(){
715 // char choice;
716 // String fileName;
717
718 // System.out.println("*---------------<Create Text File>---------------*");
719 // System.out.println(" 1. Table Format 2. Detailed Format");
720 // System.out.println(" 0. Go Back");
721 // System.out.println("*---------------<****************>---------------*\n");
722
723 // System.out.print("Please enter your choice : ");
724
725 // choice = input.next().charAt(0);
726
727 // switch(choice){
728 // case '1':
729 // input.skip("\n");
730 // System.out.print("Please enter the what you want to name your file : ");
731 // fileName = input.nextLine() + ".txt";
732
733 // try {
734 // File myFile = new File(fileName);
735 // if (myFile.createNewFile()) {
736 // System.out.println("File succesfully created : " + myFile.getName());
737
738 // FileWriter fileWriter = new FileWriter(fileName);
739 // PrintWriter printWriter = new PrintWriter(fileWriter);
740
741 // //Table Border
742 // for(int i = 0; i<91; i++){
743 // printWriter.print("=");
744 // }
745
746 // printWriter.println("");
747
748 // //Table Header
749 // printWriter.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
750
751 // //Table Divider
752 // printWriter.print("|");
753 // for(int i = 0; i<89; i++){
754 // printWriter.print("-");
755 // }
756 // printWriter.println("|");
757
758 // //Display each row of recorded movies
759 // for(int i = 0; i<total; i++){
760 // movies[i].displayTxtFileRow();
761 // }
762
763 // //Table Bottom Border
764 // for(int i = 0; i<91; i++){
765 // printWriter.print("=");
766 // }
767
768 // printWriter.println("");
769
770 // printWriter.close();
771
772 // System.out.print("\nPress enter to continue...");
773 // input.skip("\n");
774
775 // } else {
776 // System.out.println("File already exists.");
777
778 // }
779
780 // }catch(IOException e){
781 // System.out.println("An error occurred.");
782 // e.printStackTrace();
783
784 // }
785
786 // break;
787
788 // case '2':
789
790 // break;
791
792 // case '0':
793 clearScreen();
794 // mainCaller();
795 // break;
796
797 // default:
798 // System.out.print("\n'" + choice + "' is an invalid choice.\n");
799
800 // System.out.print("\nPress enter to continue...");
801 // input.skip("\n");
802 // input.skip("\n");
803
804 clearScreen();
805
806 // CreateTextFile();
807 // break;
808 // }
809 // }
810
811 //(END) Main Menu Option 6 : Create Text File ------------------------------------------/
812
813//////////////////////////////////////////////////////////////////////////////////////////////
814//////////////////////////////////////////////////////////////////////////////////////////////
815
816 //Call main method
817 static void mainCaller(){
818 main(null);
819 }
820
821 public static void main(String[] args){
822 //Initialise array of objects of Movie class
823 if(!isInitialised){
824 initialiseArray();
825 isInitialised = true;
826 }
827
828 char choice;
829
830 System.out.println("=====================Movie Management System====================");
831 System.out.println("Please key in your selection");
832 System.out.println("1. Add Default Records");
833 System.out.println("2. Add New Record");
834 System.out.println("3. Update Movie Record");
835 System.out.println("4. Delete Movie Record");
836 System.out.println("5. View Movie Records");
837 System.out.println("6. Create Text File");
838 System.out.println("0. Exit" );
839
840 System.out.print("Please enter your choice : ");
841 choice = input.next().charAt(0);
842
843
844 switch(choice){
845 case'1':
846 fillObjects();
847 System.out.println("\nDefault objects succesfully added.");
848
849 System.out.print("\nPress enter to continue...");
850 input.skip("\n");
851 input.skip("\n");
852
853 // clearScreen();
854 break;
855
856 case'2':
857 // clearScreen();
858 AddRecord();
859 break;
860
861 case'3':
862 //update
863 // clearScreen();
864 break;
865
866 case'4':
867 delete();
868 // clearScreen();
869 break;
870
871 case'5':
872 // clearScreen();
873 View();
874 // clearScreen();
875 break;
876
877 case'6':
878 //CreateTextFile();
879 // clearScreen();
880 break;
881
882 case'0':
883 System.exit(0);
884 break;
885 default:
886 System.out.print("\n'" + choice + "' is an invalid choice.\n");
887
888 System.out.print("\nPress enter to continue...");
889 input.skip("\n");
890 input.skip("\n");
891
892 break;
893 }
894 mainCaller();
895
896 }
897}