Java-图书登录系统的实现

news2024/9/29 7:32:36

实现效果
它将面对 管理员 和 普通用户 两种用户来提供服务,并且各自的服务并不相同。


实现思路
一般写项目,每个独立的功能都会写成一个类,而有关联的功能,都会将多个类存放在一个包中,此项目我们将用 3 个包来体现我们的效果

book包
Book类 —> 用来定义一本书
既然是图书系统,那么必然不可能仅仅只有一本书,我们还需要一个书架,来存储书籍 BookList 类
在这里插入图片描述

user包
因为我们有两种用户可以使用这个图书系统,而且每种用户都有自己的特性,所以也需要定义成类
有 User类(后面两个类的共同特征抽取出来的,User类可以是一个抽象类),adminUser类,normalUser类
在这里插入图片描述

opertion包
这个包底下就是我们的基本操作
查询图书,借阅图书,归还图书,退出系统,新增图书,删除图书,显示所有图书
这里我们增加一个操作接口,让所有功能来实现这个接口,从而完善各自的内容。
在这里插入图片描述

Main类
上面的类就已经实现了我们需要的功能了,Main类就是来整合这些操作功能的,在Main类中,我们需要根据登陆的账户类型不同,来显示不同的菜单供其使用图书系统.


book包的实现
创建book类用来存储书的各种信息,BookList类当做书架管理书籍.


book类
Book类中包含五个属性[书名,作者,书的描述,价格,是否被借出]。
一个构造方法(isBorrowed不用放到构造方法中,因为这个属性随着借入和归还一直变化,所以创建setter方法对他进行修改。
重写toString方法,这样打印出来才能看出来书籍信息

public class Book {
    private String name;
    private String author;
    private String describe;
    private double price;
    private boolean isBorrowed=false;

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

    public String getName() {
        return name;
    }

    public String getAuthor() {
        return author;
    }

    public String getDescribe() {
        return describe;
    }

    public double getPrice() {
        return price;
    }

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

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

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

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", describe='" + describe + '\'' +
                ", price=" + price +
                ", isBorrowed=" + isBorrowed +
                '}';
    }
}

BookList类中设置了书的数组,用来存放书。
当前书的数量。
一些方法
获得书架上某个位置的书【getBooks】
修改架上某个位置的书【setBooks】
获取当前存储了书的数量【getUsedSized】
修改当前存储的书的数量【setUsedSized】

public class BookList {
    Book[] books=new Book[5];//这里最多可以放置5本书
    private int usedSized=3;//已存数的数量

    public BookList() {
        books[0]=new Book("红楼梦","曹","小说",11);
        books[0]=new Book("红楼","网","说",1111);
        books[0]=new Book("红","照","小",111111);
    }

    public void setBooks(int pos,Book book) {//设置某个位置的书,用于查找和删除图书
        this.books[pos]=book;
    }

    public void setUsedSized(int usedSized) {
        this.usedSized = usedSized;
    }

    public Book getBooks(int pos) {//获取某个位置的书,返回值为对象
        return this.books[pos];
    }

    public int getUsedSized() {
        return usedSized;
    }
}

user包
用户分为管理员和普通用户,我们抽象出管理员和普通用户的共同点创建User类,然后让管理员和普通用户继承User类,再去创建他们特有的属性或方法。

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类
这里我们需要继承user类,
自己创建构造函数,要用到user类中的name成员变量,并初始化一个数组,这个数组中的成员为当前用户权限下可以进行的操作,成员类型为对象。
重写menu函数;
在这里插入图片描述

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("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 sc=new Scanner(System.in);
        int choice=sc.nextInt();//choice用于选择操作类型
        return choice;
    }
}

NomalUser类
与上述 的AdminiUser类几乎是一样的道理

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("hello"+this.name+"欢迎来到图书系统");
        System.out.println("1 查找图书");
        System.out.println("2 借阅图书");
        System.out.println("3 归还图书");
        System.out.println("0 退出系统");

        System.out.println("请输入你的操作");
        Scanner sc=new Scanner(System.in);
        int choice=sc.nextInt();//choice用于选择操作类型
        return choice;
    }
}

main类
这里我们通过一个login方法提示用户输入姓名和身份类型,利用login方法的返回值返回身份类型,这里发生向上转型
在这里插入图片描述
在这里插入图片描述

