当前位置: 首页 > news >正文

Java之图书管理系统

🤷‍♀️🤷‍♀️🤷‍♀️ 今天给大家分享一下Java实现一个简易的图书管理系统!

清风的个人主页🎉✏️✏️ 

🌂c/java领域新星创作者

🎉欢迎👍点赞✍评论❤️收藏

😛😛😛希望我的文章能对你有所帮助,有不足的地方还请各位看官多多指教,大家一起学习交流!

动动你们发财的小手,点点关注点点赞!在此谢过啦!哈哈哈!😛😛😛


目录

 一、找到抽象化的对象

1.书类

2.书架类

二、管理员与普通用户登录

三、实现的功能

1.查找图书

2.新增图书(管理员功能)

3.删除图书(管理员功能)

4.显示图书信息

5.退出系统

6.借阅图书(普通用户功能)

7.归还图书(普通用户功能)

四、main方法



图书管理系统源码链接-满船清梦压星河的Gitee

 

 一、找到抽象化的对象

1.书类

经过分析,我们可以知道,书可以抽象成一个类型。它的属性包括:书名,作者,价格,书的类型等等...我们就先以这些为例。为了保持封装性,我们把这些属性都设置成private修饰的。

下面是书类的定义代码:
这段代码包括一些构造函数以及设置书的属性、重写String函数等。

public class Book {private String name;private String author;private int price;private String type;private boolean isBorrowed;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;}public boolean isBorrowed() {return isBorrowed;}public void setBorrowed(boolean borrowed) {isBorrowed = borrowed;}public Book(String name, String author, int price, String type) {this.name = name;this.author = author;this.price = price;this.type = type;}@Overridepublic String toString() {return "Book{" +"name='" + name + '\'' +", author='" + author + '\'' +", price=" + price +", type='" + type + '\'' +((isBorrowed==true)?"已借出!":"未借出!") +'}';}
}

2.书架类

我们可以利用一个数组来存放这些书籍,并记录当前存放书籍的数量,为后续的增删查改做准备,同时初始化有三本书籍。

下面是代码:

public class BookList {private Book[] books;private int usedSize;//记录当前书架上实际存放的书的数量public BookList(){this.books=new Book[10];this.books[0]=new Book("三国演义","罗贯中",18,"小说");this.books[1]=new Book("西游记","吴承恩",28,"小说");this.books[2]=new Book("红楼梦","曹雪芹",35,"小说");this.usedSize=3;}//获取当前存放书籍数量public int getUsedSize() {return usedSize;}//设置存放书籍数量public void setUsedSize(int usedSize) {this.usedSize = usedSize;}//返回下标为pos的书籍public Book getBook(int pos){return books[pos];}//设置下标为pos位置的书籍为bookpublic void setBook(int pos,Book book){books[pos]=book;}//返回书籍这个数组public Book[] getBooks(){return books;}
}

二、管理员与普通用户登录

首先定义一个用户抽象类,再定义管理员与普通用户去继承抽象类并重写菜单方法。

下面是用户抽象类代码:

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

管理员类代码:

public class AdiminUser extends User{public AdiminUser(String name){super(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("*********************");Scanner scanner=new Scanner(System.in);System.out.println("请输入你的选择:>");int choice=scanner.nextInt();return choice;}
}

普通用户类代码:
 

public class NormalUser extends User{public NormalUser(String name){super(name);this.ioPerations=new IOPeration[]{new ExitOperation(),new FindOperation(),new BorrowedOperation(),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);System.out.println("请输入你的选择:>");int choice=scanner.nextInt();return choice;}
}

三、实现的功能

实现以下几个功能,可以定义一个接口,方便后续的相关操作。

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

1.查找图书

public class FindOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("查找图书>:");System.out.println("请输入要查找的书>:");Scanner scanner=new Scanner(System.in);String name=scanner.nextLine();//遍历这个数组int currentSize=bookList.getUsedSize();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("无此书!!!");}
}

2.新增图书(管理员功能)

public class AddOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("新增图书>:");int cunrrentSize=bookList.getUsedSize();if (cunrrentSize==bookList.getBooks().length){System.out.println("书架已满!");return;}Scanner scanner=new Scanner(System.in);System.out.println("输入要新增书籍>:");String name=scanner.nextLine();//检查数组当中有没有这本书for (int i = 0; i <cunrrentSize ; i++) {Book book1=bookList.getBook(i);if(book1.getName().equals(name)){System.out.println("该书已存放,无需新增!!!");return;}}System.out.println("输入书籍作者>:");String author=scanner.nextLine();System.out.println("输入书籍类型>:");String type=scanner.nextLine();System.out.println("输入书籍价格>:");int price=scanner.nextInt();Book book=new Book(name,author,price,type);bookList.setBook(cunrrentSize,book);bookList.setUsedSize(cunrrentSize+1);System.out.println("新增书籍成功!!!");}
}

