图书管理系统(Java版本)

news2024/10/5 12:48:11

文章目录

  • 前言
  • 要求
  • 1.设置对象
    • 1.1.图书
    • 1.2.书架
    • 2.管理员
    • 3.功能的实现
  • 2.搭建框架
    • 2.1.登录(login)
    • 2.2.菜单
    • 2.3.操作方法的获取
  • 3.操作方法的实现
    • 3.1.退出系统(ExitOperation)
    • 3.2.显示图书(ShowOperation)
    • 3.3.查阅图书(FindOperation)
    • 3.4.新增图书(AddOperation)
    • 3.5.借出图书(BorrowOperation)
    • 3.6.归还图书(ReturnOperation)
    • 3.7.删除图书(DelOperation)


前言

前面足足有十篇博客,给大家讲解了Java的基础语法,尤其是面向对象以及其思想,是我们遇到的第一种障碍,为此咱们写一个图书管理系统进行巩固。

要求

在这里插入图片描述
这边是一个主要流程,其中具体的功能就不在此一一展示,等会在功能的实现中,具体再说

1.设置对象

1.1.图书

根据图上所示,我们可以发现,每一个图书的成员变量有五个:
书名,作者,价格,类型,以及借出情况。
并且我们不想要让别人直接得到,那么都需要用private修饰。

1.2.书架

每本书都要放到书架中,方才可以使用,当然还要直到当前图书的个数,以及如何取书和放书。
下来我们这两个放到一个包中,编写其内容,代码。
书架类

public class BookList {
    Book[]books=new Book[10];
    private int UseSize;
    public BookList(){
        this.books[0]=new Book("三国演义","罗贯中",9.9,"小说");
        this.books[1]=new Book("我与地坛","史铁生",23.9,"散文");
        this.books[2]=new Book("红楼梦","曹雪芹",9.9,"小说");
        this.UseSize=3;
    }
    public BookList(int useSize) {
        UseSize = useSize;
    }
    public Book getBook(int pos){
        return books[pos];
    }
    public void setBook(int pos,Book book){
        this.books[pos]=book;
    }

    public int getUseSize() {
        return UseSize;
    }

    public void setUseSize(int useSize) {
        UseSize = useSize;
    }
     public Book[] getBooks() {
        return books;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }
}

图书类

public class Book  {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean isBorrowed;

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

    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 double getPrice() {
        return price;
    }

    public void setPrice(double 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 + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                (isBorrowed==false?"未借出":"已借出")+
                '}';
    }
}

2.管理员

管理员分为两类,1.一种是普通用户,2。一种是管理员用户。
我们把这两种用户的相同点放到一个User抽象类中,再分别实现自己的功能,把这三个类放到一个包里。
User抽象类

public abstract class User {
    protected String name;

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

AdminUser类

public class AdminUser extends User {
    public AdminUser(String name) {
        super(name);
    }

    @Override
    public void menu() {

    }
}

NormalUser类

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
    }

    @Override
    public void menu() {

    }
}

其中具体的实现,等会在写

3.功能的实现

我们通过上面的图,可以发现一共有7种操作方法,并且有一样的,首先我们会有2个问题:第一个问题:如何可以知道User的类型,第二种,如何可以该用户有哪些方法,并且如何去进行调用呢?这些方法等会都会进行解答,首先我们先把每个功能的框架搭建一下,在创建一个包(iOperation)。
在这里插入图片描述

这边各种包的创建过程,其中有7大操作功能,内容和上面的都一样,大家下面自己去搭建一下。
具体内容,现在进行操作。


2.搭建框架

2.1.登录(login)

在这里插入图片描述

我们现在实现这个页面的内容。主要我们是要想返回值是什么?结合上面第一个问题,返回值是User,通过登录这个方法,我们可以确定目前是什么用户进行登录。

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

2.2.菜单

在这里插入图片描述
现在我们完成这个菜单页面,并且返回值又是什么?因为,我们已经知道身份了,那么就要知道操作方法的选择,因此返回值是int

AdminUser类

public class AdminUser extends User {
    public AdminUser(String name) {
        super(name);
    }

    @Override
    public int menu() {
        System.out.println("****************");
        System.out.println("0.退出系统        ");
        System.out.println("1.查找图书        ") ;
        System.out.println("2.新增图书        ") ;
        System.out.println("3.删除图书        ") ;
        System.out.println("4.显示图书        ") ;
        System.out.println("****************");
        System.out.println("请输入你的操作:");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }
}

