一、简介
实现此项目的目的是巩固并理解前面的知识点:类,抽象类,封装,继承,多态,接口等
二、核心需求
管理端
查阅书籍
增加书籍
删除书籍
打印书籍列表
退出系统
用户端
查询书籍
借阅书籍
归还书籍
打印书籍列表
退出系统
三、类的设计
1. 创建图书类
图书类中包含图书的名称,价格,类型,作者和是否被借出等信息,并生成构造方法,Getter()和Setter()方法,toString方法(注意成员变量应该尽可能使用private关键字修饰)
public class Book {
private String name;
private double price;
private String type;
private String author;
private boolean isBorrowed;
public Book(String name, double price, String type, String author) {
this.name = name;
this.price = price;
this.type = type;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", type='" + type + '\'' +
", author='" + author + '\'' +
", 状态:" +((isBorrowed) ? "已借出":"未借出")+
'}';
}
}
2. 创建图书列表类
图书列表类用于存放图书,我们可以先在列表中初始化几本书以方便后续测试
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList(){
books[0] = new Book("三国演义",19,"小说","罗贯中");
books[1] = new Book("水浒传",29,"小说","施耐庵");
books[2] = new Book("西游记",39,"小说","吴承恩");
usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos){
return