I am new to Java and I am trying to create a library program containing two separate class files. One file contains the Book class while the other contains the Library class.
Book class:
public class Book {
private boolean borrowed;
private static String title;
// Creates a new Book
public Book(String title) { // Implement this method
// Marks the book as rented
Book.title = title;
borrowed = false;
}
public void borrowed() { // Implement this method }
// Marks the book as not rented
borrowed = true;
}
public void returned() { // Implement this method }
borrowed = false;
}
public boolean isBorrowed() {
return borrowed; // Implement this method }
// Returns the title of the book public
}
public static String getTitle() { // Implement this method
return title;
}
}
Library class:
import java.util.ArrayList;
import package.Book;
public class Library {
private String hours;
private String address;
private String title;
private Book book;
private ArrayList<Book> books = new ArrayList<Book>();
// Add the missing implementation (methods and data definitions) to this class
// Constructor – look it up
public Library(String address) {
this.address = address;
hours = "9 am to 5 pm";
}
void printOpeningHours()
{
System.out.println(hours);
}
void printAddress() {
System.out.println(address);
}
void addBook(Book book)
{
this.book = book;
books.add(book);
}
void printAvailableBooks() {
if (books.size() == 0){
System.out.println("There are no books available");
}
else {
System.out.println(books);
}
}
void borrowBook(String title)
{
this.title = title;
for (int i = 0; i < books.size(); i++) {
if ((books.get(i).getTitle()) == title) {
if (book.isBorrowed() == false){
book.borrowed();
}
else {
System.out.println("Sorry, that book is already checked out");
}
}
else {
System.out.println("Sorry, no such book is available");
}
}
}
void returnBook(String title) {
for (int i = 0; i < books.size(); i++) {
if (book.getTitle() == title) {
if (book.isBorrowed() == true){
book.returned();
}
}
}
}
public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
// Add four books to the first library
firstLibrary.addBook(new Book("The Da Vinci Code")); firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities")); firstLibrary.addBook(new Book("The Lord of the Rings"));
// Print opening hours and the addresses
System.out.println("Library hours:");
firstLibrary.printOpeningHours();
System.out.println("Library addresses:");
firstLibrary.printAddress();
System.out.println();
System.out.println("Library hours:");
secondLibrary.printOpeningHours();
System.out.println("Library addresses:");
secondLibrary.printAddress();
// Try to borrow The Lords of the Rings from both libraries System.out.println("Borrowing The Lord of the Rings:"); firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings"); secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
// Print the titles of all available books from both libraries System.out.println("Books available in the first library:"); firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:"); secondLibrary.printAvailableBooks();
System.out.println();
// Return The Lords of the Rings to the first library
System.out.println("Returning The Lord of the Rings:"); firstLibrary.returnBook("The Lord of the Rings");
System.out.println();
}
}
However, my issue is that when I try to add a book in the books array via the addBook(book) method, it doesn't do so, and as a result, it prints out "Sorry, that book is already checked out."
Here is the result for clarification:
Library hours:
9 am to 5 pm
Library addresses:
10 Main St.
Library hours:
9 am to 5 pm
Library addresses:
228 Liberty St.
Sorry, that book is already checked out
Sorry, that book is already checked out
Sorry, that book is already checked out
Books available in the second library:
There are no books available
Returning The Lord of the Rings:
What should I do so that the arraylist will recognize the book objects implemented?