有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从具有特定属性的ArrayList打印一个或多个对象?

我有一个读书班:

public class Book {
    public String title;
  
    public  String author;

    public  int genre;
    
    public Book(){}
  
    public Book(String title, String author, int genre) {
        this.title = title;
        this.author = author;
        this.genre = genre;
    }
  

    public String getBookTitle() {
        return  title;
    }
    
    public String getBookAuthor() {
        return author;
    }
    
    public String getBookGenre() {
        if (genre == 1) {
          return ("Fantasy");
        }
        else if (genre == 2) {
          return ("Science Fiction");
        }
        else if (genre == 3) {
          return ("Dystopian");
        }
        return "genre";
        
    }

    @Override
    public String toString() {
        return ("Title: " + this.getBookTitle() + " \nAuthor: " + this.getBookAuthor() + " \nGenre: " + this.getBookGenre() + "\n");
    }
    
}

我有一个LibraryDatabase类,它有两个Book对象的ArrayList:

import java.util.*;
public class LibraryDatabase extends Book {
  List<Book> bookDatabase = new ArrayList<>();

  public LibraryDatabase() {
      super();
  }

  public List<Book> books() {
    Book book1 = new Book("Harry Potter", "J.K. Rowling", 1);
    Book book2 = new Book("Neuromancer", "William Gibson", 2);

    bookDatabase.add(book1);
    bookDatabase.add(book2);
    return bookDatabase;
  }
}

我想允许用户选择他们想要的类型,并让控制台打印出将该类型作为属性的所有对象。 我在一个单独的类中使用此方法:

public void showTitles() {
    LibraryDatabase libraryDatabase = new LibraryDatabase();
    Scanner kbMeter = new Scanner(System.in);
    String genre = kbMeter.nextLine();
    if (genre.equals(libraryDatabase.getBookGenre())) {
      //???
    }

  }

我不知道在if语句中放入什么才能打印出所有具有该类型的对象。谢谢你的帮助


共 (3) 个答案

  1. # 1 楼答案

    你的设计有一些根本性的问题。你的LibraryDatabase不应该扩展Book

    您可以在LibraryDatabase类中公开findBookByGenre(String genre)方法,也可以筛选books()返回的列表

            libraryDatabase.books().forEach(book -> {
                if ("aaa".equals(book.getBookGenre())) {
                    System.out.println(book.getBookTitle());
                }
            });
    
    
  2. # 2 楼答案

    通过数据库中每个Book的简单for循环应该可以工作:

    for (Book book : bookDatabase) {
        if (book.getBookGenre().equals(genre) {
            System.out.println(book);
        }
    }
    
  3. # 3 楼答案

    我建议至少像这样重构books()方法以防止引用转义

    public Collection<Book> getBooks() {
        Book book1 = new Book("Harry Potter", "J.K. Rowling", 1);
        Book book2 = new Book("Neuromancer", "William Gibson", 2);
    
        bookDatabase.add(book1);
        bookDatabase.add(book2);
        return Collections.unmodifiableCollection(bookDatabase);
    } 
    

    和重构showTitles方法,使其使用java 8功能和更通用的功能:

    public void showTitles(Consumer<Book> consumer) {
        LibraryDatabase libraryDatabase = new LibraryDatabase();
        Scanner kbMeter = new Scanner(System.in);
        String genre = kbMeter.nextLine();
        libraryDatabase.getBooks()
                    .stream()
                    .filter(b -> b.getBookGenre().equals(genre))
                    .forEach(consumer);
    
    }
    

    并像这样使用此API:

    LibraryDatabase db = new LibraryDatabase();
    db.showTitles(b -> System.out.println(String.format("%s, %s, %s", b.getBookAuthor(), b.getBookTitle(), b.getBookGenre())));