BarackBarackBarack
BarackBarackBarack

Reputation: 101

How do I print one or multiple objects from an ArrayList with a particular attribute?

I have a Book class:

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

And I have a LibraryDatabase class that has an ArrayList of two Book objects:

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

I want to allow the user to choose which genre they would like and have the console print out all objects that have that genre as an attribute. I have this method in a separate class:

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

  }

I don't know what to put inside the if statement in order to print out all objects with that genre. Thanks for the help.

Upvotes: 0

Views: 205

Answers (3)

badger
badger

Reputation: 3256

I suggest at least refactor books() method like this to prevent reference escape

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

and refactor showTitles method to make it use java 8 features and more general:

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

}

and use this API like this:

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

Upvotes: 0

Zaphod Beeblebrox
Zaphod Beeblebrox

Reputation: 679

There is something fundamentally wrong with your design. Your LibraryDatabase shouldn't extend Book.

You can either expose a findBookByGenre(String genre) method in your LibraryDatabase class or filter the list returned by books().

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

Upvotes: 0

Ayush Garg
Ayush Garg

Reputation: 2517

A simple for loop that goes through each Book in the database should work:

for (Book book : bookDatabase) {
    if (book.getBookGenre().equals(genre) {
        System.out.println(book);
    }
}

Upvotes: 1

Related Questions