【Java】实现简易图书管理系统(详细功能图解)

在这里插入图片描述

一、功能介绍

1.1 系统的使用

1、输入姓名
在这里插入图片描述
2、选择身份
在这里插入图片描述
3、根据身份出现不同的菜单
在这里插入图片描述

1.2 整体框架

在这里插入图片描述
将这三个方向做成三个包来分别实现
在这里插入图片描述
下面让我们进行更加具体的思路讲解:

二、具体思路

在这里插入图片描述
因为功能类本质都是对数组进行操作,只是各自功能不同,所以创建一个IOperation接口,里面放一个work方法,让每个功能类都implements这个接口,对其中的work方法进行重写
在这里插入图片描述
在这里插入图片描述

三、代码实现

3.1 book包

3.1.1 Book类

package book;

public class Book {
    private String name;//书的名字
    private String author;//书的作者
    private int price;//书的价格
    private String type;//书的类型
    private boolean state;//是否被借出

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
        this.state = state;
    }

    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;
    }

    @Override
    public String toString() {
        return super.toString();
    }
    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }
}

3.1.2 BookList类

package book;

//书架类
public class BookList {
    private Book[] books=new Book[10];//书架能放十本书
    public int useSize;//存储当前书的个数


    //实现通过构造方法初始化的时候,给数组里存三本书 useSize=3
    public BookList(){
        books[0]= new Book("《三国演义》","罗贯中",98,"小说");
        books[1]= new Book("《西游记》","吴承恩",78,"小说");
        books[2]= new Book("《红楼梦》","曹雪芹",89,"小说");
        this.useSize=3;
    }


    //这里本来应该写一些操作这个数组的方法,但我们不这么做,换一种方法
    //把这些操作都写在一个接口里


    public int getUseSize() {
        return useSize;
    }

    public void setUseSize(int useSize) {
        this.useSize = useSize;
    }

    //你给我一个位置,我给你返回一个该位置的书
    public Book getBooks(int pos){
        return books[pos];
    }

    //存放一本书到指定位置
    //这里的指定位置是当前最后一本书的下一个位置
    public void setBooks(Book book,int pos) {
        books[pos] = book;
    }
}

3.2 operations包

3.2.1 IOperation接口

package operations;

import book.BookList;

public interface IOperation {
    void work(BookList bookList);
}

3.2.2 AddOperationk类

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{

    public void work(BookList bookList){
        System.out.println("添加图书");

        System.out.println("请输入你要新增图书的名字: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入你要新增图书的作者: ");
        String author = scanner.nextLine();
        System.out.println("请输入你要新增图书的价格: ");
        int price = scanner.nextInt();

        scanner.nextLine();//读取回车键

        System.out.println("请输入你要新增图书的类型: ");
        String type = scanner.nextLine();
        Book book = new Book(name,author,price,type);
        //1.获取当前可以放书的位置
        int currentSize = bookList.getUseSize();
        //2.把书放进指定位置
        bookList.setBooks(book,currentSize);
        //3.书的有效个数加 1
        bookList.setUseSize(currentSize+1);
    }
}

3.2.3 DelOperationk类

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的书名:");
        String name = scanner.nextLine();
        //获取书架中现在有几本书
        int usedSize = bookList.getUseSize();
        int i=0;
        //根据书名找到这个本书的下标
        for (i = 0; i < usedSize; i++) {
            Book book = bookList.getBooks(i);
            if (book.getName().equals(name)) {
                break;
            }
        }
        if (i == usedSize) {
            System.out.println("删除失败,未找到该书。");
            return;
        }
        //通过前移将书删除
        for (i = 0; i < usedSize - 1; i++) {
            bookList.setBooks(bookList.getBooks(i+1),i);
        }
        bookList.setUseSize(bookList.getUseSize()-1);//书架中图书数量-1
        System.out.println("删除成功。");
    }
}

3.2.4 FindOperationk类

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation{

    public void work(BookList bookList){
        System.out.println("查找图书");
        System.out.println("请输入你要查找图书的名字: ");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            if (name.equals(book.getName())){
                System.out.println("找到了这本书");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

3.2.5 DisplayOperation类

package operations;

import book.Book;
import book.BookList;

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示所有图书");

        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            System.out.println(book);
        }
    }
}

3.2.6 BorrowOperation类

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入想要借阅书籍的名称:  ");
        String name = scanner.nextLine();
        //获取当前书架上书的个数
        int currentSize = bookList.getUseSize();
        //查找有没有要借阅的书
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBooks(i);
            //判断书有没有被借走
            if (book.getName().equals(name)){
                if (!book.isState()){
                    System.out.println("借阅成功!");
                    book.setState(true);  //将书的状态改为已借出
                    return;
                }else {
                    System.out.println("借阅失败,该书已被借走");
                    return;
                }
            }
        }
        System.out.println("借阅失败,未找到您要找的书籍");
    }
}

3.2.7 ReturnOperation类

package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");


        Scanner scanner = new Scanner(System.in);
        System.out.println("输入要归还书的名称:");
        String name = scanner.nextLine();
        //获取书架上书的个数
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            //查找书架上有没有这本书
            Book book = bookList.getBooks(i);
            if (book.equals(name)){
                //判断书是否被借走
                if (book.isState()){
                    System.out.println("成功归还");
                    book.setState(false);//将该书状态设置为未被借阅
                    return;
                }else {
                    System.out.println("归还失败,该书已被借阅");
                    return;
                }
            }
        }
        System.out.println("未找到该书");
        return;
    }
}

3.2.7 ExitOperation类

package operations;

import book.BookList;

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

3.3 user包

3.3.1 User类

package user;

import book.BookList;
import operations.IOperation;

import javax.swing.plaf.SpinnerUI;

//All使用者类
public abstract class User {
    public void doOperation(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }


    protected IOperation[] iOperations;//只是定义了数组,没有初始化

    protected String name;

    public User(String name) {
        this.name = name;
    }

    public abstract int menu();
}

3.3.2 AdminUser类

package user;

import operations.*;

import java.util.Scanner;

//管理员类
public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);

        this.iOperations = new IOperation[]{  //AdminUser接口数组
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation()
        };
    }
    public int menu(){
        System.out.println("********************************");
        System.out.println("hello " + name +"欢迎来到图书小练习");
        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);
        int choice = scanner.nextInt();
        return choice;
    }
}

3.3.3 NormalUser类

package user;

import operations.*;

import java.util.Scanner;

//普通用户类
public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{  //NormalUser接口数组
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
        };

    }
    public int menu(){
        System.out.println("********************************");
        System.out.println("hello " + name +"欢迎来到图书小练习");
        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);
        int choice = scanner.nextInt();
        return choice;
    }
}

感谢阅读,如有问题请各位大佬指正
在这里插入图片描述

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值