· 5 years ago · Feb 05, 2020, 12:58 PM
1import java.util.Scanner;
2
3import java.io.File;
4
5import java.io.FileWriter;
6
7import java.io.IOException;
8
9import java.io.PrintWriter;
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 //Creates 5 default objects
145 public static void fillObjects(){
146 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.");
147 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.");
148 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.");
149 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.");
150 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.");
151 total += 5;
152 movieID += 5;
153 }
154
155 //(END) Manipulate Objects Functions ---------------------------------------------------/
156
157 //User Interface Functions -------------------------------------------------------------\
158 //Clear Command Line Console
159 public static void clearScreen(){
160 System.out.print("\033[H\033[2J");
161 System.out.flush();
162 }
163
164 public static void tableTop(){
165 //Table Border
166 for(int i = 0; i<91; i++){
167 System.out.print("=");
168 }
169
170 System.out.println("");
171
172 //Table Header
173 System.out.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
174
175 //Table Divider
176 System.out.print("|");
177 for(int i = 0; i<89; i++){
178 System.out.print("-");
179 }
180 System.out.println("|");
181 }
182
183 public static void tableBottom(){
184 //Table Bottom Border
185 for(int i = 0; i<91; i++){
186 System.out.print("=");
187 }
188
189 System.out.println("");
190 }
191
192 //(END) User Interface Functions -------------------------------------------------------/
193
194 //Search Functions ---------------------------------------------------------------------\
195 //Check if there is any movie with the same Genre
196 static boolean searchGenre(String genre){
197 for(int i = 0; i < size; i++){
198 //found
199 if(genre.equals(movies[i].getGenre())){
200 return true;
201 }
202 }
203 //not found
204 return false;
205 }
206
207 //Check if there is any movie with the same Age Rating
208 static boolean searchAgeRating(String ageRating){
209 for(int i = 0; i < total; i++){
210 //found
211 if(ageRating.equals(movies[i].getAgeRating())){
212 return true;
213 }
214 }
215 //not found
216 return false;
217 }
218
219 //Check if there is any movie with the same Name
220 static int searchName(String name){
221 for(int i = 0; i < total; i++){
222 //found
223 if(name.equals(movies[i].getName())){
224 return i;
225 }
226 }
227 //not found
228 return -1;
229 }
230
231 //Check if there is any movie with the same Id
232 static int searchId(int id){
233 for(int i = 0; i < total; i++){
234 //found
235 if(id == movies[i].getId()){
236 return i;
237 }
238 }
239 //not found
240 return -1;
241 }
242
243 //(END) Search Functions ---------------------------------------------------------------/
244
245 //Recursive Functions ------------------------------------------------------------------\
246 //Display movies that has the same Genre
247 static void givenGenre(String genre, int high){
248 if (high < 0){
249 return;
250 }
251 else if (genre.equals(movies[high].getGenre())){
252 movies[high].displayRow();
253 givenGenre(genre, high-1);
254 }
255 else {
256 givenGenre(genre, high-1);
257 }
258 }
259
260 //Display movies that has the same AgeRating
261 static void givenAgeRating(String ageRating, int high){
262 if (high < 0){
263 return;
264 }
265 else if (ageRating.equals(movies[high].getAgeRating())){
266 movies[high].displayRow();
267 givenAgeRating(ageRating, high-1);
268 }
269 else {
270 givenAgeRating(ageRating, high-1);
271 }
272 }
273
274 //Calculate total movies that has the same Name
275 static int findTotalByName(String name, int high, int sum){
276 if (high < 0){
277 return sum;
278 }
279 else if (name.equals(movies[high].getName())){
280 sum++;
281 return findTotalByName(name, high-1, sum);
282 }
283 else {
284 return findTotalByName(name, high-1, sum);
285 }
286 }
287
288 //Display movies that has the same Name
289 static void givenName(String name, int high){
290 if (high < 0){
291 return;
292 }
293 else if (name.equals(movies[high].getName())){
294 movies[high].displayRow();
295 givenName(name, high-1);
296 }
297 else {
298 givenName(name, high-1);
299 }
300 }
301
302 //(END) Recursive Functions ------------------------------------------------------------/
303
304 //Main Menu Option 2 : Add Record ------------------------------------------------------\
305 public static void AddRecord(){
306 String name;
307 int duration;
308 String description;
309
310 System.out.println("*--------------------<Add New Record>--------------------*");
311
312 System.out.println("\nMovie ID : " + movieID);
313 movies[total].setId(movieID);
314
315 System.out.println("\nPlease enter the name of the movie: ");
316 input.skip("\n");
317 name=input.nextLine();
318 movies[total].setName(name);
319
320 System.out.println("");
321
322 //Genre
323 movies[total].setGenre(genreSelection(1));
324
325 System.out.println("");
326
327 //Age Rating
328 movies[total].setAgeRating(ageRatingSelection(1));
329
330 //Duration
331 System.out.println("\nPlease enter the duration of the movie: ");
332 duration=input.nextInt();
333 movies[total].setDuration(duration);
334
335 //Description
336 System.out.println("\nPlease enter the description of the movie: ");
337 input.skip("\n");
338 description=input.nextLine();
339 movies[total].setDescription(description);
340
341 total++;
342 movieID++;
343
344 System.out.println("\n*--------------------<**************>--------------------*");
345
346 System.out.println("\nMovie succesfully added.");
347
348 System.out.println("\nTotal Movies : " + total);
349
350 //Pause screen before clearing
351 System.out.print("\nPress enter to continue...");
352 input.skip("\n");
353
354 clearScreen();
355
356 }
357
358 //(END) Main Menu Option 2 : Add Record ------------------------------------------------/
359
360 //Main Menu Option 4 : Delete ----------------------------------------------------------\
361 public static void Delete(){
362 char choice;
363 int index;
364
365 System.out.println("*-----------------------<Delete>----------------------*");
366 System.out.println(" 1. Delete by ID 2. Delete by Name");
367 System.out.println(" 0. Go Back");
368 System.out.println("------------------------<******>----------------------*\n");
369
370 System.out.print("Please enter your choice : ");
371 choice= input.next().charAt(0);
372
373 clearScreen();
374
375 switch(choice){
376 //Delete by ID
377 case '1':
378 int id;
379 System.out.print("Please enter the ID of the movie : ");
380 id = input.nextInt();
381
382 clearScreen();
383
384 index = searchId(id);
385
386 if(index == -1){
387 //If a movie with that ID cannot be found
388 System.out.println("\nThere are no movies that have the entered ID.");
389
390 //Pause screen before clearing
391 System.out.print("\nPress enter to continue...");
392 input.skip("\n");
393 input.skip("\n");
394
395 Delete();
396
397 }else{
398 //If a movie with the same ID is found
399 movies[index].displayDetails();
400
401 System.out.println("\nAre you sure you want to delete this movie?(Y/N)");
402 choice=input.next().charAt(0);
403
404 if(choice=='Y' || choice == 'y'){
405 deleteById(id);
406
407 }else{
408 Delete();
409
410 }
411 }
412
413 break;
414
415 //Delete by Name
416 case '2':
417 String name;
418 boolean found = false;
419
420 input.skip("\n");
421 System.out.println("Please enter the name of the movie : ");
422 name = input.nextLine();
423
424 //find the number of movies with the given name
425 int sumOfSameName = findTotalByName(name, total-1, 0);
426
427 if(sumOfSameName == 0){
428 //If there are no movies with the given name
429 System.out.println("\nThere are no movies named " + name + ".\n");
430
431 //Pause screen before clearing
432 System.out.print("\nPress enter to continue...");
433 input.skip("\n");
434
435 clearScreen();
436 View();
437
438 }else if(sumOfSameName > 1){
439 //If there are more than 1 movie with the same name
440 clearScreen();
441
442 int idNum;
443
444 do{
445 //Table Name
446 System.out.printf("%49s %n","Delete by Name");
447
448 tableTop();
449
450 //Search and display movies with the chosen Name
451 givenName(name, total-1);
452
453 tableBottom();
454
455 System.out.print("Please enter the ID of the movie you want to delete : ");
456 idNum = input.nextInt();
457
458 //find the index of the movie that has the entered ID
459 index = searchId(idNum);
460
461 if(index != -1 && name.equals(movies[index].getName())){
462 //if there is a movie with the same ID and name
463 clearScreen();
464
465 found = true;
466
467 movies[index].displayDetails();
468
469 System.out.println("\nAre you sure you want to delete this movie?(Y/N)");
470 choice=input.next().charAt(0);
471
472 if(choice == 'Y' || choice == 'y'){
473 deleteByName(name);
474
475 }else{
476 Delete();
477
478 }
479
480 }else{
481 //if there is no movie with the same ID and name
482
483 found = false;
484
485 System.out.print("'" + idNum +"' is an invalid choice.\n");
486
487 //Pause screen before clearing
488 System.out.print("\nPress enter to continue...");
489 input.skip("\n");
490 input.skip("\n");
491
492 clearScreen();
493
494 }
495
496 }while(!found);
497
498 }else{
499 //There is only one movie with the same name
500 movies[searchName(name)].displayDetails();
501
502 System.out.println("\nAre you sure you want to delete this movie?(Y/N)");
503 choice=input.next().charAt(0);
504
505 if(choice == 'Y' || choice == 'y'){
506 deleteByName(name);
507
508 }
509 else{
510 Delete();
511
512 }
513
514 }
515
516 //Pause screen before clearing
517 System.out.print("\nPress enter to continue...");
518 input.skip("\n");
519 input.skip("\n");
520
521 System.out.println("Movie succesfully deleted.");
522
523 clearScreen();
524
525 mainCaller();
526
527 break;
528
529 case '0':
530 mainCaller();
531 break;
532
533 default:
534 System.out.println("'" + choice + "' is an invalid choice.\n");
535 Delete();
536
537 }
538 }
539
540 public static void deleteById (int id) {
541 for(int i=0;i<total;i++){
542 if(movies[i].getId()==id){
543 for (int j=i;j<total;j++){
544 if(j<total-1){
545 movies[j]=movies[j+1];
546 }else{
547 movies[j]=null;
548 }
549 }
550 total--;
551 break;
552 }
553 }
554 }
555
556 public static void deleteByName(String name){
557 for(int i=0;i<total;i++){
558 if(movies[i].getName().equals(name)){
559 for(int j=i;j<total;j++){
560 if(j<total-1){
561 movies[j]=movies[j+1];
562 }else{
563 movies[j]=null;
564 }
565 }
566 total--;
567 break;
568 }
569 }
570 }
571
572 //(END) Main Menu Option 4 : Delete ----------------------------------------------------/
573
574 //Main Menu Option 5 : View ------------------------------------------------------------\
575 public static void View(){
576 char choice;
577
578 System.out.println("*---------------------------------<View>--------------------------------*");
579 System.out.println(" 1. View all(Table) 2. List all, given Genre(Table)");
580 System.out.println(" 3. List all, given Age Rating(Table) 4. Search by Name(Detailed)");
581 System.out.println(" 0. Go Back");
582 System.out.println("*---------------------------------<****>--------------------------------*\n");
583
584 System.out.print("Please enter your choice : ");
585 choice = input.next().charAt(0);
586
587 clearScreen();
588
589 switch(choice){
590 //View All
591 case '1':
592 //Border
593 for(int i = 0; i<91; i++){
594 System.out.print("=");
595 }
596
597 //Table Name
598 System.out.printf("%49s %n","View All");
599
600 viewAll();
601
602 //Pause screen before clearing
603 System.out.print("\nPress enter to continue...");
604 input.skip("\n");
605 input.skip("\n");
606
607 break;
608
609 //List all by Genre
610 case '2':
611 String genre;
612
613 do{
614 genre = genreSelection(4);
615
616 clearScreen();
617
618 if(searchGenre(genre)){
619 //If there are movies with the chosen genre
620 //Border
621 for(int i = 0; i<91; i++){
622 System.out.print("=");
623 }
624
625 //Table Name
626 System.out.printf("%49s %n",genre + " Movie(s)");
627
628 tableTop();
629
630 //Search and display movies with the chosen genre
631 givenGenre(genre, total-1);
632
633 tableBottom();
634 }else{
635 //If there are no movies with the chosen genre
636 System.out.println("\nThere are no " + genre + " movies.");
637
638 //Pause screen before clearing
639 System.out.print("\nPress enter to continue...");
640 input.skip("\n");
641 input.skip("\n");
642
643 clearScreen();
644 View();
645 }
646 }while(!searchGenre(genre));
647
648 //Pause screen before clearing
649 System.out.print("\nPress enter to continue...");
650 input.skip("\n");
651 input.skip("\n");
652
653 break;
654
655 //List all by Age Rating
656 case '3':
657 String ageRating;
658 do{
659 ageRating = ageRatingSelection(4);
660
661 clearScreen();
662
663 if(searchAgeRating(ageRating)){
664 //If there are movies with the chosen age rating
665 //Table Name
666 System.out.printf("%49s %n",ageRating + " Movie(s)");
667
668 tableTop();
669
670 //Search and display movies with the chosen Age Rating
671 givenAgeRating(ageRating, total-1);
672
673 tableBottom();
674 }else{
675 //If there are no movies with the chosen age rating
676 System.out.println("\nThere are no " + ageRating + " movies.");
677
678 //Pause screen before clearing
679 System.out.print("\nPress enter to continue...");
680 input.skip("\n");
681 input.skip("\n");
682
683 clearScreen();
684 View();
685 }
686 }while(!searchAgeRating(ageRating));
687
688 //Pause screen before clearing
689 System.out.print("\nPress enter to continue...");
690 input.skip("\n");
691 input.skip("\n");
692
693 break;
694
695 //Search by Name
696 case '4':
697 String name;
698 input.skip("\n");
699 System.out.println("Please enter the name of the movie : ");
700 name = input.nextLine();
701
702 //find the number of movies with the given name
703 int sumOfSameName = findTotalByName(name, total-1, 0);
704
705 if(sumOfSameName == 0){
706 //If there are no movies with the given name
707 System.out.println("\nThere are no movies named " + name + ".\n");
708
709 //Pause screen before clearing
710 System.out.print("\nPress enter to continue...");
711 input.skip("\n");
712
713 clearScreen();
714 View();
715
716 }else if(sumOfSameName > 1){
717 //If there are more than 1 movie with the same name
718 clearScreen();
719
720 int idNum;
721 int index;
722
723 do{
724 //Table Name
725 System.out.printf("%49s %n","Search by Name");
726
727 tableTop();
728
729 //Search and display movies with the chosen Name
730 givenName(name, total-1);
731
732 tableBottom();
733
734 System.out.print("Please enter the ID of the movie you want to view in detail : ");
735 idNum = input.nextInt();
736
737 //find the index of the movie that has the entered ID
738 index = searchId(idNum);
739
740 if(index != -1 && name.equals(movies[index].getName())){
741 //if there is a movie with the same ID and name
742 movies[index].displayDetails();
743
744 //Pause screen before clearing
745 System.out.print("\nPress enter to continue...");
746 input.skip("\n");
747 input.skip("\n");
748
749 clearScreen();
750
751 }else{
752 //if there is no movie with the same ID and name
753 System.out.print("'" + idNum +"' is an invalid choice.\n");
754
755 //Pause screen before clearing
756 System.out.print("\nPress enter to continue...");
757 input.skip("\n");
758 input.skip("\n");
759
760 clearScreen();
761
762 }
763
764 }while(!(index != -1 && name.equals(movies[index].getName())));
765
766 }else{
767 //There is only one movie with the same name
768 movies[searchName(name)].displayDetails();
769 //Pause screen before clearing
770 System.out.print("\nPress enter to continue...");
771 input.skip("\n");
772 }
773
774 break;
775
776 //Go Back to Main Menu
777 case '0':
778 mainCaller();
779
780 default:
781 System.out.println("'" + choice + "' is an invalid choice.\n");
782 View();
783 }
784
785 clearScreen();
786
787 mainCaller();
788
789 }
790
791 //View Menu Option 1 : View All
792 public static void viewAll(){
793 tableTop();
794
795 //Display each row of recorded movies
796 for(int i = 0; i<total; i++){
797 movies[i].displayRow();
798 }
799
800 tableBottom();
801 }
802
803 //View Menu Option 2 : List all, given Genre
804 //Selecting genre
805 //selection is the number of the main menu option that was chosen
806 static String genreSelection(int selection){
807 char choice;
808 String genre;
809 do{
810 genre = "";
811 System.out.println("*-------------------<Genres>------------------*");
812 System.out.println(" 1. Action 2. Adventure");
813 System.out.println(" 3. Comedy 4. Drama");
814 System.out.println(" 5. Horror 6. Sci-Fi");
815 System.out.println(" 7. Romance 8. Others");
816 System.out.println(" 0. Go Back");
817 System.out.println("*-------------------<******>------------------*\n");
818
819 System.out.print("Please enter your choice : ");
820
821 choice = input.next().charAt(0);
822
823 switch(choice){
824 case '1':
825 genre = "Action";
826 break;
827
828 case '2':
829 genre = "Adventure";
830 break;
831
832 case '3':
833 genre = "Comedy";
834 break;
835
836 case '4':
837 genre = "Drama";
838 break;
839
840 case '5':
841 genre = "Horror";
842 break;
843
844 case '6':
845 genre = "Sci-Fi";
846 break;
847
848 case '7':
849 genre = "Romance";
850 break;
851
852 case '8':
853 genre = "Others";
854 break;
855
856 case '0':
857 clearScreen();
858
859 if(selection==5){
860 //if genreSelection(...) was called from under the view menu.
861 View();
862
863 }else{
864 //if genreSelection(...) was called from other Main Menu options. E.g : Add New Record, Update, Delete etc
865 mainCaller();
866 }
867
868 break;
869
870 default:
871 System.out.print("\n'" + choice + "' is an invalid choice.\n");
872
873 System.out.print("\nPress enter to continue...");
874 input.skip("\n");
875 input.skip("\n");
876
877 clearScreen();
878 genre = "INVALID";
879 break;
880 }
881 }while(genre.equals("INVALID"));
882
883 return genre;
884 }
885
886 //View Menu Option 3 : List all, given Age Rating
887 //Selecting age rating
888 //selection is the number of the main menu option that was chosen
889 static String ageRatingSelection(int selection){
890 char choice;
891 String ageRating;
892 do{
893 ageRating = "";
894 System.out.println("*-----------------<Age Rating>----------------*");
895 System.out.println(" 1. G 2. PG");
896 System.out.println(" 3. PG-13 4. R");
897 System.out.println(" 0. Go Back");
898 System.out.println("*-----------------<**********>----------------*\n");
899
900 System.out.print("Please enter your choice : ");
901
902 choice = input.next().charAt(0);
903
904 switch(choice){
905 case '1':
906 ageRating = "G";
907 break;
908
909 case '2':
910 ageRating = "PG";
911 break;
912
913 case '3':
914 ageRating = "PG-13";
915 break;
916
917 case '4':
918 ageRating = "R";
919 break;
920
921 case '0':
922 clearScreen();
923
924 if(selection==5){
925 //if genreSelection(...) was called from under the view menu.
926 View();
927
928 }else{
929 //if genreSelection(...) was called from other Main Menu options. E.g : Add New Record, Update, Delete etc
930 mainCaller();
931 }
932
933 break;
934
935 default:
936 System.out.print("\n'" + choice + "' is an invalid choice.\n");
937
938 System.out.print("\nPress enter to continue...");
939 input.skip("\n");
940 input.skip("\n");
941
942 clearScreen();
943 ageRating = "INVALID";
944 break;
945 }
946 }while(ageRating.equals("INVALID"));
947
948 return ageRating;
949 }
950
951 //(END) Main Menu Option 5 : View ------------------------------------------------------/
952
953 //Main Menu Option 6 : Create Text File ------------------------------------------------\
954 static void CreateTextFileMenu(){
955 char choice;
956
957 System.out.println("*------------------<Create Text File>-----------------*");
958 System.out.println(" 1. Table Format 2. Detailed Format");
959 System.out.println(" 0. Go Back");
960 System.out.println("*------------------<****************>-----------------*\n");
961
962 System.out.print("Please enter your choice : ");
963
964 choice = input.next().charAt(0);
965
966 switch(choice){
967 case '1':
968 CreateTextFileFunction(1);
969
970 break;
971
972 case '2':
973 CreateTextFileFunction(2);
974
975 break;
976
977 case '0':
978 clearScreen();
979 mainCaller();
980 break;
981
982 default:
983 System.out.print("\n'" + choice + "' is an invalid choice.\n");
984
985 System.out.print("\nPress enter to continue...");
986 input.skip("\n");
987 input.skip("\n");
988
989 clearScreen();
990
991 CreateTextFileMenu();
992 break;
993 }
994 }
995
996 static void CreateTextFileFunction(int textFileFormat){
997 String fileName;
998
999 input.skip("\n");
1000 System.out.print("Please enter what you want to name your file : ");
1001 fileName = input.nextLine() + ".txt";
1002
1003 try {
1004 File myFile = new File(fileName);
1005 if(myFile.createNewFile()){
1006 System.out.println("File succesfully created : " + myFile.getName());
1007
1008 FileWriter fileWriter = new FileWriter(fileName);
1009 PrintWriter printWriter = new PrintWriter(fileWriter);
1010
1011 if(textFileFormat == 1){
1012 //Text File : Table Format
1013
1014 //Border
1015 for(int i = 0; i<91; i++){
1016 printWriter.print("=");
1017 }
1018
1019 printWriter.println("\nMovies Management System : Movie Records in Table Format");
1020
1021 //Table Border
1022 for(int i = 0; i<91; i++){
1023 printWriter.print("=");
1024 }
1025
1026 printWriter.println("");
1027
1028 //Table Header
1029 printWriter.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
1030
1031 //Table Divider
1032 printWriter.print("|");
1033 for(int i = 0; i<89; i++){
1034 printWriter.print("-");
1035 }
1036 printWriter.println("|");
1037
1038 //Display each row of recorded movies
1039 for(int i = 0; i<total; i++){
1040 movies[i].writeTxtFileRow(printWriter);
1041 }
1042
1043 //Table Bottom Border
1044 for(int i = 0; i<91; i++){
1045 printWriter.print("=");
1046 }
1047
1048 }else{
1049 //Text File : Detailed Format
1050
1051 //Border
1052 for(int i = 0; i<91; i++){
1053 printWriter.print("=");
1054 }
1055
1056 printWriter.println("\nMovies Management System : Movie Records in Detailed Format");
1057
1058 //Border
1059 for(int i = 0; i<91; i++){
1060 printWriter.print("=");
1061 }
1062
1063 for(int i = 0; i<total; i++){
1064 movies[i].writeTxtFileDetails(printWriter);
1065 //Border
1066 for(int j = 0; j<91; j++){
1067 printWriter.print("=");
1068 }
1069 }
1070 }
1071
1072 printWriter.close();
1073
1074 System.out.print("\nPress enter to continue...");
1075 input.skip("\n");
1076
1077 }else{
1078 System.out.println("File already exists.");
1079
1080 System.out.print("\nPress enter to continue...");
1081 input.skip("\n");
1082
1083 clearScreen();
1084 CreateTextFileMenu();
1085
1086 }
1087
1088 }catch(IOException e){
1089 System.out.println("An error occurred.");
1090 e.printStackTrace();
1091
1092 }
1093 }
1094
1095 //(END) Main Menu Option 6 : Create Text File ------------------------------------------/
1096
1097//////////////////////////////////////////////////////////////////////////////////////////////
1098//////////////////////////////////////////////////////////////////////////////////////////////
1099
1100 //Call main method
1101 static void mainCaller(){
1102 main(null);
1103 }
1104
1105 public static void main(String[] args){
1106 //Initialise array of objects of Movie class
1107 if(!isInitialised){
1108 initialiseArray();
1109 isInitialised = true;
1110 }
1111
1112 char choice;
1113
1114 System.out.println("*--------------<Movie Management System>--------------*");
1115 System.out.println(" 1. Add Default Records 2. Add New Record");
1116 System.out.println(" 3. Update Movie Record 4. Delete Movie Record");
1117 System.out.println(" 5. View Movie Records 6. Create Text File");
1118 System.out.println(" 0. Exit");
1119 System.out.println("*--------------<***********************>--------------*\n");
1120
1121 System.out.print("Please enter your choice : ");
1122 choice = input.next().charAt(0);
1123
1124 switch(choice){
1125 case'1':
1126 fillObjects();
1127 System.out.println("\nDefault objects succesfully added.");
1128
1129 System.out.print("\nPress enter to continue...");
1130 input.skip("\n");
1131 input.skip("\n");
1132
1133 clearScreen();
1134 break;
1135
1136 case'2':
1137 clearScreen();
1138 AddRecord();
1139 break;
1140
1141 case'3':
1142 //update
1143 clearScreen();
1144 break;
1145
1146 case'4':
1147 Delete();
1148 clearScreen();
1149 break;
1150
1151 case'5':
1152 clearScreen();
1153 View();
1154 clearScreen();
1155 break;
1156
1157 case'6':
1158 clearScreen();
1159 CreateTextFileMenu();
1160 clearScreen();
1161 break;
1162
1163 case'0':
1164 System.exit(0);
1165 break;
1166 default:
1167 System.out.print("\n'" + choice + "' is an invalid choice.\n");
1168
1169 System.out.print("\nPress enter to continue...");
1170 input.skip("\n");
1171 input.skip("\n");
1172
1173 break;
1174 }
1175 mainCaller();
1176
1177 }
1178}