Java--图书管理系统(新版详细讲解)

news2024/9/25 9:35:49

前言:

        对于初学者,自己写一个图书管理系统,会有效提高自己的代码能力,加深对Java中面向对象的理解,里面蕴含了Java中的类、接口、继承、多态等思想,接下来我们一起完成这一份"伟大的作品!"

注:以下的是作者自己的思想,大家可做借鉴和改动!

设计思想:

        首先Java中最核心的是什么?

        要想清楚对象之间的关系,我打算把每种对象都归类:

        1、和书有关的东西:书、书架

        2、和人有关的:管理员、普通用户

        3、链接人和书的所有功能:找书、展示所有书籍、借书、还书、结束系统等功能

我们发现大概可以分为三大类,每一类又可以再细分,细分之后还可以找到之间的关系(比如继承关系,扩展接口等等) 。

所以打算建4个包分别是:

        1、Book(实现和书有关的类)

        2、IOperation(实现和功能有关的类)

        3、User(实现和人有关的类)

        4、Main(实现和测试有关的类)

之后再细分:

和书有关的有:书架类、书类

和功能有关的有:找书类、借书类、还书类、查看所有图书类、添加书籍类、删除书籍类、退出系统类

和人相关的:管理员类、普通用户类

        在此基础上,我为了后期方便设计,我还在IOperation包中添加了一个公共接口为了让

后期所有类都可以统一(会在后期说明)

        在User包中添加一个父类,因为普通用户和管理员有共同特点!

Book包中的设计:

        现在首先设计Book包中的两个类:

Book类的设计:

        一本书得有哪些特征:

        1、书名

        2、作者

        3、出版社

        4、类型

        5、价格

        6、是否被借出

        ......

这里我就取前6种类型设计:

为了安全,我将这些变量都设计成private访问形式。(大家也可以设计成protected)

为了能够调用赋值,我给出get和set方法。

同样为了能够初始化:我给出constructor方法。(isBorrow变量不用初始化,一开始默认为false)

public class Book {
    private String name;
    private String author;
    private String publisher;
    private String type;
    private int price;
    private boolean isBorrow;

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

    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 String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public boolean isBorrow() {
        return isBorrow;
    }

    public void setBorrow(boolean borrow) {
        isBorrow = borrow;
    }
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", publisher='" + publisher + '\'' +
                ", type='" + type + '\'' +
                ", price=" + price +
                ", isBorrow=" + isBorrow +
                '}';
    }//重写打印的时候的内容
}

BookList类的设计:

        书架类的设计:

        1、书架上肯定有好书,这里可以用Book类型的数组表示

        2、书架上得记录书的数量,在这里给出num变量。

 对这两个变量给出set和get方法,并且给出构造方法进行初始化:

public class BookList {
    private Book[] books;
    private int num;

    public BookList( ) {
        this.books = new Book[10];//该书架上一共可以放10本书
        books[0] = new Book("三国演义","罗贯中","人民文学","小说",10);
        books[1] = new Book("水浒传","施耐庵","人民文学","小说",9);
        books[2] = new Book("Java","高斯林","外国文学","文学",8);
        this.num = 3;
    }

     public Book getBook(int i) {
        return books[i];
    }

    public void setBooks(Book book,int i) {
        books[i] = book;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

User包中的设计:

User类的设计:

        不管是普通人员或是管理人员共有:

        1、姓名

        2、菜单的打印

由于这个类我们不会直接使用里面的方法,可以设计为抽象类! 

public abstract class User {
    protected String name;//为了能够在子类中调用设计为protected

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract int menu();
}

 AdmiUser类的设计:

一定要继承父类,然后可以重写菜单方法,到时候可以用多态打印相应的菜单!

public class AdmiUser extends User{
    public AdmiUser(String name) {
        super(name);
        this.name = name;
    }
    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);
        int choice = scanner.nextInt();
        return choice;
    }
}

NomalUser类的设计:

和AdmiUser是一样的,只不过菜单有点区别:

