1. 设计背景
- 随着社会的发展和科技的进步,图书馆的规模和藏书量都在不断扩大,图书的管理和维护变得越来越复杂。传统的图书管理方式已经无法满足现代图书馆的需求,因此需要开发一种高效、便捷的图书管理系统来提高图书管理效率和读者的借阅体验。
- 在这种背景下,我们设计了Java图书管理系统。该系统采用Java语言开发,具有跨平台、可扩展、可维护等优点,可以满足现代图书馆的管理需求。该系统分为管理员用户和普通用户,主要实现了图书的查询、新增、删除、借阅、归还等功能。
- 此外,图书管理系统是 java 知识学习的运用,运用了到了类和对象,构造方法,方法调用,数组,继承,多态,封装,接口,抽象类等知识。通过这个图书管理系统的练习,能更好的帮助我们更好的巩固对前面学习知识。
2. 需求分析
这个图书系统在登陆页面分为管理员用户和普通用户,管理员用户和普通用户的实现页面不一样
- 管理员用户需要实现的功能有查找图书, 新增图书,删除图书,显示图书,退出系统。
- 普通用户需要实现的功能有查找图书, 借阅图书,归还图书 退出系统。
3. 设计思路
回顾面向对象的核心:
- 找到对象
- 创建对象
- 使用对象
- 首先我们需要找出图书馆里系统里的所有对象:
在图书管理系统在的对象有书,用户(普通用户和管理员用户),其中还有存放书本的书架。- 普通用户和管理员用户所展示的页面有所不同,利用继承和多态实现这一思路。
- 我们将普通用户和管理员用户的操作单独封装起来设计成一个类,并且定义一个接口来接收方法,接口达到了统一性。
4. 实现
我们要完成图书系统,可以先搭框架,再完善细节。其中使用了三个包,book包;operation 包和 user 包。
4.1 book包
book包中包含Book类和BookList类(书架)
4.1.1 Book类
Book类针对书籍,定义了有关书的属性,作者、价格、名字、类别,状态(判断是否被借出),通过构造方法,获取 get 和 set 方法,重写了toString 函数。
代码实现:
package book;
public class Book {
private String name;
private String author;
private int price;
private String type;
private boolean isBorrowed;
//构造方法
public Book(String name, String author, int price,String type) {
this.name = name;
this.author = author;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
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 + '\'' +
((isBorrowed == true) ? " 已经借出" : " 未被借出") +
//", isBorrowed=" + isBorrowed +
'}';
}
}
4.1.2 BookList类(书架)
这么多本书怎么放到书架上?其中数组可以组织数据,所以我们在书架类里会使用一个数组来存放书本
代码实现:
package book;
public class BookList {
private Book[] books;
private int usedSize;//记录当前书架上 实际存放书的数量
private static final int DEFAULT_CAPACITY = 10;
public BookList(){
this.books = new Book[DEFAULT_CAPACITY];//当前书架能存放10本书
//放好书!
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",23,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",28,"小说");
this.usedSize = 3;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public Book getBook(int pos){
return books[pos];
}
public void setBooks(int pos,Book book){
books[pos] = book;
}
public Book[] getBooks(