Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner; //Used in Driver file
- //import java.io.InputStream;
- //Movie lass file
- public class Movie{
- private int id;
- private String name;
- private String genre;
- private int duration;
- private String ageRating;
- private String description;
- public Movie(){
- id = 0;
- name = "NULL";
- genre = "NULL";
- duration = 0;
- ageRating = "NULL";
- description = "NULL";
- }
- public Movie(int idNo, String na, String gen, int dur, String ageR, String desc){
- id = idNo;
- name = na;
- genre = gen;
- duration = dur;
- ageRating = ageR;
- description = desc;
- }
- public void setId(int idNo){
- id = idNo;
- }
- public void setName(String na){
- name = na;
- }
- public String getName(){
- return name;
- }
- public void setGenre(String gen){
- genre = gen;
- }
- public String getGenre(){
- return genre;
- }
- public void setDuration(int dur){
- duration = dur;
- }
- public void setAgeRating(String ageR){
- ageRating = ageR;
- }
- public String getAgeRating(){
- return ageRating;
- }
- public void setDescription(String desc){
- description = desc;
- }
- public void displayRow(){
- System.out.printf("%-1s %-3s %-50s %-23s %-8s %-1s %n", "|", id, name, genre, ageRating, "|");
- }
- public void displayDetails(){
- System.out.println("Movie Id : " + id);
- System.out.println("Movie Name : " + name);
- System.out.println("Genre : " + genre);
- System.out.println("Duration : " + duration);
- System.out.println("Age Rating : " + ageRating);
- System.out.println("Description : \n" + description);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////
- //In Functions File
- //Global Variable
- public static int size = 10; //Size of array/maximum number of movies that can be recorded
- public static int total = 5;
- public static Movie movies[] = new Movie[size]; //array of Movie class object
- public static int index = 0; //index of movie
- public static Scanner input = new Scanner(System.in);
- public static String pause; //system("Pause") / getch() dummy (OR variable used to pause wait and for user to enter)
- //Initialise array of objects
- public static void initialiseArray(){
- for(int i = 0; i<size; i++){
- movies[i] = new Movie();
- }
- }
- //fill 5 objects with data/details aka HARDCODED OBJECTS
- public static void fillObjects(){
- movies[0] = new Movie(1, "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.");
- movies[1] = new Movie(2, "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.");
- movies[2] = new Movie(3, "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.");
- movies[3] = new Movie(4, "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.");
- movies[4] = new Movie(5, "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.");
- }
- //Call main method
- static void mainCaller(){
- main(null);
- }
- //Clear Command Line Console
- public static void clearScreen(){
- System.out.print("\033[H\033[2J");
- System.out.flush();
- }
- public static void tableTop(){
- //Table Border
- for(int i = 0; i<91; i++){
- System.out.print("=");
- }
- System.out.println("");
- //Table Header
- System.out.printf("%-1s %-3s %-50s %-20s %-11s %-1s %n", "|", "ID", "Name", "Genre","Age Rating", "|");
- //Table Divider
- System.out.print("|");
- for(int i = 0; i<89; i++){
- System.out.print("-");
- }
- System.out.println("|");
- }
- public static void tableBottom(){
- //Table Bottom Border
- for(int i = 0; i<91; i++){
- System.out.print("=");
- }
- System.out.println("");
- }
- //Main Menu Option 4 : View
- public static void View(){
- char choice;
- System.out.println("*--------------------<View>--------------------*");
- System.out.println(" 1. View all.....................(Table Format)");
- System.out.println(" 2. List all, given Genre........(Table Format)");
- System.out.println(" 3. List all, given Age Rating...(Table Format)");
- System.out.println(" 4. Search by Name............(Detailed Format)");
- System.out.println(" 0. Go Back......................(To Main Menu)");
- System.out.println("*--------------------<****>--------------------*\n");
- System.out.print("Please enter your choice : ");
- choice = input.next().charAt(0);
- clearScreen();
- switch(choice){
- //View All
- case '1':
- //Table Name
- System.out.printf("%49s %n","View All");
- viewAll();
- break;
- //List all by Genre
- case '2':
- String genre;
- do{
- //ERROR : genreSelection() wont return a genre value after a invalid choice is entered
- genre = genreSelection();
- System.out.println(genre + " haha"); //Temporary test
- if(searchGenre(genre)){
- //Table Name
- System.out.printf("%49s %n",genre + " Movie(s)");
- tableTop();
- //Search and display movies with the chosen genre
- givenGenre(genre, total-1);
- tableBottom();
- }else{
- System.out.println("There are no " + genre + " movies.\n");
- //Pause screen before clearing
- System.out.print("\nPress enter to continue...");
- pause = input.nextLine();
- pause = input.nextLine();
- clearScreen();
- }
- }while(!searchGenre(genre));
- break;
- //List all by Age Rating
- case '3':
- String ageRating;
- do{
- ageRating = ageRatingSelection();
- if(searchAgeRating(ageRating)){
- //Table Name
- System.out.printf("%49s %n",ageRating + " Movie(s)");
- tableTop();
- //Search and display movies with the chosen Age Rating
- givenAgeRating(ageRating, total-1);
- tableBottom();
- }else{
- System.out.println("There are no " + ageRating + " movies.\n");
- //Pause screen before clearing
- System.out.print("\nPress enter to continue...");
- pause = input.nextLine();
- pause = input.nextLine();
- clearScreen();
- }
- }while(!searchAgeRating(ageRating));
- break;
- //Search by Name
- //CANNOT FIND
- case '4':
- String name;
- input.skip("\n");
- System.out.println("Please enter the name of the movie : ");
- name = input.nextLine();
- if(searchName(name) == -1){
- System.out.println("There are no movies named " + name + ".\n");
- //Pause screen before clearing
- System.out.print("\nPress enter to continue...");
- pause = input.nextLine();
- clearScreen();
- View();
- }else{
- movies[searchName(name)].displayDetails();
- //Pause screen before clearing
- System.out.print("\nPress enter to continue...");
- pause = input.nextLine();
- pause = input.nextLine();
- clearScreen();
- mainCaller();
- }
- break;
- //Go Back to Main Menu
- case '0':
- mainCaller();
- default:
- System.out.println("'" + choice + "' is an invalid choice.\n");
- //input.skip("\n");
- View();
- }
- //Pause screen before clearing
- System.out.print("\nPress enter to continue...");
- pause = input.nextLine();
- pause = input.nextLine();
- clearScreen();
- mainCaller();
- }
- //View Menu Option 1 : View All
- public static void viewAll(){
- tableTop();
- //Display each row of recorded movies
- for(int i = 0; i<size; i++){
- movies[i].displayRow();
- }
- tableBottom();
- }
- //View Menu Option 2 : List all, given Genre
- //Selecting genre
- static String genreSelection(){
- char choice;
- String genre = "";
- System.out.println("*-------------------<Genres>-------------------*");
- System.out.println(" 1. Action 2. Adventure");
- System.out.println(" 3. Comedy 4. Drama");
- System.out.println(" 5. Horror 6. Sci-Fi");
- System.out.println(" 7. Romance 8. Others");
- System.out.println(" 0. Go Back......................(To View Menu)");
- System.out.println("*-------------------<******>-------------------*\n");
- System.out.print("Please enter your choice : ");
- choice = input.next().charAt(0);
- switch(choice){
- case '1':
- genre = "Action";
- break;
- case '2':
- genre = "Adventure";
- break;
- case '3':
- genre = "Comedy";
- break;
- case '4':
- genre = "Drama";
- break;
- case '5':
- genre = "Horror";
- break;
- case '6':
- genre = "Sci-Fi";
- break;
- case '7':
- genre = "Romance";
- break;
- case '8':
- genre = "Others";
- break;
- case '0':
- clearScreen();
- View();
- break;
- default:
- clearScreen();
- System.out.print("'" + choice + "' is an invalid choice.\n\n");
- genreSelection();
- break;
- }
- return genre;
- }
- //Check if there is any movie with the same Genre
- static boolean searchGenre(String genre){
- for(int i = 0; i < size; i++){
- //found
- if(genre == movies[i].getGenre()){
- return true;
- }
- }
- //not found
- return false;
- }
- //Display movies that has the same Genre
- static void givenGenre(String genre, int high){
- if (high < 0){
- return;
- }
- else if (genre == movies[high].getGenre() ){
- movies[high].displayRow();
- givenGenre(genre, high-1);
- }
- else {
- givenGenre(genre, high-1);
- }
- }
- //View Menu Option 3 : List all, given Age Rating
- //Selecting age rating
- static String ageRatingSelection(){
- char choice;
- String ageRating = "";
- System.out.println("*-----------------<Age Rating>-----------------*");
- System.out.println(" 1. G 2. PG");
- System.out.println(" 3. PG-13 4. R");
- System.out.println(" 0. Go Back......................(To View Menu)");
- System.out.println("*-----------------<**********>-----------------*\n");
- System.out.print("Please enter your choice : ");
- choice = input.next().charAt(0);
- switch(choice){
- case '1':
- ageRating = "G";
- break;
- case '2':
- ageRating = "PG";
- break;
- case '3':
- ageRating = "PG-13";
- break;
- case '4':
- ageRating = "R";
- break;
- case '0':
- clearScreen();
- View();
- break;
- default:
- clearScreen();
- System.out.print("'" + choice + "' is an invalid choice.\n\n");
- ageRatingSelection();
- break;
- }
- return ageRating;
- }
- //Check if there is any movie with the same Age Rating
- static boolean searchAgeRating(String ageRating){
- for(int i = 0; i < size; i++){
- //found
- if(ageRating == movies[i].getAgeRating()){
- return true;
- }
- }
- //not found
- return false;
- }
- //Display movies that has the same AgeRating
- static void givenAgeRating(String ageRating, int high){
- if (high < 0){
- return;
- }
- else if (ageRating == movies[high].getAgeRating() ){
- movies[high].displayRow();
- givenAgeRating(ageRating, high-1);
- }
- else {
- givenAgeRating(ageRating, high-1);
- }
- }
- //Check if there is any movie with the same Name
- static int searchName(String name){
- for(int i = 0; i < size; i++){
- //found
- if(name == movies[i].getName()){
- return i;
- }
- }
- //not found
- return -1;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////
- public static void main(String[] args){
- //Create input object of Scanner class
- String dummyS;
- int dummyI;
- //Initialise array of objects of Movie class
- if(index == 0){
- initialiseArray();
- }
- fillObjects();
- View();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement