一、引言
本篇介绍了一个简易的图书管理系统,面向管理员和普通用户分别给出了不同的菜单,实现了一些基本的图书操作功能,包括图书的增删查改、借阅、归还等
二、图书管理系统框架
图书管理系统,顾名思义,管理的是图书,所以需要定义一个类来存放书籍的信息,同时定义一个类来表示存放书籍的书架;在系统中需要进行查找图书、借阅图书、归还图书、新增图书、删除图书、显示图书、退出系统等操作,我们可以将这些功能定义在一个包里面,通过实现一个共同的接口,进而进行书籍的各项操作;之前所描述的菜单也可以定义在一个包中,管理员和普通用户分别定义为不同的类,继承自同一个父类,在创建不同的对象时进行不同的操作
2.1书籍
书籍类中需要包含书籍的基本信息以及使用这些信息的方法,包括
- 书籍名称name
- 书籍作者author
- 书籍价格price
- 书籍类型type
- 被借阅情况isBorrowed
- 相应的各个get-set方法
- 显示所有信息的toString方法(需要重写)
2.2书架
创建一个Book类数组,用来存储书籍的数据,变量useSize表示有效的数据个数(实际存放的书的个数),相应的get-set方法,toString方法(需要被重写)
2.3功能接口
定义一个IOperations接口,通过接口实现具体的功能,使程序更有条理性
二、图书管理系统菜单
不同身份的人对于图书管理系统有着不同的需求,比如管理者需要进行图书的增删查改,普通用户则需要进行图书的借阅和归还,在程序中体现在根据不同身份给出不同的系统菜单
定义一个User类,实现两个类共有的成员变量以及成员方法
3.1管理员菜单
新建一个AdminUser类,继承User类,需要重写父类的方法,并实现自己菜单的打印
- 查找图书
- 新增图书
- 删除图书
- 显示图书
- 退出系统
3.2普通用户菜单
新建一个NormalUser类,继承User类,需要重写父类的方法,并实现自己菜单的打印
- 查找图书
- 借阅图书
- 归还图书
- 退出系统
四、图书管理系统功能
4.1新增图书
首先需要判断书架是否是满的,若是满的,输出信息,否则进行新增书籍的信息的录入。在获取到新增书籍的信息之后,需要判断一下这本书在书架上是否存在,若存在,输出错误信息,不存在,正常录入,同时需要修改usedSize的值
4.2借阅图书
输入需要借阅的书籍名称,在书架上查找该书,找到后判断该书籍的借阅状态,若已被借阅,输出错误信息,若未被借阅,修改借阅状态,借阅成功,若是未找到该书,输出错误信息
4.3删除图书
输入需要删除的书籍名称,在书架上查找该书,若存在,查找要删除的书籍所在下标,从该下标开始,通过元素覆盖的方式,删除书籍信息,删除后要记得修改对应的usedSize的值。同样的,要是被删除的书籍不存在,输出错误信息
4.4查找图书
遍历数组,通过书籍的名字进行图书的查找,这里需要用到书架的usedSize作为循环的终止条件,找到后打印书籍信息,否则输出错误信息
4.5归还图书
和借阅图书过程类似,输入书籍名称,遍历书架,找到被借走的书籍的信息,将书籍的状态改成未借阅,如果没有找到该书,则输出错误信息
4.6展示图书
遍历书架,逐个打印书籍信息
4.7退出系统
使用exit方法退出系统
五、图书管理系统完整代码实现
5.1总体框架
5.2book包
5.2.1ioperations包
5.2.1.1AddOperations类
package book.ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperations implements IOperations{
public void work(BookList bookList){
System.out.println("新增图书...");
int currentsize = bookList.getUsedSize();
if(currentsize == bookList.getBooks().length){
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("请输入价格:");
int price = scanner.nextInt();
System.out.println("请输入书籍类型:");
String type = scanner.nextLine();
Book newbook = new Book(name,author,price,type);
for (int i = 0; i < currentsize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
System.out.println("此书已存在,插入失败");
return;
}
bookList.setBook(currentsize,newbook);
bookList.setUsedSize(currentsize++);
System.out.println("新增图书成功!");
}
}
}
5.2.1.2BorrowOperations类
package book.ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperations implements IOperations{
public void work(BookList bookList){
System.out.println("借阅图书...");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要借阅的图书名:");
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)){
if(book.isBorrowed()){
System.out.println("该书已被借阅...");
book.setBorrowed(false);
return;
}else{
System.out.println("借阅成功!");
return;
}
}
}
System.out.println("未找到此书,借阅失败!");
}
}
5.2.1.3DeleteOperations类
package book.ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DeleteOperations implements IOperations{
public void work(BookList bookList){
System.out.println("删除图书...");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的图书名:");
String name = scanner.nextLine();
int currentsize = bookList.getUsedSize();
int pos = -1;
int i = 0;
for (; i < currentsize; i++) {
Book book = bookList.getBook(i);
if(book.getName().equals(name)) {
pos = i;
break;
}
}
if(i == currentsize){
System.out.println("该图书不存在,无法删除!");
return;
}
for(int j = pos; j < currentsize - 1; j++){
Book book = bookList.getBook(j+1);
bookList.setBook(j,book);
}
bookList.setBook(currentsize-1,null);
bookList.setUsedSize(currentsize-1);
System.out.println("《"+name+"》"+"删除成功");
}
}
5.2.1.4ExitOperations类
package book.ioperations;
import book.BookList;
public class ExitOperations implements IOperations{
public void work(BookList bookList){
System.out.println("退出系统...");
System.exit(0);
}
}
5.2.1.5FindOperations类
package book.ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperations implements IOperations{
public void work(BookList bookList){
System.out.println("借阅图书...");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查找的图书名:");
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("已找到...\n书籍信息为:");
System.out.println(book);
return;
}
}
System.out.println("未找到此书...");
}
}
5.2.1.6ReturnOperations类
package book.ioperations;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperations implements IOperations{
public void work(BookList bookList){
System.out.println("归还图书...");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要归还的图书名:");
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)) {
if (book.isBorrowed()) {
System.out.println("归还成功!");
book.setBorrowed(false);
return;
}
}
System.out.println("未找到此书,归还失败!");
}
}
}
5.2.1.7ShowOperations类
package book.ioperations;
import book.Book;
import book.BookList;
import java.sql.SQLOutput;
public class ShowOperations implements IOperations{
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);
}
}
}
5.2.1.8IOperations接口
package book.ioperations;
import book.BookList;
public interface IOperations {
void work(BookList bookList);
}
5.2.2Book类
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?" 已借出":" 未借出")+
'}';
}
}
5.2.3BookList类
package book;
import java.util.Arrays;
public class BookList {
private Book[] books = new Book[10];
private int usedSize;
public BookList(){
this.books[0] = new Book("西游记","吴承恩",20,"小说");
this.books[1] = new Book("水浒传","施耐恩",20,"小说");
this.books[2] = new Book("三国演义","罗贯中",20,"小说");
this.usedSize = 3;
}
public Book[] getBooks() {
return books;
}
public Book getBook(int pos) {
return books[pos];
}
public void setBook(int pos,Book book) {
this.books[pos] = book;
}
public int getUsedSize() {
return usedSize;
}
public void setUsedSize(int usedSize) {
this.usedSize = usedSize;
}
@Override
public String toString() {
return "BookList{" +
"books=" + Arrays.toString(books) +
'}';
}
}
5.3Main包
5.3.1test类
package Main;
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class test {
public static User login(){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.nextLine();
System.out.println("请选择您的身份:1.管理员 2.普通用户");
int choice = scanner.nextInt();
while(true){
if(choice==1){
return new AdminUser(name);
}else if(choice==2){
return new NormalUser(name);
}else{
System.out.println("输入错误,请重新输入!!");
}
}
}
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while(true) {
int choice = user.menu();
user.doIoperation(choice, bookList);
}
}
}
5.4user包
5.4.1User类
package user;
import book.BookList;
import book.ioperations.IOperations;
public abstract class User {
protected String name;
public IOperations[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doIoperation(int choice, BookList bookList){
iOperations[choice].work(bookList);
}
}
5.4.2AdminUser类
package user;
import java.util.Scanner;
import book.ioperations.*;
public class AdminUser extends User {
public AdminUser(String name) {
super(name);
this.iOperations = new IOperations[]{
new ExitOperations(),
new FindOperations(),
new AddOperations(),
new DeleteOperations(),
new ShowOperations()
};
}
public int menu(){
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("请输入你的操作:");
return scanner.nextInt();
}
}
5.4.3NormalUser类
package user;
import book.ioperations.*;
import java.util.Scanner;
public class NormalUser extends User{
public NormalUser(String name) {
super(name);
this.iOperations = new IOperations[]{
new ExitOperations(),
new FindOperations(),
new BorrowOperations(),
new ReturnOperations()
};
}
public int menu(){
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("请输入你的操作:");
return scanner.nextInt();
}
}