3.删除图书(管理员功能)

public class AddOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("新增图书>:");int cunrrentSize=bookList.getUsedSize();if (cunrrentSize==bookList.getBooks().length){System.out.println("书架已满!");return;}Scanner scanner=new Scanner(System.in);System.out.println("输入要新增书籍>:");String name=scanner.nextLine();//检查数组当中有没有这本书for (int i = 0; i <cunrrentSize ; i++) {Book book1=bookList.getBook(i);if(book1.getName().equals(name)){System.out.println("该书已存放,无需新增!!!");return;}}System.out.println("输入书籍作者>:");String author=scanner.nextLine();System.out.println("输入书籍类型>:");String type=scanner.nextLine();System.out.println("输入书籍价格>:");int price=scanner.nextInt();Book book=new Book(name,author,price,type);bookList.setBook(cunrrentSize,book);bookList.setUsedSize(cunrrentSize+1);System.out.println("新增书籍成功!!!");}
}

4.显示图书信息

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

5.退出系统

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

6.借阅图书(普通用户功能)

public class BorrowedOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("借阅图书>:");/*** 1.你要借阅哪本书?* 2.你借阅的书存在吗?* 借阅的方式是什么?*/Scanner scanner=new Scanner(System.in);System.out.println("输入要借阅书籍>:");String name=scanner.nextLine();int currentSize=bookList.getUsedSize();int i = 0;for (; i <currentSize ; i++) {Book book=bookList.getBook(i);if(book.getName().equals(name)){book.setBorrowed(true);System.out.println("借阅成功!!!");return;}}if(i==currentSize){System.out.println("该书不存在,无法借阅!!!");}}
}

7.归还图书(普通用户功能)

public class ReturnOperation implements IOPeration{@Overridepublic void work(BookList bookList) {System.out.println("归还图书>:");Scanner scanner=new Scanner(System.in);System.out.println("输入要归还书籍>:");String name=scanner.nextLine();int currentSize=bookList.getUsedSize();int i = 0;for (; i <currentSize ; i++) {Book book=bookList.getBook(i);if(book.getName().equals(name)){book.setBorrowed(false);System.out.println("归还成功!!!");return;}}if(i==currentSize){System.out.println("该书不存在,无需归还!!!");}}
}

四、main方法

