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