图书管理系统—Java

文章目录

  • 1.书类
  • 2.图书列表
  • 3.新增图书
  • 4.借阅图书
  • 5.删除图书
  • 6.显示图书
  • 7.退出系统
  • 8.查找图书
  • 9.接口
  • 10.归还图书
  • 11.管理员菜单
  • 12.普通用户菜单
  • 13.用户类
  • 14.主函数

1.书类

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

2.图书列表

package book;public class BookList {public Book[] bookList = new Book[10];private int usedSize;//当前书架上书的数目public BookList() {bookList[0] = new Book("三国演义","罗贯中",18,"小说");bookList[1] = new Book("水浒传","施耐庵",48,"小说");bookList[2] = new Book("西游记","吴承恩",28,"小说");this.usedSize = 3;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}//把书放到pos位置public void setBookList(int pos,Book book) {bookList[pos] = book;}//获取pos位置的书public Book getBook(int pos) {return bookList[pos];}//这里本质上是可以写借阅书籍等操作的
}

3.新增图书

package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class AddOperation implements IOperation {public void work(BookList bookList) {System.out.println("新增图书!");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("输入你要查找新增书籍的价格:");int price = scanner.nextInt();Book book = new Book(name,author,price,type);int currentSize = bookList.getUsedSize();bookList.setBookList(currentSize,book);bookList.setUsedSize(currentSize + 1);//放入一本书System.out.println("新增成功");}
}

4.借阅图书

package operation;import book.Book;
import book.BookList;import java.util.Scanner;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.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)) {book.setBorrowed(true);System.out.println("借阅成功,借阅书籍信息如下:");System.out.println(book);return;}}System.out.println("借阅失败,没有这本书籍!");}
}

5.删除图书

package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class DelOperation 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.getUsedSize();int index = -1;for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)) {index = i;break;}}//如果index==-1,说明没有这本书if (index == -1) {System.out.println("没有你要删除的图书");return;}//从这里开始删除for (int i = 0; i < currentSize-1; i++) {Book book = bookList.getBook(i + 1);bookList.setBookList(i,book);}bookList.setUsedSize(currentSize - 1);}
}

6.显示图书

package operation;import book.Book;
import book.BookList;public class DisplayOperation implements IOperation {public 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);}}
}

7.退出系统

package operation;import book.BookList;public class ExitOperation implements IOperation{public void work(BookList bookList){//可以对书架进行手动清空System.out.println("退出系统!");System.exit(0);}
}

8.查找图书

package operation;import book.Book;
import book.BookList;import java.util.Scanner;public class FindOperation implements IOperation {public void work(BookList bookList) {System.out.println("查找图书!");Scanner scanner = new Scanner(System.in);System.out.println("输入你要查找的书籍:");String name = scanner.nextLine();int size = bookList.getUsedSize();for (int i = 0; i < size; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)) {System.out.println("找到了这本书");System.out.println(book);return;}}System.out.println("没有这本书");}
}

9.接口

package operation;import book.BookList;public interface IOperation {void work(BookList bookList);
}

10.归还图书

package operation;import book.Book;
import book.BookList;import java.util.Scanner;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.getUsedSize();for (int i = 0; i < currentSize; i++) {Book book = bookList.getBook(i);if (book.getName().equals(name)) {book.setBorrowed(false);System.out.println("归还成功,归还书籍信息如下:");System.out.println(book);return;}}System.out.println("归还失败,没有这本书籍!");}
}

11.管理员菜单

package user;import operation.*;import java.util.Scanner;public class AdiminUser extends User {public AdiminUser(String name) {super(name);this.iOperations = new IOperation[] {new ExitOperation(),new FindOperation(),new AddOperation(),new DelOperation(),new DisplayOperation()};}public int menu() {System.out.println("==============管理员菜单===========");System.out.println("hello "+this.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;}
}

12.普通用户菜单

package user;import operation.*;import java.util.Scanner;public class NormaUser extends User {public NormaUser(String name) {super(name);this.iOperations = new IOperation[] {new ExitOperation(),new FindOperation(),new BorrowOperation(),new ReturnOperation()};}public int menu() {System.out.println("==============普通用户菜单===========");System.out.println("hello "+this.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;}
}

13.用户类

package user;import book.BookList;
import operation.IOperation;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) {this.iOperations[choice].work(bookList);}
}

14.主函数

