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