java图书管理系统(简易)

实现的基本功能:

登录时,需要输入姓名,然后选择作为管理者还是普通用户。选择成功后选择想要实现的功能。管理者的目录下方有有五个功能,而普通用户有4个功能,如下图

首先我们要建立Book这个类,里面包含书本的详细信息

public class Book {private String name;private String author;private int price;private String type;private boolean isLend;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 isLend() {return isLend;}public void setLend(boolean lend) {isLend = lend;}public String toString() {return "book{" +"书名 " + name + "  " +"作者 " + author + "  " +"价格 " + price +"类型 " + type + "  " +"是否借出 " + (isLend?"已借出":"未借出") +'}';}}

要设置一个书架,里面建立一个数组来放书:

 private Book[] books=new Book[10];private int usedSize;public BookList() {books[0]=new Book("三国演义","罗贯中",10,"小说");books[1]=new Book("西游记","吴承恩",12,"小说");books[2]=new Book("红楼梦","曹雪芹",11,"小说");this.usedSize =3;}public int getUsedSize() {return usedSize;}public void setUsedSize(int usedSize) {this.usedSize = usedSize;}public Book[] getBooks() {return books;}public void setBooks(Book[] books) {this.books = books;}public Book getBook(int pos){return books[pos];}public void setBook(Book book,int pos){books[pos]=book;}public boolean isFull(){if(this.usedSize==books.length){return true;}else {return false;}}
}

设置主函数:

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

面对的有两类人,一类为普通用户,另一类为管理者,建立两个类,他们均继承User这个类,因为这个User这个类不用实现对象,所以为一个抽象类

public abstract class User  {private String name;IOperation [] iOperations=new IOperation[]{};public User(String name) {this.name = name;}public  abstract int menu();public void doOperation(int choice, BookList bookList){this.iOperations[choice].work(bookList);}}
public class AdminUser extends User{public AdminUser(String name) {super(name);this.iOperations=new IOperation[]{new ExitSystem(),new FindBook(),new AddBook(),new DelBook(),new ShowBook()};}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;}
}
public class NormalUser extends User {public NormalUser(String name) {super(name);this.iOperations=new IOperation[]{new ExitSystem(),new FindBook(),new LendBook(),new ReturnBook()};}public int  menu(){System.out.println("********普通用户菜单*********");System.out.println("1,查找图书");System.out.println("2,借阅图书");System.out.println("3,归还图书");System.out.println("0,退出系统");Scanner scanner=new Scanner(System.in);int choice=scanner.nextInt();return choice;}}

实现各个功能:

public interface IOperation {void work(BookList bookList);
}
新增图书
public class AddBook  implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("新增图书");if (bookList.isFull()==true){System.out.println("满了不能放");return;}System.out.println("请输入新增图书的书名:");Scanner scanner=new Scanner(System.in);String bookName=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(bookName,author,price,type);int current=bookList.getUsedSize();bookList.setBook(book,current);bookList.setUsedSize(current+1);System.out.println("新增图书成功");}
}
删除图书
public class DelBook  implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("删除图书");System.out.println("请输入要删除的书名:");Scanner scanner=new Scanner(System.in);String bookName=scanner.nextLine();int currentSize= bookList.getUsedSize();int pos=-1;int i = 0;for (; i <currentSize ; i++) {Book book=bookList.getBook(i);if(book.getName().equals(bookName)){System.out.println("找到这本书,开始删除");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(book,j);}bookList.setUsedSize(currentSize-1);bookList.setBook(null,currentSize-1);System.out.println("删除成功");}
}
退出系统
public class ExitSystem implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("退出系统");for (int i = 0; i < bookList.getUsedSize(); i++) {bookList.setBook(null,i);}System.exit(0);}
}
查找图书
public class FindBook  implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("查找图书");System.out.println("请输入要查找的书名:");Scanner scanner=new Scanner(System.in);String bookName=scanner.nextLine();int currentSize= bookList.getUsedSize();for (int i = 0; i <currentSize ; i++) {Book book=bookList.getBook(i);if(book.getName().equals(bookName)){System.out.println("找到了这本书");System.out.println(book);return;}}System.out.println("没有找到这本书");}
}
借书
public class LendBook  implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("借书");System.out.println("请输入要借阅的书名:");Scanner scanner=new Scanner(System.in);String bookName=scanner.nextLine();int currentSize= bookList.getUsedSize();for (int i = 0; i <currentSize ; i++) {Book book=bookList.getBook(i);if(book.getName().equals(bookName)){book.setLend(true);System.out.println("借阅成功");return;}}System.out.println("借阅失败");}
}
归还图书
public class ReturnBook  implements IOperation{@Overridepublic void work(BookList bookList) {System.out.println("ReturnBook");System.out.println("请输入要归还的书名:");Scanner scanner=new Scanner(System.in);String bookName=scanner.nextLine();int currentSize= bookList.getUsedSize();for (int i = 0; i <currentSize ; i++) {Book book=bookList.getBook(i);if(book.getName().equals(bookName)){book.setLend(false);System.out.println("归还成功");return;}}System.out.println("归还失败");}
}
显示图书
public class ShowBook  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);}}
}

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

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

