图书馆还书系统:如何基于现有代码创建书籍状态展示方法
Hey Eric, let's break this down step by step. Since you mentioned you have an ArrayList for books and a Status enum, I'll start by assuming a common structure for your Book class and Status enum (feel free to adjust if yours is different), then show you how to build practical status display methods.
First, let's define a typical Status enum (I'll add a human-readable description to make output friendlier—you can skip this if your enum is simpler):
public enum Status { AVAILABLE("Ready for checkout"), BORROWED("Currently checked out"), OVERDUE("Overdue - please return promptly"), DAMAGED("Unavailable due to damage"); private final String description; Status(String description) { this.description = description; } // Getter for the description public String getDescription() { return description; } }
Next, your Book class should hold the status along with other book details (like title/ISBN):
public class Book { private String title; private String isbn; private Status status; // Constructor public Book(String title, String isbn, Status status) { this.title = title; this.isbn = isbn; this.status = status; } // Getters (you'll need these to access status) public String getTitle() { return title; } public String getIsbn() { return isbn; } public Status getStatus() { return status; } // Optional setter if you need to update status (e.g., when a book is returned) public void setStatus(Status status) { this.status = status; } }
Now let's build two useful status display methods—one for all books, and one for a specific book:
1. Display Status for All Books in the ArrayList
This method will loop through your entire book collection and print each book's details + status:
public class LibraryManager { // Print status for every book in the library public void displayAllBookStatuses(ArrayList<Book> books) { System.out.println("=== Full Library Book Status Report ==="); for (Book book : books) { System.out.printf("Title: %-20s | ISBN: %-13s | Status: %-10s - %s%n", book.getTitle(), book.getIsbn(), book.getStatus().name(), // Gets the enum's constant name (e.g., BORROWED) book.getStatus().getDescription()); // Gets the friendly description } } }
2. Display Status for a Single Book (by ISBN)
If you need to look up a specific book's status (common in a return system), use this method with a stream to safely find the book:
public class LibraryManager { // ... previous code ... // Find and print status for a book by its ISBN public void displaySingleBookStatus(ArrayList<Book> books, String targetIsbn) { // Use Optional to avoid null pointer exceptions Optional<Book> foundBook = books.stream() .filter(book -> book.getIsbn().equals(targetIsbn)) .findFirst(); if (foundBook.isPresent()) { Book book = foundBook.get(); System.out.printf("\nBook Found:%nTitle: %s%nISBN: %s%nStatus: %s - %s%n", book.getTitle(), book.getIsbn(), book.getStatus().name(), book.getStatus().getDescription()); } else { System.out.printf("\nNo book found with ISBN: %s%n", targetIsbn); } } }
How to Use These Methods
Here's a quick example to test everything:
public class Main { public static void main(String[] args) { // Populate your book list ArrayList<Book> libraryBooks = new ArrayList<>(); libraryBooks.add(new Book("The Great Gatsby", "9780743273565", Status.AVAILABLE)); libraryBooks.add(new Book("1984", "9780451524935", Status.BORROWED)); libraryBooks.add(new Book("To Kill a Mockingbird", "9780061120084", Status.OVERDUE)); LibraryManager manager = new LibraryManager(); // Show all statuses manager.displayAllBookStatuses(libraryBooks); // Look up a single book manager.displaySingleBookStatus(libraryBooks, "9780451524935"); } }
Quick Notes
- If your
Statusenum doesn't have a description field, just usebook.getStatus().toString()instead ofgetDescription(). - For a return system, you'd likely call
setStatus(Status.AVAILABLE)when a book is returned, then use these display methods to confirm the update. - If you're working with a UI instead of console output, the logic stays the same—just replace
System.outcalls with UI rendering code.
内容的提问来源于stack exchange,提问作者Eric Graham