public class NomalUser extends User {
    public NomalUser(String name) {
        super(name);
        this.name = name;
    }
    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);
        int choice = scanner.nextInt();
        return choice;
    }
}

Main包中的设计:

Main类的设计:

        作为程序的入口,我们应该想好,一开始的界面:

        "请输入姓名:"

        "请选择你的身份:"

        之后选择哪个就打印哪个菜单:

public class Main {
    public static User menu(){
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请选择你的身份:1.管理员  2.普通用户");
        int a = scanner.nextInt();
        while(true) {
            if (a == 1) {
                return new AdmiUser(name);
            } else if (a == 2){
                return new NomalUser(name);
            }else{
                System.out.println("选择错误,请重新选择:");
            }
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = menu();
        int choice = user.menu();
        
    }
}

当有这个界面就可以了,当然这时候还差如何把这些功能和链接进去:

此时就需要一个数组,然后把这些对应功能放在对应每个用户方法里面:

到时候就可以调用相应的功能功能了:


此时User类中就需要将数组放进去,但是这些功能的类型都不一样,如果是数组的话用改保持类型一样菜可以放,这时候我一开始定义的接口就有用了,如果此时,哪些所有功能能够扩展我的接口,那么就可以用到向上转型的知识,将所有类放进同一个数组里面:

修改User包:

将上述思想运用到User类、AdmiUser类、NomalUser类中:

User类:

public abstract class User {
    protected String name;
    protected Ioperation[] ioperations;

    public Ioperation[] getIoperations() {
        return ioperations;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public abstract int menu();
}

AdminUser类:

public class AdmiUser extends User{
    public AdmiUser(String name) {
        super(name);
        this.name = name;
        this.ioperations = new Ioperation[]{new ExitOperation(),new FindOperation(),new AddOperation()
        , new DelOperation(),new ShowOperation()};
    }
    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("************************");
        System.out.println("请选择你需要的功能:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

NomalUser类:

public class NomalUser extends User {
    public NomalUser(String name) {
        super(name);
        this.name = name;
        this.ioperations = new Ioperation[]{new ExitOperation(),new FindOperation(),new BorrowOperation()
                ,new ReturnOperation()
                };
    }
    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);
        int choice = scanner.nextInt();
        return choice;
    }
}

注意:

        在进行数组初始化的时候,所有的功能必须都得扩展Ioperation接口,否则会报错!!

修改Main类:

public class Main {
    public static User menu(){
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请选择你的身份:1.管理员  2.普通用户");
        int a = scanner.nextInt();
        while(true) {
            if (a == 1) {
                return new AdmiUser(name);
            } else if (a == 2){
                return new NomalUser(name);
            }else{
                System.out.println("选择错误,请重新选择:");
            }
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = menu();
        int choice = user.menu();
        user.getIoperations()[choice].work(bookList);//这一点很重要
        //这个会在相关IOperation包中说明
    }
}

IOperation包中的设计:

Ioperation接口的设计:

        该接口必须有,原因在Main设计的时候说过了,单纯只有这一个包还不行,该需要有方法,之后所有的类的方法都需要进行多态实现:

        设计一个work方法,IOperation包中所有类中都需要重写该方法:

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

 注意:

        必须传入书架这个类型参数,因为我们每个功能都需要借助书架来实现。

 AddOperation类的设计:

