Java 图书管理系统设计与实现
本教程将使用 Java 面向对象编程思想,基于类、对象、继承、多态及接口实现一个控制台图书管理系统。系统包含 Book、BooList、User、NormalUser、AdminiUser 等核心类,通过接口 IOperation 统一管理业务操作。
一、package book
1.1. Book
定义书籍实体类,包含书名、作者、价格、类型及借阅状态。
package book;
public class Book {
private String title; // 书籍名称
private String author; // 作者
private int price; // 价格
private String type; // 类型
private boolean BeBorrowed; // 是否被借出
public Book(String title, String author, int price, String type, boolean beBorrowed) {
this.title = title;
this.author = author;
this.price = price;
this.type = type;
this.BeBorrowed = beBorrowed;
}
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
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 isBeBorrowed() { return BeBorrowed; }
public void setBeBorrowed(boolean beBorrowed) { BeBorrowed = beBorrowed; }
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", price=" + price +
", type='" + type + '\'' +
", BeBorrowed=" + BeBorrowed +
'}';
}
}
1.2. BooList
定义书架类,用于存储和管理书籍数组。
package book;
public class BooList {
private Book[] books; // 存放书籍
private static final int DEFAULT_SIZE = 10;
private int usedSize; // 有效书籍的个数
public BooList() {
this.books = new Book[DEFAULT_SIZE];
this.books[0] = new Book("骆驼祥子", "老舍", 26, "小说", false);
this.books[1] = new Book("雷雨", "曹禺", 24, "戏剧", true);
this.books[2] = new Book("再别康桥", "徐志摩", 19, "诗歌", false);
this.books[3] = new Book("五猖会", "鲁迅", 21, "散文", false);
this.usedSize = 4;
}
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; }
}
二、package user
2.1. User
定义用户抽象基类,包含姓名和菜单方法。
package user;
import book.BooList;
import operation.IOperation;
public abstract class User {
public String name;
public IOperation[] iOperations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doOperation(int choice, BooList books) {
IOperation iOperation = this.iOperations[choice];
iOperation.work(books);
}
}
2.2. NormalUser 与 AdminiUser
普通用户和管理员继承自 User 类。
package user;
import operation.*;
import java.util.Scanner;
public class NormalUser extends User {
public NormalUser(String name) {
super(name);
this.iOperations = new IOperation[]{
new ExitSystem(), new FindBook(), new BorrowBook(), new ReturnBook()
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name + "图书管理系统");
System.out.println("**********普通用户菜单**********");
System.out.println("1.查找图书");
System.out.println("2.借阅图书");
System.out.println("3.归还图书");
System.out.println("0.退出系统");
System.out.println("****************************");
Scanner sca = new Scanner(System.in);
int choice = sca.nextInt();
return choice;
}
}
package user;
import operation.*;
import java.util.Scanner;
public class AdminiUser extends User {
public AdminiUser(String name) {
super(name);
this.iOperations = new IOperation[] {
new ExitSystem(), new FindBook(), new AddBook(), new DeleteBook(), new DisplayBook()
};
}
@Override
public int menu() {
System.out.println("欢迎" + this.name + "图书管理系统");
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 sca = new Scanner(System.in);
int choice = sca.nextInt();
return choice;
}
}
三、Main
主入口类,负责登录验证和流程控制。
import book.BooList;
import user.AdminiUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static User Login() {
Scanner sca = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sca.nextLine();
System.out.println("请输入你的身份:1.管理员 2.普通用户");
int choice = sca.nextInt();
if (choice == 1) {
return new AdminiUser(name);
} else {
return new NormalUser(name);
}
}
public static void main(String[] args) {
BooList booList = new BooList();
User user = Login();
int choice = user.menu();
user.doOperation(choice, booList);
}
}
四、package operation
4.1. IOperation 接口
统一业务操作接口。
package operation;
import book.BooList;
public interface IOperation {
void work(BooList books);
}
4.2. 业务逻辑实现
查找图书
package operation;
import book.BooList;
import book.Book;
import java.util.Scanner;
public class FindBook implements IOperation {
@Override
public void work(BooList booList) {
System.out.println("查找图书...");
Scanner sca = new Scanner(System.in);
String name = sca.nextLine();
int currentSize = booList.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = booList.getBook(i);
if (book.getTitle().equals(name)) {
System.out.println("找到这本书籍了,书籍内容如下:");
System.out.println(book);
return;
}
}
System.out.println("没有你要找的书籍。");
}
}
借阅图书
package operation;
import book.BooList;
import book.Book;
import java.util.Scanner;
public class BorrowBook implements IOperation {
@Override
public void work(BooList books) {
System.out.println("借阅图书...");
System.out.println("请输入你要借阅的图书:");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
int currentSize = books.getUsedSize();
for (int i = 0; i < currentSize; i++) {
Book book = books.getBook(i);
if (book.getTitle().equals(name)) {
if (book.isBeBorrowed()) {
System.out.println("这本书已经被借出了!");
} else {
book.setBeBorrowed(true);
System.out.println(book);
System.out.println("借阅成功!!");
}
return;
}
}
System.out.println("没有你要找的这本书,无法借阅!!");
}
}
退出系统
package operation;
import book.BooList;
public class ExitSystem implements IOperation {
@Override
public void work(BooList books) {
System.out.println("退出系统...");
int currentSize = books.getUsedSize();
for (int i = 0; i < currentSize; i++) {
books.setBooks(i, null);
}
System.exit(0);
}
}
(注:AddBook, DeleteBook, DisplayBook, ReturnBook 逻辑类似,可根据需求扩展)


