import java.util.ArrayList; import java.util.Random; /** * This class stores a list of books * @author cs161 * @version spring2019 */ public class BookCase{ // Instance variables private ArrayList books; private Random rng; /** * Creates a new empty bookcase */ public BookCase(){ this.books = new ArrayList(); this.rng = new Random(); } /** * Adds a book to the book case * @param title The title of the book * @param author The author of the book */ public void addBook(String title, String author){ // Create a book using this information Book addition = new Book(title, author); // Add the book to the list this.books.add(addition); } /** * Checks if a book is contained in the book case * @param findBook The title of the book * @return True if the book occurs in the book case, false otherwise */ public boolean containsBook(String findBook){ int current = 0; while(current < books.size()){ Book b = books.get(current); String t = b.getTitle(); if(t.equals(findBook)){ return true; } current++; } return false; } /** * Removes a book from the list * @param currPosition The index of the book to be moved */ public void removeBook(int currPosition){ if(currPosition >= 0 && currPosition < books.size()){ books.remove(currPosition); } } /** * Returns the title of a random book in the book case * @return The title of a randomly chosen book */ public String getRandomBook(){ // Get a random position in the list int pickBook = rng.nextInt(books.size()); // Get the book at that position Book rb = books.get(pickBook); // Return the title of that book return rb.getTitle(); } /** * Returns a string representation of the state of the bookcase * @return A string representing the bookcase */ public String toString(){ int counter = 0; String result = ""; while(counter < books.size()){ Book b = books.get(counter); result = result + b.toString() + "\n"; counter++; } return result; } }