        首先要增加书,我觉得是常规操作,直接上代码:

public class AddOperation implements Ioperation{
    @Override
    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("请输入要增加书的出版社:");
        String publisher = scanner.nextLine();
        System.out.println("请输入要增加书的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入要增加书的价格:");
        int price = scanner.nextInt();
        Book book = new Book(name,author,publisher,type,price);
        bookList.setBooks(book, bookList.getNum());
        bookList.setNum(bookList.getNum()+1);
        System.out.println("增加成功!");
    }
}

BorrowOperation类的设计:

可以根据我的代码看出我的设计思想!!

代码如下:

public class BorrowOperation implements Ioperation{
    @Override
    public void work(BookList bookList){

        System.out.println("借阅图书!");
        System.out.println("请输入你要借阅的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int sum = bookList.getNum();
        int i= 0;
        for (i = 0; i< sum; i++) {
            if(bookList.getBook(i).getName().equals(name)) {
                System.out.println("已找到你要借阅的书!");
                if(bookList.getBook(i).isBorrow()){
                    System.out.println("不好意思,已被借出");
                    return ;
                }else{
                    System.out.println("借书成功!");
                    bookList.getBook(i).setBorrow(true);
                }
            }

        }
        if(i >= sum){
            System.out.println("这里没有你想借阅的书");
        }

    }

}

DelOperation类的设计:

代码如下:

public class DelOperation implements Ioperation{
    @Override
    public void work(BookList bookList){

        System.out.println("删除图书!");
        System.out.println("请输入你要删除书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int sum = bookList.getNum();
        int i = 0;
        for(;i<sum;i++){
            if(bookList.getBook(i).getName().equals(name)){
                System.out.println("已找到,书的信息如下,确定删除?");
                System.out.println(bookList.getBook(i));
                System.out.println("1.确定  2.取消");
                int a = scanner.nextInt();
                if(a == 1){
                    int j = i;
                    for (j = i; j < sum-1; j++) {
                        Book book = bookList.getBook(j+1);
                        bookList.setBooks(book,j);
                    }
                    bookList.setBooks(null,j);
                    System.out.println("删除成功!");
                    break;
                }else {
                    return ;
                }
            }
        }
        if(i>=sum) {
            System.out.println("未找到你要删除的相关书籍!");
        }
    }
}

ExitOperation类的设计:

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

}

这里调用了Systm中的exit方法,这里传一个可以让程序正常结束的值!

FindOperation类的设计:

public class FindOperation implements Ioperation{
    @Override
    public void work(BookList bookList){
        System.out.println("查找图书!");
        System.out.println("请输入你要查找树的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getNum();
        for (int i = 0;i<currentSize;i++){
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("存在这本书:信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到这本书,书名为"+name);
    }

}

ReturnOperation类的设计:
 

public class ReturnOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书!");
        System.out.println("请输入要归还图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int sum = bookList.getNum();
        int i = 0;
        for (; i < sum; i++) {
            if(bookList.getBook(i).getName().equals(name)){
                if(bookList.getBook(i).isBorrow()) {
                    System.out.println("归还成功!");
                    bookList.getBook(i).setBorrow(false);
                } else{
                    System.out.println("该书不在我们地点归还!");
                }

            }
        }
        if(i>sum){
            System.out.println("该书不属于我们这里!");
        }
    }
}

ShowOperation类的设计:

        我在设计的时候,加入了一个自动按价格排序!!

也就是在Book类中外接一个Compable接口:

之后重写compareTo方法:

就可以进行自动排序啦!!

代码如下:
 

public class ShowOperation implements Ioperation {
    @Override
    public void work(BookList bookList){

        System.out.println("展示图书!");
        int num = bookList.getNum();
        for (int i = 0; i < num-1; i++) {
            for (int j = 0; j <num-1-i ; j++) {
                if (bookList.getBook(j).compareTo(bookList.getBook(j + 1)) > 0) {
                    Book book = bookList.getBook(j);
                    bookList.setBooks(bookList.getBook(j + 1), j);
                    bookList.setBooks(book, j + 1);
                }
            }
        }
        for (int i = 0; i < num; i++) {
            System.out.println(bookList.getBook(i));
        }

    }
}

后记:

