gpt4 book ai didi

java - 创建方法来显示对象的状态

转载 作者:行者123 更新时间:2023-12-02 11:31:18 25 4
gpt4 key购买 nike

所以这个标题没有多大意义,我很抱歉,所以希望这能澄清问题。我正在创建一个图书馆 checkin 系统。我有一个书籍的 ArrayList,我需要创建方法来显示书籍的状态。我的问题是,我不确定如何用我已经得到的东西创建这些方法。这是代码: 导入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

最佳答案

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

示例 -

private Status status; 

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

关于java - 创建方法来显示对象的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49286098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com