import book.BookList;
import user.AdiminUser;
import user.NormaUser;
import user.User;import java.util.Scanner;public class Main {public static User login() {System.out.println("请输入你的姓名:");Scanner scanner = new Scanner(System.in);String name  = scanner.nextLine();System.out.println("请输入你的身份:1:管理员 0:普通用户");int choice = scanner.nextInt();if (choice == 1) {return new AdiminUser(name);}else{return new NormaUser(name);}}public static void main(String[] args) {BookList bookList = new BookList();User user = login();//User user = AdiminUser(name);//User user = NormaUser(name);while (true){int choice = user.menu();user.doOperation(choice,bookList);}}
}

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

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

相关文章

【AIGC】图片生成的原理与应用

前言 近两年 AI 发展非常迅速&#xff0c;其中的 AI 绘画也越来越火爆&#xff0c;AI 绘画在很多应用领域有巨大的潜力&#xff0c;AI 甚至能模仿各种著名艺术家的风格进行绘画。 目前比较有名商业化的 AI 绘画软件有 Midjourney、DALLE2、以及百度出品的文心一格&#xff1a;…

MinGW-W64 下载、安装与配置(支持最新版的GCC,目前 GCC 13.2.0)VSCode配置c/c++环境 彻底删除vscode(包括插件及配置!)

目录 一、简介 二、下载 1 旧版安装&#xff08;8.1.0&#xff09; 从 sourceforge.net 下载 2 新版安装(本次采用较新版本~~~) 从 github 下载 从 镜像站点 下载 自己编译 三、安装与配置 1. 在线安装&#xff08;这里仅作参考了解&#xff09; 2. 离线安装&…

异步FIFO设计的仿真与综合技术(3)

概述 本文主体翻译自C. E. Cummings and S. Design, “Simulation and Synthesis Techniques for Asynchronous FIFO Design 一文&#xff0c;添加了笔者的个人理解与注释&#xff0c;文中蓝色部分为笔者注或意译。前文链接&#xff1a; 异步FIFO设计的仿真与综合技术&#xf…

自动化测试(五):自动化测试框架的搭建和基于yaml热加载的测试用例的设计

该部分是对自动化测试专栏前四篇的一个补充&#xff0c;本次参考以下文章实现一个完整的谷歌翻译接口自动化测试:   [1]【python小脚本】Yaml配置文件动态加载   [2]【python做接口测试的学习记录day8——pytest自动化测试框架之热加载和断言封装】 目标&#xff1a;框架封…

ununtu中vim的使用

插入命令 i&#xff1a;表示输入 退出命令 :w - 保存文件&#xff0c;不退出 vim :w file -将修改另外保存到 file 中&#xff0c;不退出 vim :w! -强制保存&#xff0c;不退出 vim :wq -保存文件&#xff0c;退出 vim :wq! -强制保存文件&#xff0c;退出 vim …

新增动态排序图、桑基图、AntV组合图,DataEase开源数据可视化分析平台v1.18.10发布

2023年9月14日&#xff0c;DataEase开源数据可视化分析平台正式发布v1.18.10版本。 这一版本的功能升级包括&#xff1a;数据集方面&#xff0c;对字段管理的后台保存做了相关优化&#xff0c;降低了资源消耗&#xff1b;仪表板方面&#xff0c;对联动、查询结果以及过滤组件等…

系统架构:软件工程速成

文章目录 参考概述软件工程概述软件过程 可行性分析可行性分析概述数据流图数据字典 需求分析需求分析概述ER图状态转换图 参考 软件工程速成(期末考研复试软考)均适用. 支持4K 概述 软件工程概述 定义&#xff1a;采用工程的概念、原理、技术和方法来开发与维护软件。 三…

GET,POST,DELETE,PUT参数传递的形式

一.get请求参数在地址后面进行拼接 1.代码&#xff1a; <template><div class""><button click"fn">点击</button></div> </template><script> import axios from "axios"; //安装完之后&#xff0…

Spring MVC 八 - 内置过滤器

SpringMVC内置如下过滤器&#xff1a; Form DataForwarded HeadersShallow ETagCORS Form Data 浏览器可以通过HTTP GET或HTTP POST提交form data&#xff08;表单数据&#xff09;&#xff0c;但是非浏览器客户端可以通过HTTP PUT、HTTP DELETE、HTTP PATCH提交表单数据。但…

MFC中嵌入显示opencv窗口

在MFC窗体中建立一个Picture Control控件,用于显示opencv窗口 在属性中设置图片控件的资源ID为IDC_PIC1 主要的思路: 使用GetWindowRect可以获取图片控件的区域 使用cv::resizeWindow可以设置opencv窗口的大小,适合图片控件的大小 使用cvGetWindowHandle函数可以获取到ope…

conn,cur=DBHelper.connDB(DBconfig.A)是什么意思

这段代码是 Python 中调用 DBHelper.connDB() 方法的语句&#xff0c;并将其返回的连接对象和游标对象赋值给变量 conn 和 cur。 假设 DBHelper 是一个 Python 类&#xff0c;而 connDB() 是类 DBHelper 中的一个静态方法&#xff08;类方法&#xff09;&#xff0c;用于建立数…

OpenCV(四十二):Harris角点检测

1.Harris角点介绍 什么是角点&#xff1f; 角点指的是两条边的交点&#xff0c;图中红色圈起来的点就是角点。 Harris角点检测原理&#xff1a;首先定义一个矩形区域&#xff0c;然后将这个矩形区域放置在我的图像中&#xff0c;求取这个区域内所有的像素值之和&#xff0c;之…

ARTS第6周T:设计模式——外观模式(Facade模式)

下面是一个基于Java语言的Facade模式示例&#xff1a; java // 这个接口是客户端所看到的&#xff0c;也就是外观 public interface ICalculator { int add(int a, int b); int subtract(int a, int b); } // 这个类实现了上面的接口&#xff0c;也就是具体子系统的一…

麒麟v10安装mysql(ARM架构)

下载MYSQL安装包 华为开源镜像站_软件开发服务_华为云 上面的选择一个下载 或者用命令下载 wget https://repo.huaweicloud.com/kunpeng/yum/el/7/aarch64/Packages/database/mysql-5.7.27-1.el7.aarch64.rpm 检查是否已经安装MySQL rpm -qa | grep mysql将包卸载掉 rpm -…

vue之封装tab类组件

vue之封装tab类组件 vue之封装tab类组件CSS样式方面JS方面 vue之封装tab类组件 需求&#xff1a;点击【上一步】、【下一步】按钮&#xff0c;切换tab&#xff0c;且有淡入浅出的动画。 CSS样式方面 <div class"parent"><div class"childDiv" id…

图论第三天|130. 被围绕的区域、417. 太平洋大西洋水流问题、827. 最大人工岛

130. 被围绕的区域 文档讲解 &#xff1a;代码随想录 - 130. 被围绕的区域 状态&#xff1a;开始学习。 思路&#xff1a; 步骤一&#xff1a; 深搜或者广搜将地图周边的 ‘O’ 全部改成 ’A’&#xff0c;如图所示&#xff1a; 步骤二&#xff1a; 再遍历地图&#xff0c;将 …

JWT 安全及案例实战

文章目录 一、JWT (json web token)安全1. Cookie&#xff08;放在浏览器&#xff09;2. Session&#xff08;放在服务器&#xff09;3. Token4. JWT (json web token)4.1 头部4.1.1 alg4.1.2 typ 4.2 payload4.3 签名4.4 通信流程 5. 防御措施 二、漏洞实例&#xff08;webgoa…

API原理概念篇(六)玩转正则表达式等常用API

一 玩转正则表达式等常用API ① 正则 1、openresty存在两套正则表达式规范1) lua自身独有的正则规范 备注&#xff1a;大约有5%&#xff5e;15%性能损耗损耗原因&#xff1a;表达式compile成pattern,并不会被缓存,每次都会被重新compile编译2) nginx的符合POSIX规范的PCR…

集合减法【新思路】

#include<stdio.h> int main() {int n,m,flag0;int x;int a[100001]{0},b[100001]{0};scanf("%d %d",&n,&m);以集合A所有元素作为数组下标映射值成1 for (int i 0; i < n; i) {scanf("%d", &x);a[x] 1; }以集合B所有元素作为数组下…

生产消费者模型的介绍以及其的模拟实现

目录 生产者消费者模型的概念 生产者消费者模型的特点 基于阻塞队列BlockingQueue的生产者消费者模型 对基于阻塞队列BlockingQueue的生产者消费者模型的模拟实现 ConProd.c文件的整体代码 BlockQueue.h文件的整体代码 对【基于阻塞队列BlockingQueue的生产者消费者模型…