简体   繁体   English

循环ArrayList打印出null

[英]Loop for ArrayList printing out null

I'm new to Java so sorry for all the mistakes! 我是Java新手,对于所有错误,我们深表歉意!

Im creating a Library program consisting of 4 classes: Library, Book, BookInterface & Patron. 我创建了一个由4个类组成的Library程序: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). 在Book类中,我有一种方法可以打印出图书馆中的所有书籍及其状态(输入或输出)。 Instead I keep getting something like this: 相反,我不断得到这样的东西:

 Great Gatsby: null                                            

 Withering Heights: null

Does it have something to do with the setStatus() method? 它与setStatus()方法有关吗? Every time a user adds a new book, it creates a new Book instance and then I do setStatus("IN"). 每次用户添加新书时,它都会创建一个新的Book实例,然后执行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("-----------------------------------------------------");
    }

}

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. getStatus(OrigBookList.get(i))忽略传递给它的参数,仅返回您为其调用libraryInventory方法的Book的状态。 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. 显然,该Book实例没有初始化状态字段,但是即使初始化了,它也只会为您提供一本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 ). 与其拥有一个静态的书名列表( static ArrayList <String> ),不如您应该维护一个图书本身的列表( static ArrayList <Book> ),甚至更好的是,将该列表放在一个单独的类中(可以调用它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). 诸如libraryInventory Book类的方法不应该在Book类中(并且如果您坚持将它们保留在Book类中,请使它们成为静态的,因为它们没有引用单个Book实例)。

Your whole program seems to be running inside an instance of the class Book . 您的整个程序似乎都在Book类的实例中运行。 In it, you are making and discarding new instances of Book , called fullBook and book1 , and for fullBook you set its status. 在这里面,你正在和丢弃的新实例Book ,叫fullBookbook1 ,并为fullBook设置其状态。 When you call getStatus on the main Book in your program, it just returns its own status, which was never set to anything. 当您在程序的主Book上调用getStatus时,它只会返回自己的状态,该状态从未设置为任何状态。

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. 如果要保存Book实例的序列,则需要将实例放置在某处,而不仅仅是实例化它们,然后将标题添加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM