设计一个基于控制台的购书系统。该系统主要有以下类完成 (1) 设计一个图书类(Book),该类处于shopping.book包中,该类具有以下成员变量和成员方法。 成员变量:图书编号
时间: 2024-12-21 17:27:13 浏览: 9
设计一个基于控制台的购书系统,我们可以按照以下步骤来构建所需的类:
**Book 类(Shopping.Book 包中)**:
```java
// Book.java
package shopping.book;
public class Book {
private String bookID; // 图书编号
private String title; // 图书标题
private String author; // 作者
private double price; // 价格
private int stock; // 库存量
// 构造函数
public Book(String bookID, String title, String author, double price, int stock) {
this.bookID = bookID;
this.title = title;
this.author = author;
this.price = price;
this.stock = stock;
}
// 获取和设置方法
public String getBookID() { return bookID; }
public void setBookID(String bookID) { this.bookID = bookID; }
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 double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public int getStock() { return stock; }
public void setStock(int stock) { this.stock = stock; }
// 其他方法,如检查库存、销售等操作
public boolean isAvailable() { return stock > 0; }
public void sell() {
if (isAvailable()) {
stock--;
} else {
System.out.println("这本书已售罄!");
}
}
}
阅读全文