Question
JAVA PROGRAMMING I NEED TO UPDATE THE COD BELOW ACCORDING TO THE INSTRUCTIONs. PLEASE HELP ASAP I NEED IT IN COUPLE HOURS. thanks Please modify
JAVA PROGRAMMING
I NEED TO UPDATE THE COD BELOW ACCORDING TO THE INSTRUCTIONs. PLEASE HELP ASAP I NEED IT IN COUPLE HOURS. thanks
Please modify Library.java to use a single ArrayList 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 is 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 thecode 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.
import java.time.LocalDate; public class Book { //instance variables private String getName; private String getAuthor; private String getRenter; private LocalDate date; //getter for author & Name public String getName() { return getName; } public String getAuthor() { return getAuthor; } //getter for title public String getTitle() { return getName; } //getter for renter public String getRenter() { return getRenter; } //Constructor with 2 parameters Book(String name,String author) { super(); this.getAuthor = author; this.getName = name; getRenter=null; date=null; } //Constructor with 3 parameters Book(String name,String author,String renter) { super(); this.getAuthor = author; this.getName = name; this.getRenter = renter; date=LocalDate.now(); date=date.plusDays(7); } //toString for Book details public String toString() { return "Book[author="+getAuthor+",title="+getName+"]"; } //method to check if the book is rented public boolean isRented() { if(getRenter!=null) return true; else return false; } //method to rent the book public void rent(String renterName) { this.getRenter=renterName; date=LocalDate.now(); date=date.plusDays(7); } //method to return the book to library public void returnToLibrary() { getRenter=null; date=null; } //method to get the due date public LocalDate getDueDate() { return date; } //main method to test the class public static void main(String args[]) { //Created instance b1 of the Book class and initialize using 3 parameters constructor Book b1=new Book("Machine Learning","Alan","Harry"); System.out.println(b1.toString()); System.out.println("Book rented? "+b1.isRented()); System.out.println("Due Date: "+b1.getDueDate()); //Create instance b2 of the Book class and initialize using 2 parameters constructor Book b2=new Book("Software Testing","Bob"); System.out.println(b2.toString()); System.out.println("Book rented? "+b2.isRented()); System.out.println("Due Date: "+b2.getDueDate()); b2.rent("Alice"); System.out.println("Renter: "+b2.getRenter); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started