相关文章

sqlite跨数据库复制表

1.方法1 要将 SQLite 数据库中的一个表复制到另一个数据库&#xff0c;您可以按照以下步骤操作&#xff1a; 备份原始表的SQL定义和数据&#xff1a; 使用 sqlite3 命令行工具或任何SQLite图形界面工具&#xff0c;您可以执行以下SQL命令来导出表的SQL定义和数据&#xff1a…

开发过程中PostgreSQL常用的SQL语句,持续更新ing

修改字段类型 -- ALTER TABLE 模式名.表明 ALTER COLUMN 字段名 TYPE 类型; alter table alarm.alarm_produce_config alter column alarm_level type int4;重置序列值 -- ALTER SEQUENCE 序列名 RESTART WITH 序列值; alter sequence enterprise_type_id_seq restart with 1…

腾讯云轻量8核16G18M服务器多少钱一年?

腾讯云轻量8核16G18M服务器多少钱一年&#xff1f;优惠价格4224元15个月&#xff0c;买一年送3个月。配置为轻量应用服务器、16核32G28M、28M带宽、6000GB月流量、上海/广州/北京、380GB SSD云硬盘。 腾讯云服务器有两个活动&#xff0c;一个是官方的主会场入口&#xff0c;还…

算法打卡day19

今日任务&#xff1a; 1&#xff09;235. 二叉搜索树的最近公共祖先 2&#xff09;701.二叉搜索树中的插入操作 3&#xff09;450.删除二叉搜索树中的节点 235. 二叉搜索树的最近公共祖先 题目链接&#xff1a;235. 二叉搜索树的最近公共祖先 - 力扣&#xff08;LeetCode&…

kafka-eagle 配置文件修改使用自带的数据库

###################################### multi zookeeper & kafka cluster list Settings prefixed with ‘kafka.eagle.’ will be deprecated, use ‘efak.’ instead ###################################### efak.zk.cluster.aliascluster1 #cluster1.zk.listip1:…

javaScript【2】

在 JavaScript 中&#xff0c;let、var 和 const 是用于声明变量的关键字&#xff0c;它们之间有一些区别。 var&#xff1a;var 是 JavaScript 最早引入的变量声明关键字。使用 var 声明的变量具有函数作用域&#xff0c;意味着变量在声明它的函数内可见。如果在函数内部使用…

Adobe推出20多个,企业版生成式AI定制、微调服务

3月27日&#xff0c;全球多媒体领导者Adobe在拉斯维加斯召开“Summit 2024”大会&#xff0c;重磅推出了Firefly Services。 Firefly Services提供了20 多个生成式AI和创意API服务&#xff0c;支持企业自有数据对模型进行定制、微调&#xff0c;同时可以与PS、Illustrator、Ex…

华为开源自研AI框架昇思MindSpore应用案例:梯度累加

目录 一、环境准备1.进入ModelArts官网2.使用CodeLab体验Notebook实例 二、案例实现 梯度累加的训练算法&#xff0c;目的是为了解决由于内存不足&#xff0c;导致Batch size过大神经网络无法训练&#xff0c;或者网络模型过大无法加载的OOM&#xff08;Out Of Memory&#xff…

Learn OpenGL 26 视差贴图

什么是视差贴图 视差贴图(Parallax Mapping)技术和法线贴图差不多&#xff0c;但它有着不同的原则。和法线贴图一样视差贴图能够极大提升表面细节&#xff0c;使之具有深度感。它也是利用了视错觉&#xff0c;然而对深度有着更好的表达&#xff0c;与法线贴图一起用能够产生难…

uniapp写小程序如何实现分包

