我是Java的新手,对所有的错误感到抱歉!
我创建了一个由4个类组成的库程序:Library,Book,BookInterface&顾客。
在Book类中,我有一个方法可以打印出库中的所有书籍及其状态(输入或输出)。相反,我不断得到这样的东西:
Great Gatsby: null
Withering Heights: null
是否与setStatus()方法有关? 每次用户添加新书时,它都会创建一个新的Book实例,然后我会设置setStatus(“IN”)。那么为什么它不是保存而是打印出null?
非常感谢您的帮助!!
图书课程:
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("-----------------------------------------------------");
}
}
答案 0 :(得分:3)
getStatus(OrigBookList.get(i))
会忽略您传递给它的参数,只返回您调用Book
方法的libraryInventory
的状态。显然,Book
实例没有初始化状态字段,但即使它已经初始化,它也会给你一个Book的状态。
您可能应该维护一个书籍清单(static ArrayList <String>
),或者甚至更好地将该列表放在一个单独的类中(而不是拥有书名的静态列表(static ArrayList <Book>
)(你可以称之为Library
)。
libraryInventory
之类的方法不应该在Book
类中(如果你坚持将它们保存在Book
类中,请将它们设为静态,因为它们没有引用到单个Book
实例。)
答案 1 :(得分:0)
您的整个程序似乎在类Book
的实例中运行。在其中,您正在制作并放弃Book
的新实例,名为fullBook
和book1
,而fullBook
则设置其状态。当您在程序中的主getStatus
上调用Book
时,它只返回自己的状态,该状态从未设置为任何内容。
如果要保存Book
的一系列实例,则需要将实例放在某处,而不是仅实例化它们,然后将标题添加到列表中。