public class Main {public static User login() {System.out.println("请输入你的姓名:>");Scanner scanner = new Scanner(System.in);String name = scanner.nextLine();System.out.println("请输入你的身份:> 1.管理员  2.普通用户");int choice = scanner.nextInt();if (choice == 1) {//管理员return new AdiminUser(name);} else {//普通用户return new NormalUser(name);}}public static void main(String[] args) {BookList bookList = new BookList();//user指向哪个对象,就看返回值是什么User user = login();while (true) {int choice = user.menu();System.out.println("choice:" + choice);//根据choice决定调用的是哪个方法user.doOperation(choice, bookList);}}
}

🎉好啦,今天的分享就到这里!!

创作不易,还希望各位大佬支持一下!

👍点赞,你的认可是我创作的动力!

收藏,你的青睐是我努力的方向!

✏️评论:你的意见是我进步的财富!

 

相关文章:

Java之图书管理系统

&#x1f937;‍♀️&#x1f937;‍♀️&#x1f937;‍♀️ 今天给大家分享一下Java实现一个简易的图书管理系统&#xff01; 清风的个人主页&#x1f389;✏️✏️ &#x1f302;c/java领域新星创作者 &#x1f389;欢迎&#x1f44d;点赞✍评论❤️收藏 &#x1f61b;&…...

用「埋点」记录自己,不妄过一生

最近有朋友问我「埋点怎么做」&#xff0c;给朋友讲了一些互联网广告的案例&#xff0c;从源头的数据采集讲到末尾的应用分析和流量分配等&#xff08;此处省略N多字&#xff09; 解释完以后&#xff0c;我想到一个问题&#xff1a;有了埋点可以做分析&#xff0c;那我们对自己…...

运维知识点-Docker从小白到入土

Docker从小白到入土 安装问题-有podmanCentos8使用yum install docker -y时&#xff0c;默认安装的是podman-docker软件 安装docker启动dockeryum list installed | grep dockeryum -y remove xxxx安装Docker安装配置下载安装docker启动docker&#xff0c;并设置开机启动下载所…...

基于DevEco Studio的OpenHarmony应用原子化服务(元服务)入门教程

一、创建项目 二、创建卡片 三、应用服务代码 Index.ets Entry Component struct Index {State TITLE: string OpenHarmony;State CONTEXT: string 创新召见未来&#xff01;;build() {Row() {Column() {Text(this.TITLE).fontSize(30).fontColor(0xFEFEFE).fontWeight(…...

MySQL和Java程序建立连接的底层原理(JDBC),一个SQL语句是如何执行的呢?

Java程序方面 1. JDBC驱动程序&#xff1a;JDBC驱动程序是连接MySQL数据库的核心组件。它是一组Java类&#xff0c;用于实现与MySQL数据库的通信协议和数据传输。驱动程序负责将Java程序发送的请求转化为MySQL数据库能够理解的格式&#xff0c;并将数据库返回的结果转化为Java…...

uniapp踩坑之项目:uniapp数字键盘组件—APP端

//在components文件夹创建digitKeyboard文件夹&#xff0c;再创建digitKeyboard.vue <!-- 数字键盘 --> <template><view class"digit-keyboard"><view class"digit-keyboard_bg" tap"hide"></view><view clas…...

聊一聊GPT——让我们的写作和翻译更高效

1 介绍 GPT&#xff08;Generative Pre-trained Transformer&#xff09;是一种基于Transformer的语言生成模型&#xff0c;由OpenAI开发。它采用了无监督的预训练方式&#xff0c;通过处理大量的文本数据进行自我学习&#xff0c;从而提高其语言生成的能力。 GPT在自然语言…...

413 (Payload Too Large) 2023最新版解决方法

文章目录 出现问题解决方法 出现问题 博主在用vue脚手架开发的时候&#xff0c;在上传文件的接口中碰到 这样一个错误&#xff0c;查遍所有csdn&#xff0c;都没有找到解决方法&#xff0c;通过一些方式&#xff0c;终于解决了。 解决方法 1.打开Vue项目的根目录。 2.在根目…...

uboot启动linux kernel的流程

目录 前言流程图autoboot_commandrun_command_listdo_bootmdo_bootm_statesdo_bootm_linuxboot_prep_linuxboot_jump_linux 前言 本文在u-boot启动流程分析这篇文章的基础上&#xff0c;简要梳理uboot启动linux kernel的流程。 流程图 其中&#xff0c; autoboot_command位于…...

垃圾回收系统小程序定制开发搭建攻略

在这个数字化快速发展的时代&#xff0c;垃圾回收系统的推广对于环境保护和可持续发展具有重要意义。为了更好地服务于垃圾回收行业&#xff0c;本文将分享如何使用第三方制作平台乔拓云网&#xff0c;定制开发搭建垃圾回收系统小程序。 首先&#xff0c;使用乔拓云网账号登录平…...

可变参数模板

1. sizeof...计算参数个数 template<typename... Ts> void magic(Ts... args) {std::cout << sizeof...(args) << std::endl; } 2.递归模板函数 template<typename T> void printf1(T value) {std::cout << value << std::endl; }templ…...

坐公交:内外向乘客依序选座(python字典、字符串、元组)

n排宽度不一的座位&#xff0c;每排2座&#xff0c;2n名内外向乘客依序上车按各自喜好选座。 (笔记模板由python脚本于2023年11月05日 21:49:31创建&#xff0c;本篇笔记适合熟悉python列表list、字符串str、元组tuple的coder翻阅) 【学习的细节是欢悦的历程】 Python 官网&…...

十年老程序员分享13个最常用的Python深度学习库和介绍,赶紧收藏码住!

文章目录 前言CaffeTheanoTensorFlowLasagneKerasmxnetsklearn-theanonolearnDIGITSBlocksdeepypylearn2Deeplearning4j关于Python技术储备一、Python所有方向的学习路线二、Python基础学习视频三、精品Python学习书籍四、Python工具包项目源码合集①Python工具包②Python实战案…...

【pytorch源码分析--torch执行流程与编译原理】

背景 解读torch源码方便算子开发方便后续做torch 模型性能开发 基本介绍 代码库 https://github.com/pytorch/pytorch 模块介绍 aten: A Tensor Library的缩写。与Tensor相关的内容都放在这个目录下。如Tensor的定义、存储、Tensor间的操作&#xff08;即算子/OP&#xff…...

编辑器报警处理

1、warning CS8600: 将 null 文本或可能的 null 值转换为不可为 null 类型。 原代码 string returnedString Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned); 处理后的代码 string returnedString Marshal.PtrToStringAuto(pReturnedString, (int)bytesR…...

Python库学习(十二):数据分析Pandas[下篇]

接着上篇《Python库学习(十一):数据分析Pandas[上篇]》,继续学习Pandas 1.数据过滤 在数据处理中&#xff0c;我们经常会对数据进行过滤&#xff0c;为此Pandas中提供mask()和where()两个函数&#xff1b; mask(): 在 满足条件的情况下替换数据&#xff0c;而不满足条件的部分…...

工具: MarkDown学习

具体内容看官方教程&#xff1a; Markdown官方教程...

JS逆向爬虫---请求参数加密②【某麦数据analysis参数加密】

主页链接: https://www.qimai.cn/rank analysis逆向 完整参数生成代码如下&#xff1a; const {JSDOM} require(jsdom) const dom new JSDOM(<!DOCTYPE html><p>hello</p>) window dom.windowfunction customDecrypt(n, t) {t t || generateKey(); //…...

基于APM(PIX)飞控和missionplanner制作遥控无人车-从零搭建自主pix无人车无人坦克

前面的步骤和无人机调试一样&#xff0c;可以参考无人机相关专栏。这里不再赘述。 1.安装完rover的固件后&#xff0c;链接gps并进行校准。旋转小车不同方向&#xff0c;完成校准&#xff0c;弹出成功窗口。 2.校准遥控器。 一定要确保遥控器模式准确&#xff0c;尤其是使用没…...

Vue3的手脚架使用和组件父子间通信-插槽(Options API)学习笔记

Vue CLI安装和使用 全局安装最新vue3 npm install vue/cli -g升级Vue CLI&#xff1a; 如果是比较旧的版本&#xff0c;可以通过下面命令来升级 npm update vue/cli -g通过脚手架创建项目 vue create 01_product_demoVue3父子组件的通信 父传子 父组件 <template>…...

vscode里如何用git

打开vs终端执行如下&#xff1a; 1 初始化 Git 仓库&#xff08;如果尚未初始化&#xff09; git init 2 添加文件到 Git 仓库 git add . 3 使用 git commit 命令来提交你的更改。确保在提交时加上一个有用的消息。 git commit -m "备注信息" 4 …...

渗透实战PortSwigger靶场-XSS Lab 14:大多数标签和属性被阻止

<script>标签被拦截 我们需要把全部可用的 tag 和 event 进行暴力破解 XSS cheat sheet&#xff1a; https://portswigger.net/web-security/cross-site-scripting/cheat-sheet 通过爆破发现body可以用 再把全部 events 放进去爆破 这些 event 全部可用 <body onres…...

YSYX学习记录(八)

C语言&#xff0c;练习0&#xff1a; 先创建一个文件夹&#xff0c;我用的是物理机&#xff1a; 安装build-essential 练习1&#xff1a; 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件&#xff0c;随机修改或删除一部分&#xff0c;之后…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

镜像里切换为普通用户

如果你登录远程虚拟机默认就是 root 用户&#xff0c;但你不希望用 root 权限运行 ns-3&#xff08;这是对的&#xff0c;ns3 工具会拒绝 root&#xff09;&#xff0c;你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案&#xff1a;创建非 roo…...

论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一)

宇树机器人多姿态起立控制强化学习框架论文解析 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架&#xff08;一&#xff09; 论文解读&#xff1a;交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化…...

在鸿蒙HarmonyOS 5中使用DevEco Studio实现录音机应用

1. 项目配置与权限设置 1.1 配置module.json5 {"module": {"requestPermissions": [{"name": "ohos.permission.MICROPHONE","reason": "录音需要麦克风权限"},{"name": "ohos.permission.WRITE…...

在Ubuntu24上采用Wine打开SourceInsight

1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

视觉slam十四讲实践部分记录——ch2、ch3

ch2 一、使用g++编译.cpp为可执行文件并运行(P30) g++ helloSLAM.cpp ./a.out运行 二、使用cmake编译 mkdir build cd build cmake .. makeCMakeCache.txt 文件仍然指向旧的目录。这表明在源代码目录中可能还存在旧的 CMakeCache.txt 文件,或者在构建过程中仍然引用了旧的路…...

【SSH疑难排查】轻松解决新版OpenSSH连接旧服务器的“no matching...“系列算法协商失败问题

【SSH疑难排查】轻松解决新版OpenSSH连接旧服务器的"no matching..."系列算法协商失败问题 摘要&#xff1a; 近期&#xff0c;在使用较新版本的OpenSSH客户端连接老旧SSH服务器时&#xff0c;会遇到 "no matching key exchange method found"​, "n…...