这个系统使用的是java的基础语法(没有使用数据库),实现图书管理系统基础的查询,增加,删除,借阅,归还,打印,退出功能。这个小项目作为我java基础语法的综合运用,主要是为了建立面向对象编程的思想,培养编程习惯,如果有错误或者更好的实现方法,欢迎大家批评指教。
图书管理系统的结构
编写的基本过程
- 创建三个包用来装 图书类,用户类,操作类
- 在opera包中创建IOperation接口,在user包中创建user类作为父类
- 创建main类,把各功能模块串联起来。
- (以上步骤把整个系统框架搭好了)
- 开始一步一步根据main编写的操作步骤,实现具体的功能模块代码。
具体的代码实现
Book包:
- 定义一个book类,用来定义一本书,实现构造方法,get/set,toString等方法。
-
//图书类
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 getBorrowed() {
return isBorrowed;
}
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) ? "已经被借出" : "未被借出") +
'}';
}
}
- 定义一个BookList类,当做书架,存储图书信息。
-
//书架
public class BookList {
//暂时用数组做一个数据库
private Book[] books = new Book[10];
//通过构造方法创建初始的三本书
public BookList() {
books[0] = new Book("三国演义","罗贯中",80,"小说");
books[1] = new Book("西游记","吴承恩",83,"小说");
books[2] = new Book("红楼梦","曹雪芹",82,"小说");
this.usedSize = 3 ;
}
//记录当前books数组当中有多少本书
private int usedSize;
public int getUsedSize() {
return usedSize;
}
//为封装起来的Book[]数组提供get和set方法,方便其他地方使用这个数组
public Book getBook(int pos) {
return this.books[pos];
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
public void setBook(Book book) {
this.books[usedSize] = book;
}
public void setBook(int pos, Book book) {
this.books[pos] = book;
}
}
user包
- 创建user类,作为父类,抽象出用户类共有的属性。
-
//把用户,管理员等的共性提出来,后面用继承
//建一个抽象类
public abstract class User {
protected String name;
//定义一个接口数组来存储普通用户和管理员用户对应的操作
//此处只是一个变量名,在NomaIUser和AdminUser里去开辟空间
protected IOPeration[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu() ;
//提供一个方法,去调用ioperations数组的对应的choice对应的work方法
public void doWork(int choice, BookList bookList){
this.ioPerations[choice].work(bookList);
}
}
- 创建管理员用户类(adminUer),实现增加图书,删除图书,查找图书,打印图书,退出系统功能。
//管理员
public class AdminUser extends User{
public AdminUser(String name) {
super(name);
//引用,初始化,然后开辟空间,动态空间
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new AddOperation(),
new DelOperation(),
new ShowOperation(),
};
}
// 菜单部分
public int menu(){
System.out.println("****************************");
System.out.println("hellow "+name+" 欢迎来到图书管理系统");
System.out.println("1.查找图书!");
System.out.println("2.新增图书!");
System.out.println("3.删除图书!");
System.out.println("4.显示图书!");
System.out.println("0.退出系统!");
System.out.println("****************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt();
return choice;
}
}
- 创建普通用户类(normaUser),实现借阅图书,归还图书,查询图书,打印图书,退出系统功能。
//普通用户
public class NomalUser extends User{
public NomalUser(String name) {
super(name);
//引用,然后开辟空间,动态空间,调用的时候初始化下面的对象
this.ioPerations = new IOPeration[]{
new ExitOperation(),
new FindOperation(),
new BrrowOperation(),
new ReturnOperation()
};
}
// 菜单部分
public int menu() {
System.out.println("****************************");
System.out.println("hellow" + name + "欢迎来到图书管理系统");
System.out.println("1.查找图书!");
System.out.println("2.借阅书籍!" );
System.out.println("3.归还图书!");
System.out.println("0.退出系统!");
System.out.println("****************************");
System.out.println("请输入你的操作:");
Scanner scanner = new Scanner((System.in));
int choice = scanner .nextInt();
return choice;
}
}
opera包
- 创建IOperation接口,用于后面的各功能类之间的连接。
-
public interface IOPeration {
void work(BookList bookList);
}
- 各功能模块的实现
- 添加图书
-
//添加图书
//实现IOPeration接口
public class AddOperation implements IOPeration{
//重写IOPeration接口
@Override
public void work(BookList bookList) {
System.out.println("新增图书!");
Scanner scanner = new Scanner(System.in);
System.out.println("输入书名:");
String name = scanner.nextLine();
System.out.println("输入作者:");
String author = scanner.nextLine();
System.out.println("输入类型:");
String type =scanner.nextLine();
System.out.println("输入价格:");
int price =scanner.nextInt();
//以上输入的内容构成一个book的对象
Book book = new Book(name,author,price,type);
//遍历数组
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize ; i++) {
Book tmp =bookList.getBook(i);
//遍历,对比是否有所输入的图书名字
if (tmp.getName().equals(name)){
System.out.println("已经存在这本书了,不能再添加了!");
return;
}
}
//把新的图书数据放到数组的后面
bookList.setBook(book);
//修改usedSize
bookList.setUsedSize(currentSize+1);
}
}
- 删除图书
public class DelOperation implements IOPeration {
//重写IOPeration接口
@Override
public void work(BookList bookList) {
System.out.println("删除图书!");
System.out.println("请输入需要删除的图书名字:");
Scanner scanner = new Scanner(System.in);
System.out.println("输入书名:");
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
int index = -1;
for (int i = 0; i < currentSize ; i++) {
Book tmp = bookList.getBook(i);
//遍历,对比是否有所输入的图书名字
if (tmp.getName().equals(name)) {
//用index去存找到的目标的下标
index = i;
break;
}
}
for (int j = index; j < currentSize-1 ; j++) {
Book book = bookList.getBook(j + 1);
bookList.setBook(j, book);
}
bookList.setUsedSize(currentSize-1);
//因为删除的是对象,所以需要把最后一个数组元素置为null
bookList.setBook(currentSize-1,null);
System.out.println("删除成功!");
}
}
- 查询图书
-
public class FindOperation implements IOPeration {
//重写IOPeration接口
@Override
public void work(BookList bookList) {
System.out.println("查找图书!");
System.out.println("请输入书名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize ; i++) {
Book book =bookList.getBook(i);
//遍历,对比是否有所输入的图书名字
if (book.getName().equals(name)){
System.out.println("找到了这本书:");
System.out.println(book);
return;
}
}
System.out.println("很抱歉,没有找到!");
}
}
- 借阅图书
-
public class BrrowOperation implements IOPeration {
//重写IOPeration接口
@Override
public void work(BookList bookList) {
System.out.println("借出图书!");
System.out.println("请输入你要借阅的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize =bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName() .equals(name)&&
book.isBorrowed () == false){
book.setBorrowed(true);
System.out.println("借阅成功");
return ;
}
}
}
}
- 归还图书
-
public class ReturnOperation implements IOPeration {
//重写IOPeration接口
@Override
public void work(BookList bookList) {
System.out.println("归还图书!");
System.out.println("请输入你要归还的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize =bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
if (book.getName() .equals(name)&&
book.isBorrowed () == true){
book.setBorrowed(false);
System.out.println("归还成功");
return;
}
}
System.out.println("没有你要归还的书!");
}
}
- 打印图书
-
public class ShowOperation implements IOPeration {
//重写IOPeration接口
@Override
public void work(BookList bookList) {
System.out.println("打印所有图书!");
//遍历数组获取所有图书信息
int currentSize = bookList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = bookList.getBook(i);
System.out.println(book);
}
}
}
- 退出系统
-
public class ExitOperation implements IOPeration {
//重写IOPeration接口
@Override
public void work(BookList bookList) {
System.out.println("退出系统!");
System.exit(0);//直接调用这个系统方法
}
}
main
public class Main {
private static BookList bookList;
public static User login() {
System.out.println("请输入你的姓名:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入你的身份:1-->管理员,0-->普通用户");
int choice = scanner.nextInt();
//判断进入那个操作
if (choice == 1 ) {
return new AdminUser(name);
} else {
return new NomalUser(name);
}
}
public static void main(String[] args){
BookList bookList = new BookList();
User user = login();
//调用login方法后打印出菜单,然后输入要执行的操作的代码
//然后接收代码
//用一个循环来重复显示
while (true) {
int choice = user.menu();
//根据choice和user来确定到底调用哪一个对象的哪个操作
user.doWork(choice,bookList);
}
}
}
归纳反思
这个图书管理系统的练习难度不大,实现的都是基本功能,做这个小项目的目的是为了建立对面向对象程序编写过程的理解,增强对类,继承,多态,抽象类等面型对象的运用和理解,本次项目中也出现了一个小但是很难受的问题,不小心把boolean写成了Boolean(包装类),忽略了这个知识点导致调试一致出现空指针异常的bug,调试了好久才发现,就很难受。