Java实现图书管理系统
学习了前六篇的SE
语法,我们现在要用它们实现一个简单的图书管理系统项目,深入了解各个知识点的应用场景。我们将整个过程分为四大部分:
- 问题分析
- 框架搭建
- 业务实现
- 项目测试
其中,框架搭建最为重要且难以理解,涉及类、接口等之间的复杂关系。
问题分析
我们期望实现一个不同身份操作界面不同的图书管理系统。
而要使用面向对象的思想实现该项目,第一步肯定是找对象:
初步分析,我们的项目会包含以下对象:
书
:书的属性用户
:用户又分为普通用户和管理员
单一的书对象太过分散不好维护,所以我们决定添加一个新类书架
,书架中有若干的书对象,我们后期操作的对象就是书架。
(以下所有代码块的内容不一定是完成版,我们会在解决问题的过程中不断改进之前的代码)
【书、书架】
我们先实现书类
:
其中,我们用private
修饰书类的成员变量,尽可能体现Java的封装性,同时添加get
和set
方法;
图书系统少不了图书信息的打印,为了方便打印,我们选择重写toString
方法,注意boolean
类型的打印,它指示书的借阅情况,如果仅打印true
或false
,不利于用户理解,所以我们做出一些改动如下代码中可见:
//Book.java
package Book;
public class Book {
private String name;//书名
private String author;//作者
private double price;//价格
private String kind;//类型
private boolean borrow;//借阅情况
//构造方法
public Book(String name, String author, double price, String kind) {
this.name = name;
this.author = author;
this.price = price;
this.kind = kind;
}
//get、set方法
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public boolean isBorrow() {
return borrow;
}
public void setBorrow(boolean borrow) {
this.borrow = borrow;
}
//重写toString
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", kind='" + kind + '\'' +
" " + (borrow == true ? "已借阅" : "未借阅") +
'}';
}
}
接下来是书架
:
书架里有若干书,书架与书的关系是:A has B
的关系,所以我们考虑到使用组合;
仍然使用private
修饰成员变量,并给出get
和set
方法。
//BookList.java
package Book;
public class BookList {
private Book[] books = new Book[10];//组合
private int size;//书的数量
//构造方法初始化书架
public BookList() {
//初始给两本书
this.books[0] = new Book("三国演义", "罗贯中", 23, "小说");
this.books[1] = new Book("红楼梦", "曹雪芹", 23, "小说");
this.size = 2;
}
public Book getBook(int pos) {
return this.books[pos];
}
public void setBook(Book book) {
this.books[this.size] = book;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Book[] getBooks() {
return this.books;
}
}
【用户】
分析: 用户分为两种,管理员和普通用户,符合A is B
的关系,即普通用户或管理员是用户,所以用户部分可以实现继承关系,将两种身份的共性进行抽取,简化代码。
用户类
:
实际上,我们并不会实例化一个用户的对象,因为用户要指名身份,所以我们考虑将User类
设计成抽象类;
并将其成员变量尽可能地封装。
//User.java
package User;
import Book.BookList;
import ioperation.IOperation;
public abstract class User {
public String name;
//构造方法
public User(String name) {
this.name = name;
}
}
普通用户
和管理员
的代码类似,我们一并实现:
//AdminUser.java
package User;
import ioperation.*;
import java.util.Scanner;
public class AdminUser extends User {
//构造方法先利用super初始化父类成员
public AdminUser(String name) {
super(name);
}
}
//NormalUser.java
package User;
import ioperation.*;
import java.util.Scanner;
public class NormalUser extends User {
//构造方法先利用super初始化父类成员
public NormalUser(String name) {
super(name);
}
}
到这里,我们实现了几个基本的类,后续也会一一完善,问题分析告一段落,我们直接进入框架搭建模块,这里是项目的难点。
框架搭建
我们先模拟一下项目执行过程:
程序启动 ——> 用户输入信息(登录) ——> 程序根据用户身份展示不同的操作页面 ——> 用户输入操作 ——> 程序执行 ——> … …
——> 程序终止
基于上面的模拟过程,我们开始一步一步搭建框架:
在已有实现类的基础上,我们先实现登录的逻辑,自定义方法名:login
这部分是我们框架的开始,是用户最先看到的画面,我们期望登陆方法能够:
- 实现用户选择身份的交互
- 返回可操作的对象:返回值有两种情况,而返回的对象的类共同继承了
User类
,所以我们选择将方法返回值设置成User类
,这里即体现了向上转型
public static User login() {
System.out.println("请输入您的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入您的身份:1. 管理员 2. 普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
我们拿到了用户身份,接下来我们要据此给出相应的菜单:
不同的身份,打印不同的菜单,符合多态的思想:
我们在父类中定义抽象方法menu
,两个子类重写menu
方法,然后用父类引用访问重写后的menu
方法,即可实现多态:
关于menu
方法,我们不只期望它可以打印菜单,我们期望它打印菜单的同时可以与用户交互,拿到用户的选择并返回等待进一步的操作。(菜单的打印自行发挥,菜单中的操作就是后面要实现的业务)
-
拓展父类
User
//User.java package User; import Book.BookList; import ioperation.IOperation; public abstract class User { public String name; public User(String name) { this.name = name; } //待重写的抽象方法 public abstract int menu(); }
-
子类
AdminUser
和NormalUser
重写menu
方法//AdminUser.java package User; import ioperation.*; import java.util.Scanner; public class AdminUser extends User { public AdminUser(String name) { super(name); } @Override public int menu() { System.out.println("欢迎" + this.name + "使用图书管理系统"); System.out.println("********管理员菜单********"); System.out.println("*************************"); System.out.println("******* 1.添加图书 *******"); System.out.println("******* 2.删除图书 *******"); System.out.println("******* 3.查找图书 *******"); System.out.println("******* 4.展示图书 *******"); System.out.println("******* 0.退出系统 *******"); System.out.println("*************************"); Scanner scanner = new Scanner(System.in); System.out.println("请输入您的操作:"); int choice = scanner.nextInt(); if(choice < 0 || choice > 4) { return -1; } return choice; } }
//NormalUser.java package User; import ioperation.*; import java.util.Scanner; public class NormalUser extends User { public NormalUser(String name) { super(name); } @Override public int menu() { System.out.println("欢迎" + this.name + "使用图书管理系统"); System.out.println("********普通用户菜单********"); System.out.println("*************************"); System.out.println("******* 1.查找图书 *******"); System.out.println("******* 2.借阅图书 *******"); System.out.println("******* 3.归还图书 *******"); System.out.println("******* 0.退出系统 *******"); System.out.println("*************************"); Scanner scanner = new Scanner(System.in); System.out.println("请输入您的操作:"); int choice = scanner.nextInt(); if(choice < 0 || choice > 3) { return -1; } return choice; } }
此处暂停,我们梳理一下我们已经实现的逻辑:
体现在Main类
中的main
方法中:
//Main.java
import Book.BookList;
import User.*;
import java.util.Scanner;
public class Main {
//登录方法
public static User login() {
System.out.println("请输入您的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入您的身份:1. 管理员 2. 普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
//main方法
public static void main(String[] args) {
//创建书架
BookList bookList = new BookList();
//登录并确定用户类型返回
User user = login();
while (true) {
//根据用户类型打印菜单并返回用户操作选择
int choice = user.menu();
if(choice < 0) {
System.out.println("输入非法,请重新输入!");
}else {
//根据用户选择执行相关操作
//......
}
}
}
}
最后一部分逻辑,也是最难理解的一部分:根据用户选择调用指定操作
我们当然可以选择在Main类
中一次性实现所有的操作方法并调用,但这样书写代码的缺陷是非逻辑代码大量集中,不方便维护,同时也没有利用Java的优点。
我们给出下面的解决方案:
- 创建一个接口,规范所有操作
- 创建若干操作类,实现规范接口
创建规范接口
//IOperation.java
package ioperation;
import Book.BookList;
public interface IOperation {
//工作方法
void work(BookList bookList);
}
根据菜单创建若干的操作类(这里仅给出逻辑代码,具体的操作在 业务实现 模块实现)
//AddOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("添加图书...");
}
}
//DelOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书...");
}
}
//FindOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查找图书...");
}
}
//ShowOperation.java
package ioperation;
import Book.*;
public class ShowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("展示图书...");
}
}
//BorrowOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书...");
}
}
//ReturnOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("归还图书...");
}
}
//ExitOperation.java
package ioperation;
import Book.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统...");
}
}
仅实现以上代码并不能将用户(login
方法返回值)、用户选择(menu
方法返回值)、操作(上代码)联系起来
我们采取这样的解决方案:
- 在父类
User
中添加成员变量,其类型是IOperation[]
- 分别使子类
AdminUser
和NormalUser
的构造方法可以初始化父类的IOperation[]
类型的成员,这样,我们在实例化子类对象时,就构建好了这样一个数组 - 在父类
User
中创建操作方法,父类引用调用此方法,并在此方法中再调用指定操作类中的重写方法 - 我们的项目的各种操作都是针对
BookList
(书架)的,所以我们的参数列表要大量出现BookList
类对象
完善后的父类User
//User.java
package User;
import Book.BookList;
import ioperation.IOperation;
public abstract class User {
public String name;
protected IOperation[] iOperations;//新添成员变量
public abstract int menu();
public User(String name) {
this.name = name;
}
//新添操作方法
public void doOperation(int choice, BookList bookList) {
this.iOperations[choice].work(bookList);
}
}
完善后的子类
//AdminUser.java
package User;
import ioperation.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
//根据管理员菜单进行初始化
this.iOperations = new IOperation[]{
new ExitOperation(),
new AddOperation(),
new DelOperation(),
new FindOperation(),
new ShowOperation(),
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name + "使用图书管理系统");
System.out.println("********管理员菜单********");
System.out.println("*************************");
System.out.println("******* 1.添加图书 *******");
System.out.println("******* 2.删除图书 *******");
System.out.println("******* 3.查找图书 *******");
System.out.println("******* 4.展示图书 *******");
System.out.println("******* 0.退出系统 *******");
System.out.println("*************************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的操作:");
int choice = scanner.nextInt();
if(choice < 0 || choice > 4) {
return -1;
}
return choice;
}
}
//NormalUser.java
package User;
import ioperation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
//根据普通用户菜单进行初始化
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name + "使用图书管理系统");
System.out.println("********普通用户菜单********");
System.out.println("*************************");
System.out.println("******* 1.查找图书 *******");
System.out.println("******* 2.借阅图书 *******");
System.out.println("******* 3.归还图书 *******");
System.out.println("******* 0.退出系统 *******");
System.out.println("*************************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的操作:");
int choice = scanner.nextInt();
if(choice < 0 || choice > 3) {
return -1;
}
return choice;
}
}
框架最后一步:完善main
方法:
//Main.java
import Book.BookList;
import User.*;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入您的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入您的身份:1. 管理员 2. 普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
//创建书架
BookList bookList = new BookList();
//登录并确定用户类型返回
User user = login();
while (true) {
//根据用户类型打印菜单并返回用户操作选择
int choice = user.menu();
if(choice < 0) {
System.out.println("输入非法,请重新输入!");
}else {
//根据用户选择执行相关操作
user.doOperation(choice, bookList);
}
}
}
}
业务实现
完成了框架搭建,业务实现就简单多了,业务实现仅需要将实现操作接口的操作类的重写方法完善即可。
【添加图书】
判断书架是否满 ——> 判断新书是否重复 ——> 添加
//AddOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("添加图书...");
//判断书架是否已满
int size = bookList.getSize();
if(size == 10) {
System.out.println("书架满了...");
return;
}
Book[] books = bookList.getBooks();
Scanner scanner = new Scanner(System.in);
//判断新书是否重复
System.out.println("请输入书名;");
String name = scanner.nextLine();
for (int i = 0; i < size; i++) {
if(name.equals(books[i].getName())) {
System.out.println("已存在这本书!");
return;
}
}
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入价格:");
int price = scanner.nextInt();
//这里要注意,要读走回车,否则类型kind接受的是回车
//或者让用户先输入类型,也可以解决这一问题
String tmp = scanner.nextLine();
System.out.println("请输入类型:");
String kind = scanner.nextLine();
Book book = new Book(name, author, price, kind);
bookList.setBook(book);
bookList.setSize(size + 1);
}
}
【删除图书】
判断书架是否为空 ——> 寻找指定图书 ——> 根据寻找结果进行相关操作
//DelOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书...");
//判断书架是否为空
int size = bookList.getSize();
if(size == 0) {
System.out.println("书架为空!");
return;
}
//输入信息
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要删除的书名:");
String name = scanner.nextLine();
//删除
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
books[i] = null;
for (int j = i; j < size - 1; j++) {
books[j] = books[j + 1];
}
bookList.setSize(size - 1);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有查询到指定的书!");
}
}
【查找图书】
输入信息 ——> 查找 ——> 成功则打印
//FindOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查找图书...");
int size = bookList.getSize();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要查询的书名:");
String name = scanner.nextLine();
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
System.out.println("查找成功,信息如下:");
System.out.println(books[i].toString());
return;
}
}
System.out.println("没有查询到指定的书!");
}
}
【展示图书】
判断书架是否为空 ——> 展示图书
//ShowOperation.java
package ioperation;
import Book.*;
public class ShowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("展示图书...");
int size = bookList.getSize();
if(size == 0) {
System.out.println("书架为空!");
return;
}
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
System.out.println(books[i].toString());
}
}
}
【借阅图书】
输入信息 ——> 查找图书 ——> 借阅成功或失败(借阅成功,将Book对象的borrow成员设置成true即可)
//BorrowOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书...");
int size = bookList.getSize();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要借阅的书名:");
String name = scanner.nextLine();
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
if(!books[i].isBorrow()) {
System.out.println("借阅成功!");
books[i].setBorrow(true);
return;
}else {
System.out.println("该书已被借阅!");
return;
}
}
}
System.out.println("没有查询到指定的书!");
}
}
【归还图书】
输入信息 ——> 检查借阅情况 ——> 归还或打印其他信息(归还成功,将Book对象的borrow成员设置成false即可)
//ReturnOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("归还图书...");
int size = bookList.getSize();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要归还的书名:");
String name = scanner.nextLine();
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
if(!books[i].isBorrow()) {
System.out.println("这本书还没有被借阅呢,无法归还,请检查是否输入错误!");
return;
}else {
System.out.println("归还成功!");
books[i].setBorrow(false);
return;
}
}
}
System.out.println("没有查询到指定的书!");
}
}
【退出系统】
//ExitOperation.java
package ioperation;
import Book.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统...");
System.exit(0);
}
}
项目测试
【登录测试】
【操作演示(部分)】
其他操作均可正常运行,大家下来可以自己实现一下。
代码演示
演示代码分布在多个包中:
Book
:包含书、书架对象ioperation
:包含操作接口以及实现操作接口的类User
:包含用户(父类)、管理员、普通用户类Main
(默认包):代码总逻辑,使用上三部分代码
Book
//Book.java
package Book;
public class Book {
private String name;
private String author;
private double price;
private String kind;
private boolean borrow;
public Book(String name, String author, double price, String kind) {
this.name = name;
this.author = author;
this.price = price;
this.kind = kind;
}
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public boolean isBorrow() {
return borrow;
}
public void setBorrow(boolean borrow) {
this.borrow = borrow;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", kind='" + kind + '\'' +
" " + (borrow == true ? "已借阅" : "未借阅") +
'}';
}
}
//BookList.java
package Book;
public class BookList {
private Book[] books = new Book[10];
private int size;
public BookList() {
//初始给两本书
this.books[0] = new Book("三国演义", "罗贯中", 23, "小说");
this.books[1] = new Book("红楼梦", "曹雪芹", 23, "小说");
this.size = 2;
}
public Book getBook(int pos) {
return this.books[pos];
}
public void setBook(Book book) {
this.books[this.size] = book;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Book[] getBooks() {
return this.books;
}
}
ioperation
//IOperation.java
package ioperation;
import Book.BookList;
public interface IOperation {
void work(BookList bookList);
}
//AddOperation.java
package ioperation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class AddOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("添加图书...");
int size = bookList.getSize();
if(size == 10) {
System.out.println("书架满了...");
return;
}
Book[] books = bookList.getBooks();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入书名;");
String name = scanner.nextLine();
for (int i = 0; i < size; i++) {
if(name.equals(books[i].getName())) {
System.out.println("已存在这本书!");
return;
}
}
System.out.println("请输入作者:");
String author = scanner.nextLine();
System.out.println("请输入价格:");
int price = scanner.nextInt();
//读走回车
String tmp = scanner.nextLine();
System.out.println("请输入类型:");
String kind = scanner.nextLine();
Book book = new Book(name, author, price, kind);
bookList.setBook(book);
bookList.setSize(size + 1);
}
}
//DelOperation.java
package ioperation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class DelOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("删除图书...");
int size = bookList.getSize();
if(size == 0) {
System.out.println("书架为空!");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要删除的书名:");
String name = scanner.nextLine();
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
books[i] = null;
for (int j = i; j < size - 1; j++) {
books[j] = books[j + 1];
}
bookList.setSize(size - 1);
System.out.println("删除成功!");
return;
}
}
System.out.println("没有查询到指定的书!");
}
}
//FindOperation.java
package ioperation;
import Book.*;
import java.util.Scanner;
public class FindOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("查找图书...");
int size = bookList.getSize();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要查询的书名:");
String name = scanner.nextLine();
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
System.out.println("查找成功,信息如下:");
System.out.println(books[i].toString());
return;
}
}
System.out.println("没有查询到指定的书!");
}
}
//ShowOperation.java
package ioperation;
import Book.BookList;
import Book.Book;
public class ShowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("展示图书...");
int size = bookList.getSize();
if(size == 0) {
System.out.println("书架为空!");
return;
}
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
System.out.println(books[i].toString());
}
}
}
//BorrowOperation.java
package ioperation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("借阅图书...");
int size = bookList.getSize();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要借阅的书名:");
String name = scanner.nextLine();
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
if(!books[i].isBorrow()) {
System.out.println("借阅成功!");
books[i].setBorrow(true);
return;
}else {
System.out.println("该书已被借阅!");
return;
}
}
}
System.out.println("没有查询到指定的书!");
}
}
//ReturnOperation.java
package ioperation;
import Book.Book;
import Book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("归还图书...");
int size = bookList.getSize();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要归还的书名:");
String name = scanner.nextLine();
Book[] books = bookList.getBooks();
for (int i = 0; i < size; i++) {
if(books[i].getName().equals(name)) {
if(!books[i].isBorrow()) {
System.out.println("这本书还没有被借阅呢,无法归还,请检查是否输入错误!");
return;
}else {
System.out.println("归还成功!");
books[i].setBorrow(false);
return;
}
}
}
System.out.println("没有查询到指定的书!");
}
}
//ExitOperation.java
package ioperation;
import Book.BookList;
public class ExitOperation implements IOperation {
@Override
public void work(BookList bookList) {
System.out.println("退出系统...");
System.exit(0);
}
}
User
//User.java
package User;
import Book.BookList;
import ioperation.IOperation;
public abstract class User {
public String name;
protected IOperation[] iOperations;
public abstract int menu();
public User(String name) {
this.name = name;
}
public void doOperation(int choice, BookList bookList) {
this.iOperations[choice].work(bookList);
}
}
//AdminUser.java
package User;
import ioperation.*;
import java.util.Scanner;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new AddOperation(),
new DelOperation(),
new FindOperation(),
new ShowOperation(),
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name + "使用图书管理系统");
System.out.println("********管理员菜单********");
System.out.println("*************************");
System.out.println("******* 1.添加图书 *******");
System.out.println("******* 2.删除图书 *******");
System.out.println("******* 3.查找图书 *******");
System.out.println("******* 4.展示图书 *******");
System.out.println("******* 0.退出系统 *******");
System.out.println("*************************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的操作:");
int choice = scanner.nextInt();
if(choice < 0 || choice > 4) {
return -1;
}
return choice;
}
}
//NormalUser.java
package User;
import ioperation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitOperation(),
new FindOperation(),
new BorrowOperation(),
new ReturnOperation()
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name + "使用图书管理系统");
System.out.println("********普通用户菜单********");
System.out.println("*************************");
System.out.println("******* 1.查找图书 *******");
System.out.println("******* 2.借阅图书 *******");
System.out.println("******* 3.归还图书 *******");
System.out.println("******* 0.退出系统 *******");
System.out.println("*************************");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的操作:");
int choice = scanner.nextInt();
if(choice < 0 || choice > 3) {
return -1;
}
return choice;
}
}
Main(默认包)
//Main.java
import Book.BookList;
import User.*;
import java.util.Scanner;
public class Main {
public static User login() {
System.out.println("请输入您的名字:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("请输入您的身份:1. 管理员 2. 普通用户");
int choice = scanner.nextInt();
if(choice == 1) {
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
//创建书架
BookList bookList = new BookList();
//登录并确定用户类型返回
User user = login();
while (true) {
//根据用户类型打印菜单并返回用户操作选择
int choice = user.menu();
if(choice < 0) {
System.out.println("输入非法,请重新输入!");
}else {
//根据用户选择执行相关操作
user.doOperation(choice, bookList);
}
}
}
}
当前实现的项目没有保存数据的能力,我们后面会使用数据库做到这一点。
这个项目的意义在于提高我们的编码能力、熟悉前面学习的SE语法的应用场景、同时增加学习的趣味性
希望我们大家多多练习,早日成才!