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