Question: I am working on an assignment, I must modify my code in my Library.java to use a single ArrayList (Links to an external site.) of

I am working on an assignment, I must modify my code in my Library.java to use a single ArrayList (Links to an external site.) of Book objects called books. This list should be populated with values at the beginning of the main method. The rest of the code should be modified so that books are the only field used by the application -- all other arrays storing information about the books, renters, and due dates should be deleted and all code using those arrays should be modified to work with books instead.

Eliminate the getRenter, and bookExists methods. Write two new methods:

  • void initializeDatabase() - has no parameters and returns nothing. This method should initialize the list of books to contain the 10 books originally in the arrays that you should have replaced.
  • Book getBook(String bookName) - that takes a single String argument (the title of the book), searches through the books list for the book, and returns the appropriate book object (or null if the title was not matched).

Update the rest of your code to rely on these methods. First, have initializeDatabase() be the first call your main method makes. Next, use a for-each loop over the books list in options #1 and #2. When the user looks for a book, call this method to retrieve a book object, store it in a local variable, and then use the methods of that book object to get details like the renter, author, dueDate, etc, rather than keeping an index and accessing the old arrays you deleted. Check to see if the variable holds the null value to see if the book exists or not. When you're finished, an index variable shouldn't ever by used in the main method anymore.

Here is my code:

Library.java file:

 import java.time.LocalDate; import java.util.Scanner; public class Library { // the names of books in stock in the library private static final String[] booksInStock = new String[] { "The Left Hand of Darkness", "Dune", "Hitchhiker's Guide to the Galaxy", "Do Androids Dream of Electric Sheep?", "Frankenstein", "The Hunger Games", "Never Let Me Go", "Neuromancer", "I Robot", "Snow Crash" }; // the authors of books in stock in the library private static final String[] authors = new String[] { "Ursula Le Guin", "Frank Herbert", "Douglas Adams", "Philip K. Dick", "Mary Shelly", "Suzanne Collins", "Kazuo Ishiguro", "William Gibson", "Isaac Asimov", "Neil Stephenson" }; // the names of renters of each of the books (indexes line up) // null indicates the book is currently at the library private static final String[] renters = new String[booksInStock.length]; // dates that each book was rented private static final LocalDate[] dueDates = new LocalDate[booksInStock.length]; /** returns the renter associated with the book name. * If no one is renting the book, null is returned. * @param bookName * @return */ public static String getRenter(String bookName) { for(int i=0; i 

Book.java file:

import java.time.LocalDate; public class Book { private String name; private String author; private String renter; private boolean rentstatus; private LocalDate dueDate; /** * @param name this variable holds the name of the book * @param author this variable holds the name of the author of the book */ public Book(String name, String author) { this.name = name; this.author = author; renter = null; rentstatus = false; } /** This constructor takes the book and rents it out to the renter and also sets the due date for the book. */ public Book(String name, String author, String renter, LocalDate dueDate) { this.name = name; this.author = author; this.renter = renter; rentstatus = true; this.dueDate = dueDate; this.dueDate = LocalDate.now().plusDays(7); } /** This is a getter that will return the name of the book. */ public String getName() { return name; } /** This is a getter that will return the author of the book. */ public String getAuthor() { return author; } /** This method will return the status of the book. If it is rented it returns true. If not rented it returns false. */ public boolean isRented() { return this.rentstatus; } /** This method will return the due date for the book that is checked out. It will set the due date for one week. * If the book is not checked out, it will return null. */ public LocalDate getDueDate() { if(this.rentstatus){ this.dueDate = LocalDate.now().plusDays(7); return this.dueDate; } else { return null; } } /** This is a getter that will return the renter if the book is checked out. If not checked out, it will return null. */ public String getRenter() { if (this.rentstatus) { return renter; } else{ return null; } } /** After the book is checked back in, this will set everything to null and will set the rentstatus to false. */ public void returnToLibrary() { renter = null; dueDate = null; rentstatus = false; name = null; author = null; } /** This will take the renter's name and will rent the book out to that person. It will assign the book to the renter. * It will change the rentstatus to true. It will also set the due date from one week from checking out the book. */ public void rent(String renter) { this.name = renter; this.renter = name; this.rentstatus = true; this.dueDate = LocalDate.now().plusDays(7); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!