類 `BookData` 包含靜態 `ArrayList` 字段 `book`、`customer`,及靜態方法 `bkdata()` 初始化數據。需求:在 `main` 方法中調用 `bkdata()` 並輸出書本信息。
public class BookData {
private static ArrayList book = new ArrayList();
private static ArrayList customer = new ArrayList();
public static void bkdata() {
BookDaoImp bookDaoImp = new BookDaoImp();
bookDaoImp.create("計算機類", 300);
// ... 更多創建
// 缺少:將創建的對象加入 book 列表
// 缺少:輸出邏輯
}
// getter/setter 省略
}
import java.util.ArrayList;
import java.util.List;
public class BookData {
private static List bookList = new ArrayList<>();
private static List customerList = new ArrayList<>();
public static void bkdata() {
// 創建書籍種類並加入列表
BookDaoImp b1 = new BookDaoImp(); b1.create("計算機類", 300); bookList.add(b1);
BookDaoImp b2 = new BookDaoImp(); b2.create("文史類", 300); bookList.add(b2);
BookDaoImp b3 = new BookDaoImp(); b3.create("小說類", 300); bookList.add(b3);
// 創建會員信息並加入列表
CustomerDaoImp c1 = new CustomerDaoImp(); c1.create("張三", "vip會員"); customerList.add(c1);
CustomerDaoImp c2 = new CustomerDaoImp(); c2.create("李四", "會員"); customerList.add(c2);
CustomerDaoImp c3 = new CustomerDaoImp(); c3.create("王五", "普通"); customerList.add(c3);
// 輸出書籍信息
System.out.println("=== 書籍目錄 ===");
for (BookDaoImp bk : bookList) {
System.out.println("名稱: " + bk.getName() + ", 庫存: " + bk.getTotal());
}
// 輸出會員信息
System.out.println("\n=== 會員列表 ===");
for (CustomerDaoImp c : customerList) {
System.out.println("姓名: " + c.getName() + ", 類型: " + c.getType());
}
}
public static void main(String[] args) {
bkdata();
}
}
// 簡化的 DAO 實現類
class BookDaoImp {
private String name;
private int total;
public void create(String name, int total) { this.name = name; this.total = total; }
public String getName() { return name; }
public int getTotal() { return total; }
}
class CustomerDaoImp {
private String name;
private String type;
public void create(String name, String type) { this.name = name; this.type = type; }
public String getName() { return name; }
public String getType() { return type; }
}
最佳回答提供了完整可運行的修正代碼,補全了缺失的 `add` 操作、輸出循環、DAO 類定義與 `main` 入口,直接解決了「如何在 main 裡輸出 bkdata」的問題。