图书管理系统,相信大家都不陌生。借书还书等等操作...那么如何用Java来实现呢?
首先,我们从中抽取出一个个对象。
- 管理员的操作:
- 查看某个书籍、新增书籍、删除书籍、打印书籍列表、退出系统。
- 普通用户的操作:
- 查看某个书籍的信息、借阅书籍、归还书籍、退出系统。
- 当然,除了上面这些彩色标注的字样,最重要的还要实现书、书籍列表。
Book
- 属性:书名、作者、价格、类型、是否被借(为了封装尽量使用private)
- 方法:构造方法(用来初始化)、获取书名、获取被借状态、设置被借状态、toString
public class Book {
private String name;
private String author;
private double price;
private String type;
private boolean isBorrowed=false;
//构造方法,创建实例时初始化变量
public Book(String name, String author, double price, String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public boolean isBorrowed() {
return isBorrowed;
}
public void setBorrowed(boolean borrowed) {
isBorrowed = borrowed;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
'}';
}
}
BookList
- 属性:书籍列表的长度、列表的最大长度、Book类型的数组
- 方法:构造方法、获取列表长度、设置列表长度、得到指定下标的书、设置指定下标的书
//书籍列表
public class BookList {
private int size;
private int capacity=100;
Book[] books=new Book[capacity];
public BookList() {
books[0]=new Book("西游记","吴承恩",50,"古典文学");
books[1]=new Book("三国演义","罗贯中",60,"古典文学");
books[2]=new Book("从你的全世界路过","张嘉佳",45,"青春文学");
size=3;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Book getBooks(int index) {
return books[index];
}
public void setBooks(int index,Bo