今天我要分享一个基于Java的图书管理系统的完整实现。这个系统采用了面向对象的设计思想,包含了图书管理、用户权限控制等核心功能。
1.系统架构
系统主要分为四个模块:
-
book
包:处理图书相关功能 -
ioperations
包:定义系统操作接口和实现 -
user
包:处理用户相关功能 -
test
包:系统入口
2.核心类设计
1. Book类
Book
类是系统的基础数据单元,封装了图书的基本属性:
package book;
public class Book {
private String name;
private String autor;
private int price;
private String type;
private boolean isBorrowed;
public Book(String name, String autor, int price, String type) {
this.name = name;
this.autor = autor;
this.price = price;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
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 + '\'' +
", autor='" + autor + '\'' +
", price=" + price +
", type='" + type + '\'' +
( (isBorrowed == true) ? " 已借出" : " 未借出") +
//", isBorrowed=" + isBorrowed +
'}';
}
}
2. BookList类
BookList
类管理图书集合,提供了对图书数组的基本操作:
package book;
public class BookList {
private Book books[] = new Book[10];
private int usedSize;
public BookList() {
this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",17,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",21,"小说");
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 setBook(int pos,Book book) {
this.books[pos] = book;
}
public Book[] getBooks() {
return books;
}
}
3.用户系统设计
系统区分了两种用户类型,使用继承实现:
1. 抽象基类User
package user;
import book.BookList;
import ioperations.IOPeration;
import java.util.Scanner;
public abstract class User {
public String name;
public IOPeration[] ioPerations;
public User(String name) {
this.name = name;
}
public abstract int menu();
public void doIoperation(int choice, BookList bookList){
ioPerations[choice].work(bookList);
}
}
2. 管理员用户AdminUser
管理员拥有更高级别的操作权限:
package user;
import ioperations.*;
import java.util.Scanner;
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("欢迎"+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 scanner = new Scanner(System.in);
System.out.println("请输入你的操作");
int choice = scanner.nextInt();
return choice;
}
}
3. 普通用户NormalUser
普通用户只能进行基本操作:
package user;
import ioperations.*;
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()
};
}
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 scanner = new Scanner(System.in);
System.out.println("请输入你的操作");
int choice = scanner.nextInt();
return choice;
}
}
4.操作接口与实现
系统通过IOPeration
接口定义所有操作,并使用策略模式实现不同功能:
public interface IOPeration {
void work(BookList bookList);
}
具体操作实现
-
添加图书
AddOperation
-
借阅图书
BorrowOperation
-
删除图书
DelOperation
-
查找图书
FindOperation
-
归还图书
ReturnOperation
-
显示图书
ShowOperation
-
退出系统
ExitOperation
5.系统入口
Main
类是程序的入口点:
package test;
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BookList bookList = new BookList();
User user = login();
while(true) {
int choice = user.menu();
user.doIoperation(choice,bookList);
}
}
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();
if (choice==1){
return new AdminUser(name);
}else {
return new NormalUser(name);
}
}
}
6.总结
这个图书管理系统虽然规模不大,但完整展示了Java面向对象编程的核心思想。通过合理的类设计和接口抽象,系统具有良好的扩展性和维护性。
希望这个实现能对学习Java和设计模式的同学有所帮助!