0

I'm new to Java so sorry for all the mistakes!

Im creating a Library program consisting of 4 classes: Library, Book, BookInterface & Patron.

In the Book class I have a method that prints out all the books in the library and their status' (in or out). Instead I keep getting something like this:

 Great Gatsby: null                                            

 Withering Heights: null

Does it have something to do with the setStatus() method? Every time a user adds a new book, it creates a new Book instance and then I do setStatus("IN"). So how come it is not saving and instead printing out null?

Thank you very much for the help!!

Book class:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Book implements BookInterface
{

    Scanner input = new Scanner(System.in);
    static ArrayList <String> UserList = new ArrayList<String>(); 
    static ArrayList <String> BookList = new ArrayList <String> (); //display just titles// use when checking out books
    static ArrayList <String> OrigBookList = new ArrayList <String> (); //keep track of all titles ever entered


    public String title; 
    public String author;
    public String book;
    public boolean checkIn;

    private String status;
    private String borrower; 

    public Book(String t, String a)
    {
        title = t; 
        author = a; 
    }

    //constructor create new book
    public Book(String newTitle)
    {
        title = newTitle;   
    }


    public String toString()
    {
        return title + " " + author; 
    }

    public String getTitle() 
    {
        return title;
    }

    public void setTitle(String title) 
    {
        this.title = title;
    }

    public String getAuthor() 
    {
        return author;
    }

    public void setAuthor(String author) 
    {
        this.author = author;
    }

    public String getStatus(String book)
    {
        return status; 
    }

    public void setStatus(String status)    
    {
        this.status = status; 
    }

    public void setBorrower(String borrower)
    {
        this.borrower = borrower; 
    }

    public String getBorrower(String checkPatron)
    {
        return borrower; 
    }

    public String getBook(String checkPatron)
    {
        return book; 
    }

    public void setBook(String bookCheckOut)
    {
        this.book = bookCheckOut;  
    }

    public void addBook()
    {
        Scanner input = new Scanner(System.in);
        Scanner inputread = new Scanner(System.in);
        System.out.print("Enter book title: ");
        String title1 = inputread.nextLine();

        Scanner input1 = new Scanner(System.in);
        System.out.print("Enter book author: ");
        String author1 = inputread.next(); 

        Book fullBook = new Book(title1, author1);  //create constructor w/ title & author
        Book book1 = new Book(title1);              //constructor w/ just title to be used to display all books
        OrigBookList.add(title1);
        book1.setStatus("IN");  
        System.out.println("-----------------------------------------------");
        System.out.println("-----" + title1 + " is now in the library!-----");
        System.out.println("-----------------------------------------------");
    }

    public void editBook()
    {
        Scanner inputread = new Scanner(System.in);
        System.out.println("Enter original book title: ");
        String origTitle = inputread.nextLine(); 
        System.out.println("Enter edited book title: ");
        String editedTitle = inputread.nextLine();
        Collections.replaceAll(Book.UserList, origTitle, editedTitle);
        System.out.println("------------------------------------------------------");
        System.out.println(origTitle + " has been changed to " + editedTitle + "!");
        System.out.println("------------------------------------------------------");

    }

    public void libraryInventory()
    {
        System.out.println("------------------ Library Inventory: ---------------");
        for(int i =0; i<= OrigBookList.size()-1; i++)
        {
            //Book Title: checked in/out
            System.out.println(OrigBookList.get(i) + ":" + getStatus(OrigBookList.get(i)));         
        }
        System.out.println("-----------------------------------------------------");
    }

}
3
  • When printing which Arraylist gives you null? Commented Dec 13, 2015 at 10:30
  • You should have separate classes, your "book" is some kind of strange beast in the posted code Commented Dec 13, 2015 at 10:32
  • @user3437460 the OrigBookList in libraryInventory() Commented Dec 13, 2015 at 10:41

2 Answers 2

3

getStatus(OrigBookList.get(i)) ignores the parameter you pass to it and just returns the status of the Book for which you called the libraryInventory method. Obviously, that Book instance doesn't have the status field initialized, but even if it did, it will give you the status of just one Book.

Instead of having a static list of book titles (static ArrayList <String>), perhaps you should maintain a list of the books themselves (static ArrayList <Book>), or even better, put that list in a separate class (you can call it Library).

Methods such as libraryInventory shouldn't be in the Book class (and if you insist on keeping them in the Book class, make them static, since they don't refer to a single Book instance).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi! thank you for your advice it really helps clear things up. I just changed the ArrayList from <String> to <Book> and moved it outside the Book class. Since the status' of each book are not exactly saving, could I possibly create a new ArrayList that saves both the title of the book and its status and use that in the libraryInventory ?
@kayylai The status is a member of the Book class, so if it's not saved, this means you are not setting its value in the Book objects you are adding to the list. You don't need another list.
0

Your whole program seems to be running inside an instance of the class Book. In it, you are making and discarding new instances of Book, called fullBook and book1, and for fullBook you set its status. When you call getStatus on the main Book in your program, it just returns its own status, which was never set to anything.

If you want to save a sequence of instances of Book, you need to put the instances somewhere, not just instantiate them and then add the title to a list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.