NormalUser类

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
    }

    @Override
    public int  menu() {
        System.out.println("****************");
        System.out.println("0.退出系统        ");
        System.out.println("1.查找图书        ") ;
        System.out.println("2.借阅图书        ") ;
        System.out.println("3.归还图书        ") ;
        System.out.println("****************");
        System.out.println("请输入你的操作:");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }
}

User父类就是把原来menu的返回值改为int即可,我就在这不演示啦。

2.3.操作方法的获取

现在便是要解决最难得一个问题,如何让控制台得知,我们要进行那个操作,现在我们知道了用户类型,以及选择的方法,我们可以使用一个数组,每个下标都对应各自的方法,而我们要将7大操作方法,组合在一起,又要跟下标相关联,那一定是数组。并且,要让7大操作方法,都有一个共有的东西,使得在调用这个东西的时候,就是在调用他们自己的话,那就是接口啦!
为此我们在iOperation包中创建一个接口。
IOperation接口

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

创建完接口,下来就要在User中创建IOperation数组了,然后分别在AdminUser和NormalUser中,分别对数组进行初始化,与此同时,在User中创建一个方法,可以在Test中使用接口。
User抽象类

public abstract class User {
    protected String name;
    protected IOPeration[]ioPerations;
    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);
    }
}

AdminUser类

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()  
       };
    };
    @Override
    public int menu() {
        System.out.println("****************");
        System.out.println("0.退出系统        ");
        System.out.println("1.查找图书        ") ;
        System.out.println("2.新增图书        ") ;
        System.out.println("3.删除图书        ") ;
        System.out.println("4.显示图书        ") ;
        System.out.println("****************");
        System.out.println("请输入你的操作:");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }
}

NormalUser类

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.ioPerations=new IOPeration[]{
                new ExitOperation(),
                new FindOperation() ,
                new BorrowOperation(),
                new ReturnOperation()};
    };
    @Override
    public int  menu() {
        System.out.println("****************");
        System.out.println("0.退出系统        ");
        System.out.println("1.查找图书        ") ;
        System.out.println("2.借阅图书        ") ;
        System.out.println("3.归还图书        ") ;
        System.out.println("****************");
        System.out.println("请输入你的操作:");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }
}

Test类

public class Test {
    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);
        }
    }
    public static void main(String[] args) {
        BookList bookList=new BookList();
        User user=login();
        while (true){
            int choice=user.menu();
            user.doOperation(choice,bookList);
        }
    }
}

由此观之:将7大操作方法连接起来的的是接口
将方法按照顺序让控制台知道我们的操作,是数组(也可以说是下标),将这两个结合起来,便是我们的创新的点子,最精彩的在上面,大家好好领悟,领悟完,再看每个操作方法的具体实现


3.操作方法的实现

3.1.退出系统(ExitOperation)

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

3.2.显示图书(ShowOperation)

public class ShowOperation implements IOPeration{
    public void work(BookList bookList){
    	System.out.println("显示图书...");
        int currentSize=bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book=bookList.getBook(i);
            System.out.println(book);
        }
    }
}

3.3.查阅图书(FindOperation)

public class FindOperation implements IOPeration {
    public void work(BookList bookList){
        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.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("找到了图书:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到你想查阅的书籍");
    }
}

3.4.新增图书(AddOperation)

public class AddOperation implements IOPeration{
    public void work(BookList bookList){
        //1.判满和判空
        System.out.println("新增图书...");
        int currentSize= bookList.getUseSize();
        if(currentSize==0){
            System.out.println("书架中已无书");
            return;
        }
        if(currentSize==bookList.getBooks().length){
            System.out.println("书架已放满书");
            return;
        }
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入书名:");
        String name=scanner.nextLine();
        System.out.println("请输入作者:");
        String author=scanner.nextLine();
        System.out.println("请输入类型:");
        String type=scanner.nextLine();
        System.out.println("请输入价格:");
        Double price= scanner.nextDouble();
        Book newbook=new Book(name,author,price,type);
        for (int i = 0; i < currentSize; i++) {
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("此书在书架上已存在...");
                return;
            }
        }
        //插入书籍
        bookList.setUseSize(currentSize+1);
        bookList.setBook(currentSize,newbook);
        System.out.println("新书增加成功...");
    }
}

3.5.借出图书(BorrowOperation)

