打印本书的类型

时间:2018-05-04 14:07:45

标签: java

这是我显示书籍类型的代码。在这段代码中,我只需要打印一本书的类型。那么有很多不仅仅是关于SciFi的类型,我们必须打印一种类型的SciFi。感谢。

public Class Catalogue(){

    private List<Movie> moviesAvailable;

    private List<Movie> moviesRented;
    private List<Genre> genres;
    public Catalogue(){
        moviesAvailable = new LinkedList<Movie>();
        moviesRented = new LinkedList<Movie>();
        genres = new LinkedList<Genre>();
        moviesAvailable.add(new Movie("Matrix",1999,new Genre("SciFi"),3));
        moviesAvailable.add(new Movie("Jurassic Park",1993, new Genre("SciFi"),4));
        moviesAvailable.add(new Movie("The conjuring",1993, new Genre("Horror"),4));

    }
    private getOnlyGenres(){
        System.out.print("The library has the following genres:")
        for(Genre genre:moviesAvailable){
            System.out.print(genre.toString());
        }
    }

}

2 个答案:

答案 0 :(得分:0)

你需要改变一些事情:

  1. 如果Movie课程没有,请添加Movie课程。

  2. 当您遍历电影列表时,会得到Genre个对象,而不是for(Genre genre:moviesAvailable){ 。这意味着您需要更改:

    for(Movie movie : moviesAvailable){
    

    Genre
  3. 现在从每个Movie获取Set并将其添加到List而不是{{1}}。

答案 1 :(得分:0)

以下是删除了一些额外列表的工作示例:

import java.util.*;

class Test {
    public static void main(String[] args) {

        List<Movie> moviesAvailable;

        moviesAvailable = new LinkedList<Movie>();

        moviesAvailable.add(new Movie("Matrix",1999,new Genre("SciFi"),3));
        moviesAvailable.add(new Movie("Jurassic Park",1993, new Genre("SciFi"),4));
        moviesAvailable.add(new Movie("The conjuring",1993, new Genre("Horror"),4));

        moviesAvailable.stream().map(movie -> movie.getGenre().getName()).distinct().forEach(System.out::println);

    }
}

class Genre {
    private String name;
    public String getName() {
        return this.name;
    }
    public void setName(String name)
    {
        this.name = name;
    }

    public Genre (String name) {
        this.name = name;
    }

}

class Movie {
    private String name;
    private int year;
    private Genre genre;
    private int whatever;

    public Genre getGenre()
    {
        return this.genre;
    }

    public Movie (String name, int year, Genre genre, int whatever) {
        this.name = name;
        this.year = year;
        this.genre = genre;
        this.whatever = whatever;
    }
}