-1

I've been looking at this for ages and I cannot figure out why it cannot find listOfBooks. Have I defined it incorrectly in my public class Library? Here's the code:

Library.java

import java.util.List;
import java.util.ArrayList;

public class Library {
    private List<Book> listOfBooks;
    private int capacity;
    private boolean libLimited;

    public Library() {
        libLimited = false;
    }

    public Library(int inCapacity) {
        libLimited = true;
        capacity = inCapacity;
    }


    public void addBook(Book newBook) {
        if (newBook != null && !newBook.equals("")) {
            throw new IllegalArgumentException("Can't be empty");
        }
        listOfBooks.add(newBook);
    }

    public String numberAvailableBooks() {
        boolean newBookPresent = false;
        for (Book newBook : listOfBooks) {
            if (!newBook.isBorrowed()) {
                return (newBook.getTitle());
                newBookPresent = true;
            }
        }
    }

    public int hasBookWithTitle(String bookTitle) {
        for (Book book : listOfBooks) {
            if (bookTitle.equalsIgnoreCase(listOfBooks.getTitle()) == true) {
                return true;
            }
        }
        return false;
    }

    public String getBookWithTitle(String bookTitle) {
        for (Book book : listOfBooks) {
            if (bookTitle.equlsIgnoreCase(listOfBooks.getTitle()) == true) {
                return listOfBooks;
            }
        }
        return null;
    }
}

Book.java

public class Book {
    private String title;
    private String author;
    private int copies;
    private boolean borrowed;

    public Book(String inAuthor, String inTitle, int inNumberOfCopies) {
        this.author = inAuthor;
        this.title = inAuthor;
        this.copies = inNumberOfCopies;
    }

    public void borrowed() {
        borrowed = true;
    }

    public void rented() {
        borrowed = true;
    }

    public void returned() {
        borrowed = false;
    }

    public boolean isBorrowed() {
        return borrowed;
    }


    public String getAuthor() {
        return this.author;
    }


    public String getTitle() {
        return this.title;

    }

    public int getTotalCopies() {
        return this.copies;
    }

    public int getAvailableCopies() {


    }

    public void withdrawCopy() {
        int found = 0;
        for (Book b : listOfBooks) {
            if (b.getTitle().equals(title)) {
                if (found == 0) {
                    found = 1;
                }
                if (!b.isBorrowed()) ;
                {
                    b.borrowed = true;
                    found = 2;
                    break;
                    throw new IllegalStateException("Nothing to Withdraw");
                }
            }
        }

    }

    public String returnCopy() {
        boolean found = false;
        for (Book book : listOfBooks) {
            if (book.getTitle().equals(title) && book.isBorrowed()) {
                book.returned();
                found = true;
                break;
                throw new IllegalStateException("Cannot Return");
            }
        }
    }
}

Can anyone tell me what I'm doing wrong?

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Dafzr308
  • 13
  • 6

1 Answers1

0

listOfBooks is a private variable in the Library class, and therefore cannot be accessed in the Book class. You should probably make an accessor method such as

public static List<Book> getListOfBooks() {
    return listOfBooks;
}

in your Library class, then you can access the library's list of Books in other classes by calling Library.getListOfBooks(). In this case, you will also need to change the declaration of listOfBooks to static. You can also set it up so that listOfBooks is not static, but then you will have to attach a Library object to the Book object in order to access this listOfBooks variable.

Here is more information about this pattern: https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html

In the spirit of encapsulation, it is common to make fields private. This means that they can only be directly accessed from the Bicycle class. We still need access to these values, however. This can be done indirectly by adding public methods that obtain the field values for us:

Tennyson H
  • 1,705
  • 15
  • 28