I need to make a library of books where books can be checked out, returned, and added to. They are organizable by genre, title, and author. When the user gets a list of books, they must decide which one to check out by typing in the title.
I have made a Scanner object to see what they type, but am not sure how to check if the input matches the attribute of the object, which is stored in an ArrayList in another class.
This is the Book class for defining a Book object:
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");
}
}
This is the LibraryDatabase class for storing Book objects:
import java.util.*;
import java.util.function.*;
public class LibraryDatabase extends Book {
List < Book > bookDatabase = new ArrayList < > ();
public LibraryDatabase() {
super();
}
public int blue = bookDatabase.size();
public Collection < Book > getBooks() {
Book book1 = new Book("Harry Potter", "J.K. Rowling", 1);
Book book2 = new Book("Lord of the Rings", "J.R.R. Tolkien", 1);
Book book3 = new Book("I, Robot", "Isaac Asimov", 2);
bookDatabase.add(book1);
bookDatabase.add(book2);
bookDatabase.add(book3);
return Collections.unmodifiableCollection(bookDatabase);
}
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);
}
public void checkOut(Consumer < Book > consumer) {
LibraryDatabase libraryDatabase = new LibraryDatabase();
Scanner kbMeter = new Scanner(System.in);
String title = kbMeter.nextLine();
libraryDatabase.getBooks()
.stream()
.filter(b -> b.getBookTitle().equals(title))
.forEach(consumer);
}
}
The last method in the above class, checkOut, is for making a Scanner and filtering through the ArrayList for any matches. I am just not sure how to call this from another class and use an if statement to check if it matches, so I can put some code inside the if block.
Thanks for any help.
Aucun commentaire:
Enregistrer un commentaire