Java图书管理系统设计与实现

今天我要分享一个基于Java的图书管理系统的完整实现。这个系统采用了面向对象的设计思想,包含了图书管理、用户权限控制等核心功能。

1.系统架构

系统主要分为四个模块:

  1. book包:处理图书相关功能

  2. ioperations包:定义系统操作接口和实现

  3. user包:处理用户相关功能

  4. 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);
}

具体操作实现

  1. 添加图书 AddOperation

  2. 借阅图书 BorrowOperation

  3. 删除图书 DelOperation

  4. 查找图书 FindOperation

  5. 归还图书 ReturnOperation

  6. 显示图书 ShowOperation

  7. 退出系统 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和设计模式的同学有所帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值