0

I am having a problem displaying an array list in java. The program is supposed to call a category of movies from an array list. Here is my code.

public class Movie {

private String title;
private String category;

public Movie(String title, String category) {
    this.title = title;
    this.category = category;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

/

 public class MovieDB {

private static ArrayList<Movie> allMovies = new ArrayList<>(100);

public static ArrayList<Movie> getAllMovies() {
    allMovies.add(new Movie("Citizen Kane", "drama"));
    allMovies.add(new Movie("Casablanca", "drama"));
    allMovies.add(new Movie("The Godfather", "drama"));
    allMovies.add(new Movie("Gone With The Wind", "drama"));
    allMovies.add(new Movie("Lawrence Of Arabia", "drama"));
    allMovies.add(new Movie("The Wizard Of Oz", "musical"));
    allMovies.add(new Movie("The Graduate", "drama"));
    allMovies.add(new Movie("On The Waterfront", "drama"));
    allMovies.add(new Movie("Schindler's List", "drama"));
    allMovies.add(new Movie("Singin' In The Rain", "musical"));
    allMovies.add(new Movie("It's A Wonderful Life", "drama"));
    allMovies.add(new Movie("Sunset Boulevard", "drama"));
    allMovies.add(new Movie("The Bridge On The River Kwai", "drama"));
    allMovies.add(new Movie("Some Like It Hot", "drama"));
    allMovies.add(new Movie("Star Wars", "scifi"));
    allMovies.add(new Movie("All About Eve", "drama"));
    allMovies.add(new Movie("The African Queen", "drama"));
    allMovies.add(new Movie("Psycho", "horror"));
    allMovies.add(new Movie("Chinatown", "drama"));
    allMovies.add(new Movie("One Flew Over The Cuckoo's Nest", "drama"));
    allMovies.add(new Movie("The Grapes Of Wrath", "drama"));
    allMovies.add(new Movie("2001: A Space Odyssey", "scifi"));
    allMovies.add(new Movie("The Maltese Falcon", "drama"));
    allMovies.add(new Movie("Raging Bull", "drama"));
    allMovies.add(new Movie("E.T. The extra-terrestrial", "scifi"));
    allMovies.add(new Movie("Dr. Strangelove", "drama"));
    allMovies.add(new Movie("Bonnie And Clyde", "drama"));
    allMovies.add(new Movie("Apocalypse Now", "drama"));
    allMovies.add(new Movie("Mr. Smith Goes to Washington", "drama"));
    allMovies.add(new Movie("The Treasure Of The Sierra Madre", "drama"));
    allMovies.add(new Movie("Annie Hall", "comedy"));
    allMovies.add(new Movie("The Godfather Part II", "drama"));
    allMovies.add(new Movie("High Noon", "drama"));
    allMovies.add(new Movie("To Kill A Mockingbird", "drama"));
    allMovies.add(new Movie("It Happened One Night", "drama"));
    allMovies.add(new Movie("Midnight Cowboy", "drama"));
    allMovies.add(new Movie("The Best Years Of Our Lives", "drama"));
    allMovies.add(new Movie("Double Indemnity", "drama"));
    allMovies.add(new Movie("Doctor Zhivago", "drama"));
    allMovies.add(new Movie("North By Northwest", "drama"));
    allMovies.add(new Movie("West Side Story", "musical"));
    allMovies.add(new Movie("Rear Window", "drama"));
    allMovies.add(new Movie("King Kong", "horror"));
    allMovies.add(new Movie("The Birth Of A Nation", "drama"));
    allMovies.add(new Movie("A Streetcar Named Desire", "drama"));
    allMovies.add(new Movie("A Clockwork Orange", "scifi"));
    allMovies.add(new Movie("Taxi Driver", "drama"));
    allMovies.add(new Movie("Jaws", "horror"));
    allMovies.add(new Movie("Snow White And The Seven Dwarfs", "animated"));
    allMovies.add(new Movie("Butch Cassidy And The Sundance Kid", "drama"));
    allMovies.add(new Movie("The Philadelphia Story", "drama"));
    allMovies.add(new Movie("From Here To Eternity", "drama"));
    allMovies.add(new Movie("Amadeus", "drama"));
    allMovies.add(new Movie("All Quiet On The Western Front", "drama"));
    allMovies.add(new Movie("The Sound Of Music", "musical"));
    allMovies.add(new Movie("M*A*S*H", "comedy"));
    allMovies.add(new Movie("The Third Man", "drama"));
    allMovies.add(new Movie("Fantasia", "animated"));
    allMovies.add(new Movie("Rebel Without A Cause", "drama"));
    allMovies.add(new Movie("Raiders Of The Lost Ark", "drama"));
    allMovies.add(new Movie("Vertigo", "drama"));
    allMovies.add(new Movie("Tootsie", "comedy"));
    allMovies.add(new Movie("Stagecoach", "drama"));
    allMovies.add(new Movie("Close Encounters Of The Third Kind", "scifi"));
    allMovies.add(new Movie("The Silence Of The Lambs", "horror"));
    allMovies.add(new Movie("Network", "drama"));
    allMovies.add(new Movie("The Manchurian Candidate", "drama"));
    allMovies.add(new Movie("An American In Paris", "drama"));
    allMovies.add(new Movie("Shane", "drama"));
    allMovies.add(new Movie("The French Connection", "drama"));
    allMovies.add(new Movie("Forrest Gump", "drama"));
    allMovies.add(new Movie("Ben-Hur", "drama"));
    allMovies.add(new Movie("Wuthering Heights", "drama"));
    allMovies.add(new Movie("The Gold Rush", "drama"));
    allMovies.add(new Movie("Dances With Wolves", "drama"));
    allMovies.add(new Movie("City Lights", "drama"));
    allMovies.add(new Movie("American Graffiti", "drama"));
    allMovies.add(new Movie("Rocky", "drama"));
    allMovies.add(new Movie("The Deer Hunter", "drama"));
    allMovies.add(new Movie("The Wild Bunch", "drama"));
    allMovies.add(new Movie("Modern Times", "drama"));
    allMovies.add(new Movie("Giant", "drama"));
    allMovies.add(new Movie("Platoon", "drama"));
    allMovies.add(new Movie("Fargo", "drama"));
    allMovies.add(new Movie("Duck Soup", "comedy"));
    allMovies.add(new Movie("Mutiny On The Bounty", "drama"));
    allMovies.add(new Movie("Frankenstein", "horror"));
    allMovies.add(new Movie("Easy Rider", "drama"));
    allMovies.add(new Movie("Patton", "drama"));
    allMovies.add(new Movie("The Jazz Singer", "drama"));
    allMovies.add(new Movie("My Fair Lady", "musical"));
    allMovies.add(new Movie("A Place In The Sun", "drama"));
    allMovies.add(new Movie("The Apartment", "drama"));
    allMovies.add(new Movie("Goodfellas", "drama"));
    allMovies.add(new Movie("Pulp Fiction", "drama"));
    allMovies.add(new Movie("The Searchers", "drama"));
    allMovies.add(new Movie("Bringing Up Baby", "drama"));
    allMovies.add(new Movie("Unforgiven", "drama"));
    allMovies.add(new Movie("Guess Who's Coming To Dinner", "drama"));
    allMovies.add(new Movie("Yankee Doodle Dandy", "musical"));
    return allMovies;
}

public static ArrayList<Movie> getMovies(String category) {
    ArrayList<Movie> movies = new ArrayList<>();
    return (ArrayList<Movie>) getAllMovies().stream().filter(t->t.getCategory().equals(category)).collect(Collectors.toList());
}    
public static List<Movie> getMovies1(String category) {
    // TODO: add code to add all movies with the specified category
    List<Movie> movies= new ArrayList<>();
    for (Movie movie : getAllMovies()) {
        if (movie.getCategory().equals(category)){
            movies.add(movie);
        }
    }
    return movies;
}

     @Override
      public String toString() {
return "Movie{" +
        "title='" + title + '\'' +
        ", category='" + category + '\'' +
        '}';
        }
        }

/

public class MovieApp {

public static void main(String[] args) {
    System.out.println("Welcome to the Movie Application.");
    System.out.println("There are 100 movies in the list.");
    System.out.println("What category are you interested in?");
    System.out.println();

    //Create Scanner object
    Scanner sc = new Scanner(System.in);

    //Initialize variables
    String choice="y";

    //Create a while loop to continue until user enters no.
    while(choice.equalsIgnoreCase("y"))
    {
        //Get the input from the user
        String movieList = Validator.getString(sc, 
                "Animated, Drama, Horror, SciFi \n"
                        + "Please enter a category: ");

        //Get the movie object
        List<Movie> movie = MovieDB.getMovies(movieList);

        //Display the output
        System.out.println(); //print blank line
        System.out.println("Movie Category:  " + movie);

        //See if the user wants to continue
        choice = Validator.getString(sc, "Continue: (y/n): ");
        System.out.println();
    }    
}

/

public class Validator {

public static String getString(Scanner sc, String prompt){

    System.out.print(prompt);
    String s = sc.next(); //Reads the user entry
    sc.nextLine();  //Discards any other data on the line
    return s;
}

public static String getLine(Scanner sc, String prompt){

    System.out.print(prompt);
    String s = sc.nextLine();        // read the whole line
    return s;
}    

I am having trouble with the MovieApp class.

//Get the movie object ArrayList movie = MovieDB.getMovies(movieList);

This line specifically. Whatever I put in I either get every movie and this assignment11.Movie@55f96302

or just [].

I am not sure how to get the category list to display.

I want it to display like this

Annie Hall MAS*H Tootsie Duck Soup

Chris3546
  • 19
  • 2
  • 2
    Possible duplicate of [How do I print my Java object without getting "SomeType@2f92e0f4"?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – azurefrog Mar 20 '17 at 17:12
  • `System.out.println("Movie Category: " + Arrays.toString(movie))` – MirMasej Mar 20 '17 at 17:18

3 Answers3

0

Based on you needs you'd probably want to do

Map<Category, List<Movie>>

then it would be trivial to find all moves in a genre

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
0

To achieve getMoviesByCategory functionality, I would suggest you should use better dataStructure.Something like:

Map<String, List<Movie>> movies = new HashMap<String, <List>Movie>();
if(movies.get("musical") == null ){
    movies.add(new ArrayList().add(new Movie("My Fair Lady", "musical")));
}else{
   movies.get("musical").add(new Movie("My Fair Lady", "musical"));
}

And then in

public static ArrayList<Movie> getMovies(String category) {
     return movies.get(category);
} 

Apart from this, override toString() in Movie class to print it correctly.

Jaydeep Rajput
  • 3,605
  • 17
  • 35
0

I changed the methods declarations and returns from ArrayList to List which is usually better to use the interface.

In that method you just need to go through the elements and filter them like so:

Java8

public static List<Movie> getMovies(String category) {
    // TODO: add code to add all movies with the specified category
    return getAllMovies().stream().filter(t->t.getCategory().equals(category)).collect(Collectors.toList());
}

For each

public static List<Movie> getMovies(String category) {
    // TODO: add code to add all movies with the specified category
    List<Movie> movies= new ArrayList<>();
    for (Movie movie : getAllMovies()) {
        if (movie.getCategory().equals(category)){
            movies.add(movie);
        }
    }
    return movies;
}

Finally to avoid assignment11.Movie@55f96302, you need to implement a toString():

@Override
public String toString() {
    return "Movie{" +
            "title='" + title + '\'' +
            ", category='" + category + '\'' +
            '}';
}

As others pointed out, the map is the better option to store the data, using the list you need to go through every element of the list to get the movies every time, while with the map, you only have to do it once to build it, and then querying is constant.

Tiago
  • 718
  • 10
  • 16