public class BorrowOperation implements IOPeration {
    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.getBook(i);
            if(book.getName().equals(name)){
                if(book.isBorrowed()){
                    System.out.println("此书已经借出!");
                    return;
                }
                book.setBorrowed(true);
                System.out.println("借阅成功...");
                return;
            }
        }
        System.out.println("没有查到你要借阅的图书");
    }
}

3.6.归还图书(ReturnOperation)

借阅图书和归还图书本质是一样的

public class ReturnOperation implements IOPeration{
    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.getBook(i);
            if(book.getName().equals(name)){
                if(book.isBorrowed()){
                    book.setBorrowed(false);
                    System.out.println("归还成功...");
                    return;
                }
            }
        }
        System.out.println("没有查到你要归还的图书");
    }
}

3.7.删除图书(DelOperation)

这就需要数据结构的知识啦,可以利用画图来演示
在这里插入图片描述
这里用数字代替图书

public class DelOperation 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();
        int i=0;
        int pos=-1;
        for (;i<currentSize;i++){
            Book book=bookList.getBook(i);
            if(book.getName().equals(name)){
                pos=i;
                break;
            }
        }
        if(i==currentSize){
            System.out.println("没有找到你想删除的图书");
            return;
        }
        //删除图书
        for (int j=pos;j<currentSize-1;j++){
            Book book=bookList.getBook(j+1);
            bookList.setBook(j,book);
        }
        bookList.setBook(currentSize,null);
        bookList.setUseSize(currentSize-1);
        System.out.println("删除成功!");
    }
}


具体代码:请看大松鼠的码云

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

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

相关文章

go-zero 实战(5)

引入Prometheus 用 Prometheus 监控应用 1. 用 docker 启动 Prometheus 编辑配置位置&#xff0c;我将 prometheus.yaml 和 targets.json 文件放在了 /opt/prometheus/conf目录下 prometheus.yaml global:scrape_interval: 15s # 抓取间隔evaluation_interval: 15s # 评估…

宝塔Linux下安装EMQX服务并设置匿名访问

简述 之前有在Windows和Linux下搭建过EMQX服务并且使用方面都没问题,但那都是使用的用户和密码方式访问,且前提都是通过浏览器进入EMQX的配置页面设置的属性; 但这次使用的是腾讯云租用的宝塔Liniux,由于没有浏览器只能通过命令行方式修改EMQX配置以达到目的;由于事先没看…

Android studio关闭自动更新

Windows下&#xff1a; 左上角file - setting - Appearance & Behavier - system setting - update - 取消勾选

【实战】SpringBoot整合Websocket、Redis实现Websocket集群负载均衡

文章目录 前言技术积累什么是Websocket什么是Redis发布订阅Redis发布订阅与消息队列的区别 实战演示SpringBoot整合WebsoketWebsoket集群负载均衡 实战测试IDEA启动两台服务端配置nginx负载均衡浏览器访问模拟对话 前言 相信很多同学都用过websocket来实现服务端主动向客户端推…

案例题(第二版)

案例题目 信息系统架构设计 基本概念 信息系统架构&#xff08;ISA&#xff09;是对某一特定内容里的信息进行统筹、规划、设计、安排等一系列的有机处理的活动。特点如下 架构是对系统的抽象&#xff0c;它通过描述元素、元素的外部可见属性及元素之间的关系来反映这种抽象…

初识C语言——第二十八天

代码练习1&#xff1a; 用函数的方式实现9*9乘法表 void print_table(int n) {int i 0;int j 0;for (i 1; i< n; i){for (j 1; j< i; j){printf("%d*%d%-3d ", i, j, i * j);}printf("\n");}}int main() {int n 0;scanf("%d", &a…

2024-5-24 石群电路-15

2024-5-24&#xff0c;星期五&#xff0c;22:15&#xff0c;天气&#xff1a;晴&#xff0c;心情&#xff1a;晴。今天最后一天上班&#xff0c;终于要放返校假啦&#xff0c;开心&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;不过放假也不能耽误…

day16|二叉树的属性

相关题目 ● 104.二叉树的最大深度 559.n叉树的最大深度 ● 111.二叉树的最小深度 ● 222.完全二叉树的节点个数 二叉树的深度与高度 如图&#xff0c; 二叉树的深度表示&#xff1a;任意一个叶子节点到根节点的距离&#xff0c;是从上往下计数的&#xff0c;因此使用前序遍历…

忍の摸头之术游戏娱乐源码

