使用用户输入从另一个阵列列表返回指定项目的新阵列列表

时间:2017-03-20 01:26:37

标签: java arrays arraylist

所以我的目标是返回一个数组列表,当用户输入他们感兴趣的电影类型时,它会从数据库数组列表中指定电影。我假设我需要从用户输入中设置类别,所以我创建了一个新的对象,以便这样做。我被困的地方是为指定电影类别的新数组列表(在MovieDB类中)提供代码块。有人能指出我正确的方向吗?

主要

import java.util.Scanner;

public class ListMoviesApp {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    System.out.println("Welcome to the Movie List App!");
    System.out.println("There are 100 movies in the list");

    String title = null;
    String category = null;

    String choice = "y";
    while (choice.equalsIgnoreCase("y")) {

    System.out.println("What genre are you interested in?");
    Movie newMovie = new Movie(title,category);
    category = sc.nextLine().toLowerCase();
    newMovie.setCategory(category);

    System.out.println(MovieDB.getMovies(category));

    System.out.println("Would you like to continue? (y/n)");
    choice = sc.nextLine();
}

System.out.println("Thanks for using the Movies List App! Goodbye!");
sc.close();
}
}

电影

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;
}
}

MovieDB

import java.util.ArrayList;

public class MovieDB {

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

public static ArrayList<Movie> getAllMovies() {
    allMovies.add(new Movie("Citizen Kane", "drama"));
    allMovies.add(new Movie("Casablanca", "drama"));
    allMovies.add(new Movie("The Wizard Of Oz", "musical"));
    allMovies.add(new Movie("Singin' In The Rain", "musical"));
    allMovies.add(new Movie("Star Wars", "scifi"));
    allMovies.add(new Movie("2001: A Space Odyssey", "scifi"));
    allMovies.add(new Movie("Psycho", "horror"));
    allMovies.add(new Movie("King Kong", "horror"));
    allMovies.add(new Movie("Annie Hall", "comedy"));
    allMovies.add(new Movie("M*A*S*H", "comedy"));
    allMovies.add(new Movie("Lion King", "animated"));
    allMovies.add(new Movie("Fantasia", "animated"));

    return allMovies;
}

public static ArrayList<Movie> getMovies(String category) {
    ArrayList<Movie> movies = new ArrayList<>();
    // Here I need code to return only movies from a specific category
    // From input from the user in the main

    return movies;
}    
}

1 个答案:

答案 0 :(得分:0)

你只需要循环allMovies并找出类别与输入相等的电影。例如:

public class MovieDB {

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

public static ArrayList<Movie> getAllMovies() {
    allMovies.add(new Movie("Citizen Kane", "drama"));
    allMovies.add(new Movie("Casablanca", "drama"));
    allMovies.add(new Movie("The Wizard Of Oz", "musical"));
    allMovies.add(new Movie("Singin' In The Rain", "musical"));
    allMovies.add(new Movie("Star Wars", "scifi"));
    allMovies.add(new Movie("2001: A Space Odyssey", "scifi"));
    allMovies.add(new Movie("Psycho", "horror"));
    allMovies.add(new Movie("King Kong", "horror"));
    allMovies.add(new Movie("Annie Hall", "comedy"));
    allMovies.add(new Movie("M*A*S*H", "comedy"));
    allMovies.add(new Movie("Lion King", "animated"));
    allMovies.add(new Movie("Fantasia", "animated"));

    return allMovies;
}

public static ArrayList<Movie> getMovies(String category) {
    if (allMovies.isEmpty()) {
        // Lazy initialize allMovies
        getAllMovies();
    }

    ArrayList<Movie> movies = new ArrayList<>();
    // Here I need code to return only movies from a specific category
    // From input from the user in the main
    if (category == null) {
        return movies;
    }

    for (Movie movie : allMovies) {
        if (movie.getCategory().equals(category)) {
            movies.add(movie);
        }
    }

    return movies;
}
}

更新:您无法打印ArrayList之类的内容 System.out.println(MovieDB.getMovies(category));

在覆盖Movie类的toString()方法后,可以将ArrayList打印为:

for (Movie movie : MovieDB.getMovies(category)) {
    System.out.println(movie);
}

而不是System.out.println(MovieDB.getMovies(category));