public class Main {
    public static User login(){
        System.out.println("请输入你的姓名");
        Scanner sc=new Scanner(System.in);
        String name= sc.nextLine();
        System.out.println("请输入你的身份:1 管理员   0 普通用户");
        int choice=sc.nextInt();
        if(choice==1){
            return new AdminUser(name);//向上转型
        }else{
            return new NormalUser(name);//向上转型
        }
    }
    public static void main(String[] args) {
        User user=login();//返回值方式向上转型,确定操作者的身份类型
        BookList bookList=new BookList();//new一个书库的对象

        while(true){//可以循环输入
            int choice=user.menu();//操作者可以输入要进行怎么样的操作
            user.doOperation(choice,bookList);//对书库进行功能操作
        }
    }
}

opertion包
IOperation接口
这里我们只提供一个抽象方法,用于对Booklist类的对象进行功能上的操作
在这里插入图片描述






至此,我们的框架已经搭建完毕,可以进行简单的试跑
在这里插入图片描述





下面我们补全对应的功能操作
AddOperation类
在这里插入图片描述

public class AddOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入书的名字");
        String name=sc.nextLine();
        System.out.println("请输入书的作者");
        String author=sc.nextLine();
        System.out.println("请输入书的类型");
        String describe=sc.nextLine();
        System.out.println("请输入书的价格");
        double price=sc.nextDouble();

        Book book =new Book(name,author,describe,price);//new一个新的book对象,并完成初始化
        int pos=bookList.getUsedSized();//获取booklist中的书总数,为增加新书获取位置
        bookList.setBooks(pos,book);//将新书放置在数组库中的最后一个位置
        bookList.setUsedSized(pos+1);//书的总数+1
        System.out.println("添加书籍成功");
    }
}

运行结果如下
在这里插入图片描述


FindOperation类
在这里插入图片描述

ublic class FindOperation implements IOperation{
    public void work(BookList bookList) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要查找图书的名字");
        String name=sc.nextLine();

        //遍历书架,去查找有无此书
        int currentSize=bookList.getUsedSized();
        for (int i = 0; i <currentSize; i++)
            if (name.equals(bookList.getBooks(i).getName())) {
                System.out.println("查到了,该书的信息如下");
                System.out.println(bookList.getBooks(i));
                return;
            }
        System.out.println("找不到该书");
    }
}

BorrowOperation类

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要借阅图书的名字");
        String name=sc.nextLine();
        
        //遍历书架,去查找有无此书
        int currentSize=bookList.getUsedSized();
        for (int i = 0; i <currentSize; i++) {
            if(name.equals(bookList.getBooks(i).getName())){
                //如果有,看它是否有被借出
                if(bookList.getBooks(i).isBorrowed()==false){
                    System.out.println("借阅成功");
                    bookList.getBooks(i).setBorrowed(true);
                    return;
                }else{
                    System.out.println("该书已被借出");
                    return;
                }
            }
        }
        System.out.println("没有此书");
    }
}

运行结果如下
在这里插入图片描述


DelOperation类
在这里插入图片描述

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的书籍");
        String name=sc.nextLine();
        int index=-1;
        for (int i = 0; i < bookList.getUsedSized(); i++) {
            if(bookList.getBooks(i).getName().equals(name)){//查找有无该书
                index=i;
                break;
            }
        }
        if(index==-1){
            System.out.println("没有你要删除的图书");
            return;
        }
        //找到了,准备开始向前覆盖
        for (int i = index; i < bookList.getUsedSized()-1; i++) {
            Book book=bookList.getBooks(i+1);
            bookList.setBooks(i,book);
        }
        //覆盖之后,最后一个数组位置赋null,并设置书的数量-1
        bookList.setBooks((bookList.getUsedSized())-1,null);
        bookList.setUsedSized(bookList.getUsedSized()-1);
        System.out.println("删除成功");
    }
}

在这里插入图片描述


ExitOperation类

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出成功");
        System.exit(0);
    }
}

在这里插入图片描述


ReturnOperation类
逻辑同借阅类的逻辑相似

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入要归还图书的名字");
            String name=sc.nextLine();

            //遍历书架,去查找有无此书
            int currentSize=bookList.getUsedSized();
            for (int i = 0; i <currentSize; i++) {
                if(name.equals(bookList.getBooks(i).getName())){
                    //如果有,看它是否有被借出
                    if(bookList.getBooks(i).isBorrowed()==true){
                        System.out.println("归还成功");
                        bookList.getBooks(i).setBorrowed(false);
                        return;
                    }else{
                        System.out.println("该书未被借出");
                        return;
                    }
                }
            }
            System.out.println("没有此书");
        }
    }