本资源提供给大家学习及参考研究借鉴美工之用&#xff0c;请勿用于商业和非法用途&#xff0c;无任何技术支持&#xff01; 忍の摸头之术游戏娱乐源码&#xff0c;抖音上面非常火的摸头杀画面,看得我眼花缭乱,源码拿去玩吧&#xff1b; 目录说明 忍の摸头之术&#xff1a;域…

[牛客网]——C语言刷题day5

答案&#xff1a;D 解析&#xff1a;因为两个指针都指向的字符串常量&#xff0c;不能被重新赋值&#xff0c;*p*q是错误的 在C语言中&#xff0c;赋值语句的返回值都是所赋的值&#xff0c;所以才会有连续赋值的语句&#xff0c;例如ab10&#xff0c;因此&#xff0c;这里的i…

TypeScript-初识

TypeScript 是具有类型语法的JavaScript&#xff0c;是一门强类型的编程语言 变量不能做随意类型赋值 好处&#xff1a; 1️⃣ 静态类型检查&#xff0c;提前发现代码错误 function arrToStr(arr: Array<string>){return arr.join() } arrToStr(123) // 类型“stri…

汇聚荣科技有限公司优点有哪些?

在当今快速发展的科技时代&#xff0c;企业之间的竞争愈发激烈。作为一家专注于科技创新与研发的公司&#xff0c;汇聚荣科技有限公司凭借其卓越的技术实力和创新能力&#xff0c;在业界树立了良好的口碑。那么&#xff0c;汇聚荣科技有限公司究竟有哪些优点呢?接下来&#xf…

基于CentOS7的openGauss5.x极简版安装过程分享

背景&#xff1a;国产信创适配大环境下&#xff0c;安装并体验一下&#xff0c;了解一些数据库适配情况 约束&#xff1a;CentOS Linux release 7.8.2003 (Core) 范围&#xff1a;仅记录上述平台下的简单安装体验过程 目的&#xff1a;节约大家初次体验的时间&#xff0c;为社会…

Python协程的作用

过分揣测别人的想法&#xff0c;就会失去自己的立场。大家好&#xff0c;当代软件开发领域中&#xff0c;异步编程已成为一种不可或缺的技术&#xff0c;用于处理大规模数据处理、高并发网络请求、实时通信等应用场景。而Python协程&#xff08;Coroutine&#xff09;作为一种高…

string类的各个功能函数的底层实现

我们在上一篇文章中详细地讲解了string类的各个常用功能成员函数的讲解&#xff0c;本文我们将对上文进行一个小收尾&#xff0c;然后开始实现string类的底层。 一、上一篇的收尾 1.find函数&#xff1a;顾名思义&#xff0c;它的功能是在字符串中找到目标字符并返回它的位置…

骨传导耳机哪个品牌好?避坑必读精析5大热门款式推荐!

作为一名有着多年工作经验的数码测评师&#xff0c;我发现市场上有许多骨传导耳机品牌都声称自己具有出色的音质和佩戴舒适度。但是&#xff0c;从用户的实际反馈来看&#xff0c;大部分产品都或多或少存在一些问题&#xff0c;如音质失真、佩戴不稳定、耳朵不适等&#xff0c;…

【Pytorch】16.使用ImageFolder加载自定义MNIST数据集训练手写数字识别网络(包含数据集下载)

数据集下载 MINST_PNG_Training在github的项目目录中的datasets中有MNIST的png格式数据集的压缩包 用于训练的神经网络模型 自定义数据集训练 在前文【Pytorch】13.搭建完整的CIFAR10模型我们已经知道了基本搭建神经网络的框架了&#xff0c;但是其中的数据集使用的torchvision…

vue实现加入购物车动效

实现 实现逻辑&#xff1a; 点击添加购物车按钮时&#xff0c;获取当前点击位置event的clientX 、clientY&#xff1b;动态创建移动的小球&#xff0c;动态计算小球需要移动到的位置&#xff08;通过ref 的getBoundingClientRect获取统计元素按钮位置&#xff09;&#xff1b…

JS 网页密码框验证信息

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</title><style>/* 当没有密码…

【错误解决】使用HuggingFaceInstructEmbeddings时的一个错误

起因&#xff1a;使用huggingface构建一个问答程序时出现的问题。 错误内容&#xff1a; 分析&#xff1a; 查看代码发现&#xff0c;HuggingFaceInstructEmbeddings和sentence-transformers模块版本不兼容导致。 可以明显看到方法参数不同。 解决&#xff1a; 安装sentenc…