创建方法以显示对象的状态

时间:2018-03-14 19:20:37

标签: java

因此标题没有多大意义,我道歉,所以希望这会让事情变得清晰起来。我正在创建一个图书馆检查系统。我有一个书籍的ArrayList,我需要创建方法来显示书籍的状态。我的问题是,我不确定如何用我已经给出的方法创建这些方法。这是代码:     import java.util。*;

public class Catalog{


private ArrayList<Book> books;
    Book b1 = new Book("Harry Potter and the Goblet of Fire", "J.K. Rowling", "0439139600", 2000, Status.LOST);
    Book b2 = new Book("The Hunger Games", "Suzanne Collins", "0439023483", 2008, Status.ONLOAN);
    Book b3 = new Book("The Help", "Kathryn Stockett", "9780399155345", 2009, Status.INLIBRARY);
    Book b4 = new Book("The Road", "Cormac McCarthy", "9780307265432", 2006, Status.INLIBRARY);
    Book b5 = new Book("The Lightning Thief", "Rick Riordan", "0786838655", 2005, Status.ONLOAN);
    Book b6 = new Book("The Da Vinci Code", "Dan Brown", "0307277674", 2003, Status.LOST);
    Book b7 = new Book("Life of Pi", "Yann Martel", "0770430074", 2001, Status.ONLOAN);
    Book b8 = new Book("The Book Thief", "Marcus Zusak", "9780375831003", 2005, Status.INLIBRARY);
    Book b9 = new Book("The Time Traveler's Wife", "Audrey Niffenegger", "9781939126016", 2003, Status.LOST);


//Empty-argument constructor that initializes the books ArrayList.
public Catalog() {
    ArrayList<Book> books = new ArrayList<Book>();

}//end empty constructor


//Method for adding books to the array List
public ArrayList<Book> addBook(Book b) {
    books.add(b); //not sure if this is done correctly
    return books; 

}//end addBook

//Method that should return a list of the titles of all books
//and their status(LOST,INLIBRARY,ONLOAN).
public ArrayList<Book> showAllBooks() {


}//end showAllBooks

//Method that should return an list of the titles in the catalog that are marked LOST
public String showLost() {


}
}//end class

除此之外,我还创建了一个名为Status的枚举类,它包含用于指示书籍状态的常量。这是代码:

   /**
 * Data Type to hold constants to
 * indicate status of Book Object in a Library.
 * @author 
 * @version 1.0
 * Programming Project 2 Starter
 * 2/10/17
 */
public enum Status {

LOST,
INLIBRARY,
ONLOAN

}//end enum

图书课程:        公共课书{

private Status status;
private String title, authors, isbn;
private int yearPublished;
private Status status;
private String publisherName;


public Book() {

    title = null;
    authors = null;
    isbn = null;
    yearPublished = 0;
    status = null;
    publisherName = null;

}//end book constructor

public Book(String isbn, String title, String authors, int yearPublished, Status status) {
    this.isbn = isbn;
    this.title = title;
    this.authors = authors;
    this.yearPublished = yearPublished;
    this.status = status;


}//end book constructor

public Status getStatus() {
    return this.status;
   }//end getStatus

1 个答案:

答案 0 :(得分:1)

在Book类中,您需要为Status类型设置getter方法。在您想要获取状态的地方调用此方法。

示例 -

buildscript {
    repositories {
        google()
        jcenter()
    }