-1

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?

  • 2
    *private static String title * why it is static? – kofemann Sep 12 '21 at 19:54
  • 2
    Use "equas" method to compare strings – kofemann Sep 12 '21 at 19:56
  • While I believe it fixed the borrowBook method, I still need the addBook method so that it allows book objects to be stored in the array list Books. – yungstirjoey666 Sep 12 '21 at 20:51
  • @yungstirjoey666 how about you follow the given answers instead of the comments. Your `addBook` is fine, it is just that your `borrowBook` is sooo broken that I doubt you fixed it unless you pasted my answer. – luk2302 Sep 13 '21 at 07:47

3 Answers3

4

Your borrowBook method is all over the place.

It should be something along the lines of

void borrowBook(String title) { 
    for (Book book : books) {
        if (book.getTitle().equals(title)) {
            if (!book.isBorrowed()) {
                book.borrowed();
            } else {
                System.out.println("Sorry, that book is already checked out");
            }
            return;
        }
    }
    System.out.println("Sorry, no such book is available");
}

That

  • removes the title assignment
  • simplifies the boolean logic
  • properly compares strings
  • properly returns early
  • only prints the error message at the very end
  • removes usage of the book field (which combined with the title screwed everything up)
  • utilizes a for-each instead of a regular for
  • don't make the Book's title or getTitle static.

Additionally remove the title and book fields from the Library entirely, they should not be there ever.

And then finally note that the second library really has no books added to it.

You should implement the returnBook in a similar fashion, it has a similar string comparison flaw.

luk2302
  • 55,258
  • 23
  • 97
  • 137
1

The check

if ((books.get(i).getTitle()) == title) {

Is an equally by reference, which always evaluated to *false * in your case. Change it as:

 if ((books.get(i).getTitle()).equals(title)) {
kofemann
  • 4,217
  • 1
  • 34
  • 39
0

I edited some part of your code. I split into two seperate file your Library.java. The first is Library.java, second is Application.java.

Your Book.java:

public class Book { 
    private boolean borrowed;
    private String title; 

    // Creates a new Book 
    public Book(String title) { // Implement this method  
    // Marks the book as rented 
        this.title = title;
        this.borrowed = false;
    }
    public void borrowed() { // Implement this method } 
    // Marks the book as not rented
        this.borrowed = true;
    }
    public void returned() { // Implement this method } 
        this.borrowed = false;
    }
    public boolean isBorrowed() {
        // Returns the title of the book public 
        return this.borrowed; // Implement this method } 
    }
    public String getTitle() { // Implement this method
        return this.title;
    }    
   
    @Override
    public String toString(){
        return "title: " + this.title + ", borrowed: " + this.borrowed;
    }
}

Your Library.java:

import java.util.ArrayList;

public class Library {
    private String hours;
    private String address;
    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) {
        books.add(book);
        System.out.println("Added book " + book.getTitle());
    }

    void printAvailableBooks() {
        if (books.size() == 0) {
            System.out.println("There are no books available");
        } else {
            System.out.println(books);
        }
    }

    void borrowBook(String title) {
        System.out.println("Borrowing the " + title);
        int foundIndex = Integer.MIN_VALUE;
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getTitle().equals(title)) {
                foundIndex = i;
                break;
            }
        }
        if (foundIndex >= 0) {
            if (!books.get(foundIndex).isBorrowed()) {
                books.get(foundIndex).borrowed();
                System.out.println("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) {
        int foundIndex = Integer.MIN_VALUE;
        System.out.println("Returning the book " + title);
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).getTitle().equals(title)) {
                foundIndex = i;
            }
        }
        if (foundIndex >= 0) {
            if (books.get(foundIndex).isBorrowed()) {
                books.get(foundIndex).returned();
                System.out.println("Book returned.");
            } else {
                System.out.println("Book didn't borrowed.");
            }
        } else {
            System.out.println("Sorry, no such book is available!");
        }
    }
}

Application.java:

public class Application {
    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(); 
        firstLibrary.printAvailableBooks();
        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();

    }
}

Some advices about design of your code:

  • You shouldn't make static your object's properties.
  • You should override toString method of a class that you want to print with println.
  • If you search in an array, you should get the founded index first and then you should do what you want.
  • You should use equals method of a string object to compare content. == compares object's itself (reference) with another object, not content.
  • Book object in the Library class is unnecessary. You already keep your books list in an ArrayList.