  当然这是我的一个整个设计思路:

希望大家看完有所收获,受到启发!!

我还是希望大家都有自己的想法,可以设计自己的图书管理系统!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2138158.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【若依RuoYi-Vue | 项目实战】帝可得后台管理系统(一)

文章目录 一、项目背景介绍1、什么是帝可得&#xff1f;2、物联网3、售货机术语4、角色与功能5、业务流程&#xff08;1&#xff09;平台管理员&#xff08;2&#xff09;运维人员&#xff08;3&#xff09;运营人员&#xff08;4&#xff09;消费者 6、产品原型7、库表设计 二…

基于Web的《药谷奇遇记》网站设计与实现---附源码72940

目 录 1 绪论 1.1 研究背景与意义 1.2国内外研究现状 1.3论文结构与章节安排 2 系统分析 2.1 可行性分析 2.1.1 技术可行性分析 2.1.2 经济可行性分析 2.1.3 法律可行性分析 2.2 系统功能分析 2.2.1 功能性分析 2.2.2 非功能性分析 2.3 系统用例分析 2.4 系统流程…

18062 二维数组每行中的最大值

### 思路 1. 使用指针变量遍历二维数组的每一行。 2. 对于每一行&#xff0c;找到该行的最大值。 3. 输出每一行的最大值。 ### 伪代码 1. 定义一个指向二维数组的指针变量 p。 2. 遍历二维数组的每一行&#xff1a; - 将 p 指向当前行。 - 初始化 max 为当前行的第一个…

【STM32系统】基于STM32设计的SD卡数据读取与上位机显示系统(SDIO接口驱动、雷龙SD卡)——文末资料下载

基于STM32设计的SD卡数据读取与上位机显示系统 演示视频&#xff1a; 基于STM32设计的SD卡数据读取与上位机显示系统 简介&#xff1a;本研究的主要目的是基于STM32F103微控制器&#xff0c;设计一个能够读取SD卡数据并显示到上位机的系统。SD卡的数据扇区读取不仅是为了验证存…

利用AI增强现实开发:基于CoreML的深度学习图像场景识别实战教程

&#x1f31f;&#x1f31f; 欢迎来到我的技术小筑&#xff0c;一个专为技术探索者打造的交流空间。在这里&#xff0c;我们不仅分享代码的智慧&#xff0c;还探讨技术的深度与广度。无论您是资深开发者还是技术新手&#xff0c;这里都有一片属于您的天空。让我们在知识的海洋中…

STL值list

list容器 头文件&#xff1a;#include<list> - list是一个双向链表容器&#xff0c;可高效地进行插入删除元素 - list不可以随机存取元素&#xff0c;所以不支持at.(pos)函数与[]操作符 注&#xff1a;list使用迭代器访问数据时可以一步一步走自增自减&#xff08;即…

誉龙视音频综合管理平台 RelMedia/FindById SQL注入漏洞复现

0x01 产品简介 誉龙视音频综合管理平台是深圳誉龙数字技术有限公司基于多年的技术沉淀和项目经验,自主研发的集视音频记录、传输、管理于一体的综合解决方案。该平台支持国产化操作系统和Windows操作系统,能够接入多种类型的记录仪,实现高清实时图传、双向语音对讲、AI应用…

CTFHub技能树-SQL注入-整数型注入

一、手动注入 思路&#xff1a;注入点->库->表->列->数据 首先使用order by探测有几列 http://challenge-215beae2f0b99b12.sandbox.ctfhub.com:10800/?id1 order by 2 我们发现order by 2 的时候有回显&#xff0c;到了order by 3 的时候就没有回显了&#xf…

npm install报错,gyp verb `which` failed Error: not found: python

主要错误 gyp verb which failed Error: not found: python2 gyp ERR! configure error gyp ERR! stack Error: Cant find Python executable "python", you can set the PYTHON env variable. npm ERR! node-sass4.14.1 postinstall: node scripts/build.js 全部错…

Apisix离线安装

上传离线包 #ll apisix-3.2.2-0.el7.x86_64.rpm apisix-base-1.21.4.1.8-0.el7.x86_64.rpm apisix-dashboard-3.0.1-0.el7.x86_64.rpm cyrus-sasl-2.1.26-24.el7_9.x86_64.rpm cyrus-sasl-devel-2.1.26-24.el7_9.x86_64.rpm cyrus-sasl-gssapi-2.1.26-24.el7_9.x86_64.rpm cyr…

【H2O2|全栈】关于CSS(1)CSS基础(一)

目录 CSS基础知识 前言 准备工作 啥是CSS&#xff1f; 如何引用CSS&#xff1f; 选择器 通配符选择器 类名&#xff08;class&#xff09;选择器 id选择器 CSS解析顺序&#xff08;优先级&#xff09; 常见CSS标签&#xff08;一&#xff09; 字体属性 font-style…

spring模块(六)spring event事件(3)广播与异步问题

发布事件和监听器之间默认是同步的&#xff1b;监听器则是广播形式。demo&#xff1a; event&#xff1a; package com.listener.demo.event;import com.listener.demo.dto.UserLogDTO; import org.springframework.context.ApplicationEvent;public class MyLogEvent extends…

C#命令行参数解析库System.CommandLine介绍

命令行参数 平常在日常的开发过程中&#xff0c;会经常用到命令行工具。如cmd下的各种命令。 以下为sc命令执行后的截图&#xff0c;可以看到&#xff0c;由于没有输入任何附带参数&#xff0c;所以程序并未执行任何操作&#xff0c;只是输出了描述和用法。 系统在创建一个新…

电脑怎么恢复原来的ip地址:全面指南与注意事项

在使用电脑连接网络时&#xff0c;有时可能会因为某些原因需要更改IP地址。然而&#xff0c;在某些情况下&#xff0c;我们可能希望将电脑的IP地址恢复到原来的设置。本文将详细介绍如何恢复电脑原来的IP地址&#xff0c;并提供一些注意事项。 一、了解IP地址的分配方式 在恢复…

Linux-LVM逻辑卷管理

一、背景 Linux运维过程中大家有没有想过生产环境服务器磁盘分区如果数据量越来越膨胀(这些都是重要数据&#xff0c;不能删除)&#xff0c;那么此时如何来应对这个问题呢? 既要不影响正在运行的程序&#xff0c;同时也不能中断关机等操作。 这么一想就很蛋疼了。假设你运行…

力扣-96.不同的二叉搜索树 题目详解

题目: 给你一个整数 n &#xff0c;求恰由 n 个节点组成且节点值从 1 到 n 互不相同的 二叉搜索树 有多少种&#xff1f;返回满足题意的二叉搜索树的种数。 二叉搜索树介绍: 二叉搜索树是一个有序树&#xff1a; 若它的左子树不空&#xff0c;则左子树上所有结点的值均小于它…

凸优化学习(3)——对偶方法、KKT条件、ADMM

&#x1f345; 写在前面 &#x1f468;‍&#x1f393; 博主介绍&#xff1a;大家好&#xff0c;这里是hyk写算法了吗&#xff0c;一枚致力于学习算法和人工智能领域的小菜鸟。 &#x1f50e;个人主页&#xff1a;主页链接&#xff08;欢迎各位大佬光临指导&#xff09; ⭐️近…

【pyenv】pyenv安装版本超时的解决方案

目录 1、现象 2、分析现象 3、手动下载所需版本 4、存放到指定路径 5、重新安装 6、pip失败&#xff08;做个记录&#xff0c;未找到原因&#xff09; 7、方法二修改环境变量方法 7.1 设置环境变量 7.2 更新 7.3 安装即可 8、方法三修改XML文件 前言&#xff1a;研…

【Android】Room—数据库的基本操作

引言 在Android开发中&#xff0c;数据持久化是一个不可或缺的部分。随着应用的复杂度增加&#xff0c;选择合适的数据存储方式变得尤为重要。Room数据库作为Android Jetpack架构组件之一&#xff0c;提供了一种抽象层&#xff0c;使得开发者能够以更简洁、更安全的方式操作SQ…

PCIe进阶之TL:First/Last DW Byte Enables Rules Traffic Class Field

1 First/Last DW Byte Enables Rules & Attributes Field 1.1 First/Last DW Byte Enables Rules Byte Enable 包含在 Memory、I/O 和 Configuration Request 中。本文定义了相应的规则。Byte Enable 位于 header 的 byte 7 。对于 TH 字段值为 1 的 Memory Read Request…