众所众知小程序上传的过程中对包的大小有限制&#xff0c;正常情况下不允许当个包超过2M&#xff0c;所以需要分包 需要再pages.json这个文件夹中进行配置 "pages": [{"path": "pages/index/index","style": {"navigationBarTit…

备考ICA----Istio实验11---为多个主机配置TLS Istio Ingress Gateway实验

备考ICA----Istio实验11—为多个主机配置TLS Istio Ingress Gateway实验 1. 部署应用 kubectl apply -f istio/samples/helloworld/helloworld.yaml -l servicehelloworld kubectl apply -f istio/samples/helloworld/helloworld.yaml -l versionv12. 证书准备 接上一个实验…

Hive常用函数之字符串处理

Hive常用函数之字符串处理 以下是Hive中常用的字符串处理函数&#xff0c;可用于执行各种字符串处理转换操作。 1. CONCAT()&#xff1a;将多个字符串连接在一起。 SELECT CONCAT(Hello, World); -- Output: HelloWorld2. SUBSTR()&#xff1a;从字符串中提取子字符串&#xf…

【备忘录】Linux系统安全限制:禁用或限制用户ssh登录

查看失败的IP iplist$(/bin/lastb |awk {print $3}|sort|uniq -c|awk {if ($1>1500) print $2} | grep -v "^10.0.0.") 并将ip追加到黑名单 for ip in ${iplist} doecho ALL: ${ip} >> /etc/hosts.deny# sshd 失败ip追加到黑名单echo "sshd:112.192.…

计算机网络:物理层 - 信道复用

计算机网络&#xff1a;物理层 - 信道复用 频分复用时分复用统计时分复用波分复用码分复用 计算机网络中&#xff0c;用户之间通过信道进行通信&#xff0c;但是信道是有限的&#xff0c;想要提高网络的效率&#xff0c;就需要提高信道的利用效率。因此计算机网络中普遍采用信道…

笔记本作为其他主机显示屏(HDMI采集器)

前言&#xff1a; 我打算打笔记本作为显示屏来用&#xff0c;连上工控机&#xff0c;这不是贼方便吗 操作&#xff1a; 一、必需品 HDMI采集器一个 可以去绿联买一个&#xff0c;便宜的就行&#xff0c;我的大概就长这样 win10下载 PotPlayer 软件 下载链接&#xff1a;h…

【微服务篇】深入理解微服务网关原理以及Spring Gateway

微服务网关的作用 微服务网关在微服务架构中扮演着至关重要的角色&#xff0c;它主要负责请求的路由、组成服务间的通信桥梁、聚合不同服务的数据以及提供跨服务的统一认证机制。以下是微服务网关的几个主要作用&#xff1a; 请求路由: 微服务网关充当所有入站请求的入口点&a…

PHP - ZipArchive上传、下载实例

概述 在很多实际生产场景都需要批量上传、下载一些文件的处理&#xff0c;整理了使用PHP语言操作ZipArchive实践和实例&#xff0c;ZipArchive需要服务器上安装zlib库&#xff0c;php扩展中安装zip扩展。 服务器环境扩展 ZipArchive类库的PHP版本要求如下&#xff0c;另外ph…

ClickHouse11-ClickHouse中文件引擎与物化视图的组合拳

全文概览&#xff1a; 什么是物化视图 使用场景 如何实现这个需求 建立一个使用表引擎的表&#xff0c;作为物化视图的目标表确定需要查询的SQL创建物化视图测试 文件引擎其实是一个不常用的特殊表引擎&#xff0c;结合【ClickHouse09-表引擎之文件引擎】一章节的基础介绍 这…

Flutter 常用插件Plugin整理并附带实例

最近有点空闲时间&#xff0c;正好写一篇文章&#xff0c;整理一下我们在Flutter开发中常用的插件Plugin使用并附带上实例。 在日常开发中&#xff0c;整个demo目前应该满足大家所有的开发需求&#xff0c;例如&#xff1a;http请求、列表刷新及加载、列表分组、轮播图、视频播…

AI浪潮席卷游戏业:未来5~10年,游戏或将由AI生成

一年前&#xff0c;因为AI失业的第一批人&#xff0c;在游戏行业出现了。游戏原画、翻译等外包团队开始遭遇砍单&#xff0c;AI绘画工具的发展速度和水平已经几乎可以媲美科班出身、初级经验的人类画师。 一年时间过去&#xff0c;在游戏制作的毛细血管中&#xff0c;越来越多…