在这里插入图片描述


ShowOperation类
直接遍历即可

public class ShowOperation implements  IOperation{
    @Override
    public void work(BookList bookList) {
        for (int i = 0; i < bookList.getUsedSized(); i++) {
            System.out.println(bookList.getBooks(i));
        }
    }
}

在这里插入图片描述


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

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

相关文章

023:vue中解决el-date-picker更改样式不生效问题

第023个 查看专栏目录: VUE ------ element UI 本文章目录 修改后的效果示例源代码&#xff08;共52行&#xff09;核心内容步骤&#xff1a;&#xff08;1&#xff09;更改样式&#xff08;2&#xff09;添加参数 专栏目标 在vue项目开发中&#xff0c;我们打算保持颜色的一致…

辅助笔记-安装CentOS8.1虚拟机

安装CentOS8.1虚拟机 文章目录 安装CentOS8.1虚拟机1. CentOS8.1的安装1.1 下载CentOS8.1镜像1.2 检查BIOS虚拟化支持1.3 新建虚拟机1.4 安装系统1.5 测试上网和终端 2. CentOS8.0和CentOS7.0的区别(了解) 本文主要参考B站视频“P116_ 韩顺平Linux_cntos8安装和介绍”。 本文目…

小红书kol投放怎么做,kol投放工作规划!

作为分享类平台&#xff0c;小红书有着众多的kol类型。但是该如何合理的使用这些达人&#xff0c;达到品牌传播的目的&#xff0c;就需要一份详尽的计划。今天就跟大家分享一下&#xff0c;小红书kol投放怎么做&#xff0c;kol投放工作规划&#xff01; 什么是kol投放 kol投放即…

迈向未来的大门:人脸识别技术的突破与应用

迈向未来的大门&#xff1a;人脸识别技术的突破与应用 人脸识别&#xff1a;人脸识别的工作流程人脸识别的作用人脸识别技术的突破与应用 在深度学习人脸识别之前我们要先知道人脸识别是什么。 人脸识别&#xff1a; 人脸识别是一种基于人脸图像或视频进行身份验证或识别的技术…

近实时智能应答 2D 数字人搭建

背景 早在大语言模型如 GPT-3.5 等的兴起和被日渐广泛地采用之前&#xff0c;教育行业已经在 AI 辅助教学领域有过各种各样的尝试。在教育行业&#xff0c;人工智能技术的采用帮助教育行业更好地实现教学目标、提高教学质量、提高学习效率、提高学习体验、提高学习成果。例如&a…

动态内存开辟

动态内存开辟 1.动态内存开辟相关试题 题目1&#xff1a; void GetMemory(char *p) {p (char *)malloc(100); } void Test(void) {char *str NULL;GetMemory(str);strcpy(str, "hello world");printf(str); }解释&#xff1a;这里在Test函数中&#xff0c;只是将…

FTP“方便”又“便宜”,为什么有必要替代?

FTP作为全世界第一款文件传输协议&#xff0c;在全球范围内得到大量应用&#xff0c;它为特定场景下的专业传输需求提供了解决方案&#xff0c;被各个行业和领域采用。 FTP使用普遍&#xff0c;主要得益于FTP的经济成本低&#xff0c;且使用方便。目前&#xff0c;开源FTP软件有…

sh脚本函数 数组 expect免交互

1、函数 在编写脚本时&#xff0c;有些脚本可以反复使用&#xff0c;可以调用函数来解决 语句块定义成函数约等于别名 1.1、设置函数 1.2、删除函数 unset 函数名 1.3、函数的传参数 函数变量的作用范围&#xff1a; 函数在shell脚本中仅在当前的shell环境中有效 shell脚…

day 28 | ● 122.买卖股票的最佳时机II ● 55. 跳跃游戏 ● 45.跳跃游戏II

122.买卖股票的最佳时机II 由于卖出没有限制条件&#xff0c;所以可以将一段时间的整体收益分割成每天零碎的收益&#xff0c;然后加起来那些高的即可。 func maxProfit(prices []int) int {sum : 0for i : 1; i < len(prices);i{if prices[i] - prices[i -1] > 0{sum …

Vue入门-特性、常用指令、生命周期、组件

