0

So i have this homework, to create a java movie program. It should have the add movie (title, actor and date of appearance), show (all the movies added) and remove movie(by movie title) options. Up till now i was able to create the addMovie(), and showMovie() methods...but i got really stuck ad removeMovies().

Here is the code for the Main.java:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

    static Scanner input = new Scanner(System.in);
    static ArrayList<Movies> movHolder = new ArrayList<Movies>();

    public static void main(String[] args) {    
        int op = -1;        
        while (op != 0){
            op= menuOption();
            switch(op){
            case 1:
                addMovies();
                break;
            case 2:
                removeMovies();
                break;
            case 3:
                showMovies();               
                break;
            case 0:
                System.out.print("\n\nYou have exited the program!");
                break;
            default:
                System.out.println("\nWrong input!");
            }           
        }
    }

    public static int menuOption(){
        int option;
        System.out.println("\nMenu\n");
        System.out.println("1. Add new movies");
        System.out.println("2. Remove movies");
        System.out.println("3. Show all movies");
        System.out.println("0. Exit program");
        System.out.print("\nChoose an option: ");
        option = input.nextInt();

        return option;      
    }

    public static void addMovies(){
        String t, a, d;

        input.nextLine();
        System.out.println("\n---Adding movies---\n");
        System.out.print("Enter title of movie: ");
        t = input.nextLine();
        System.out.print("Enter actor's name: ");
        a = input.nextLine();
        System.out.print("Enter date of apearance: ");
        d = input.nextLine();

        Movies mov = new Movies(t, a, d);
        movHolder.add(mov);         
    }

    public static void removeMovies(){
        int choice;
        System.out.println("\n---Removing movies by title---\n");
        for(int i = 0; i < movHolder.size(); i++){
            System.out.println((i+1)+ ".) "+ movHolder.get(i).toString());
        }
        System.out.print("Enter movie do you want to remove?");
        choice = input.nextInt();           

    }

    public static void showMovies(){
        System.out.print("---Showing movie list---\n");
        for(int i = 0; i < movHolder.size(); i++){
            System.out.println((i+1)+ ".) "+ movHolder.get(i).toString());
        }       
    }   
}

And here is the Movies.java with the Movie class:

public class Movies {

    private String title;
    private String actor;
    private String date;

    public Movies (String t, String a, String d){
        title = t;
        actor = a;
        date = d;
    }
    public Movies(){
        title = "";
        actor = "";
        date = "";
    }

    public String getTitle(){
        return title;
    }
    public String getActor(){
        return actor;
    }
    public String getDate(){
        return date;
    }
    public String toString(){
        return "\nTitle: " + title + 
               "\nActor: " + actor + 
               "\nRelease date: " + date;
    }   

}

As you could probably see, i am a very beginner java programmer. Please, if there is anyway someone could help with the removeMovie() method, i would be very grateful.

2 Answers 2

1

Since you have the index of the movie that should be removed (choice - 1) you can use ArrayList.remove(int)

    System.out.print("Enter movie do you want to remove?");
    choice = input.nextInt();
    movHolder.remove(choice-1);
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the remove(int index) method:

public static void removeMovies(){
     int choice;
     System.out.println("\n---Removing movies by title---\n");
     for(int i = 0; i < movHolder.size(); i++){
         System.out.println((i+1)+ ".) "+ movHolder.get(i).toString());
     }
     System.out.print("Enter movie do you want to remove?");
     choice = input.nextInt();

     // Decrement the index because you're asking the user for a 1 based input.    
     movHolder.remove(choice - 1)
}       

}

1 Comment

If you found his answer helpful then click the tick mar button to show that he answered you correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.