Vue vue简介 ​ Vue (发音为 /vjuː/&#xff0c;类似 view) 是一款用于构建用户界面的JavaScript框架。它基于标准HTML、CSS和JavaScript构建&#xff0c;并提供了一套声明式的、组件化的编程模型&#xff0c;帮助开发者高效地开发用户界面。 [7] Vue特征 解耦视图与数据M…

Meta-SR: A Magnification-Arbitrary Network for Super-Resolution整理

目录 说明摘要引言相关工作SISRMeta-Learning 本文的方法Meta-Upscale方法Location ProjectionWeight PredictionFeature Mapping 实验细节总结实现代码参考链接 说明 作为一个读者&#xff0c;在阅读这篇文章后&#xff0c;按照自己的理解对其中内容做以总结&#xff08;不然总…

ffmpeg,nginx,vlc把rtsp流转hls

ffmpeg:rtsp>hls流; nginx 托管hls流服务; vlc测试hls流服务; 参考了很多相关文档和资料,由于比较乱就不在一一引用介绍了&#xff0c;下面的是实操OK的例子&#xff1b; 1&#xff09;ffmpeg (ffmpeg-4.4.1-full_build)&#xff0c;要用full版本&#xff0c;否则会缺某些…

【从零学习python 】56. 异常处理在程序设计中的重要性与应用

文章目录 异常的概念读取文件异常try...except语句try...else语句try...finally语句 进阶案例 异常的概念 在程序运行过程中&#xff0c;由于编码不规范或其他客观原因&#xff0c;可能会导致程序无法继续运行&#xff0c;此时就会出现异常。如果不对异常进行处理&#xff0c;…

电工-什么是电功?及电功单位与计算公式讲解

什么是电功&#xff1f;及电功单位与计算公式讲解 电能是有其他形式的能量&#xff08;如机械能、热能、化学能、核能&#xff09;转换而来的一种能量&#xff0c;而电能又可以转换成为其他形式的能。比如当电能的具体体现&#xff1a;电流&#xff0c;其通过电灯泡发光就是将…

代码详解——可变形卷积(DCNv3)

文章目录 概述dcnv3.pyto_channels_firstto_channels_lastbuild_norm_layerbuild_act_layer_is_power_of_2CenterFeatureScaleModuleDCNv3_pytorchDCNv3 dcnv3_func.pyDCNv3Functiondcnv3_core_pytorch_get_reference_points_generate_dilation_grids 可变形卷积DCNv1 & DC…

华为OD机试 - 出错的或电路 - 二进制 - (Java 2023 B卷 100分)

目录 专栏导读一、题目描述二、输入描述三、输出描述四、解题思路五、Java算法源码六、效果展示1、输入2、输出3、说明 华为OD机试 2023B卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;A卷B卷&#…

Vue2.0+webpack 引入字体文件(eot,ttf,woff)

webpack.base.config.js 需要配置 {test:/\/(woff2?|eot|ttf|otf)(\?.*)?$/,loader: url-loader,options: {limit: 10000,name: utils.assetsPath(fonts/[name].[hash:7].[ext])}} 如果 Vue2.0webpack3.6引入字体文件&#xff08;eot&#xff0c;ttf&#xff0c;woff&…

成都爱尔林江院长解析离焦眼镜为何与众不同

近视是影响我国国民尤其是青少年眼健康的重大公共卫生问题。因病因不明确&#xff0c;且尚无有效的治疗方法&#xff0c;如何有效控制近视发生和增长备受关注。国家出台了儿童近视防控方案&#xff0c;社会上也出现了各种近视防控方法及策略。周边离焦技术&#xff0c;算得上近…

快速上手Linux核心命令:Linux的文本编辑器vi和vim

前言 上一篇中已经预告&#xff0c;我们这篇主要说Linux中vi/vim 编辑器。它是我们使用Linux系统不可缺少的工具&#xff0c;学会了&#xff0c;你就可以在Linux世界里畅通无阻&#xff0c;学废了&#xff0c;常用操作你也会了&#xff0c;也是够用了&#xff0c;O(∩_∩)O 简…

javascript初学者可以做些什么小东西或者项目来练手?

前言 可以试一下面的一些项目&#xff0c;可能有一些比较复杂&#xff0c;可以学习一下代码的结构思路&#xff0c;希望对你有帮助~ 实用工具向 1.Exchart Star&#xff1a;55.6k Exchart提供了大量精美的图表&#xff0c;只有你想不到&#xff0c;没有你在它上面找不到的&…