图书管理系统代码(Java)

news2024/11/26 18:23:24

1、运行演示

QQ2024528-205028-HD

详细讲解在这篇博客:JavaSE:图书管理系统-CSDN博客 

2、所建的包

3、Java代码

3.1 book包

3.1.1 Book类代码

package book;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:16
 */
public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;
    public Book(String name, String author, String type, int price) {
        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 boolean getBorrowed() {
        return isBorrowed;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +", 状态:"+
                (isBorrowed ? " 该书已被借出" : "该书未被借出")+
                '}';
    }
}

3.1.2 BookList类代码

package book;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:19
 */
public class BookList {
    private Book[] books = new Book[10];
    private int bookNumber;

    public BookList() {
        books[0] = new Book("哈利波特", "J.K.罗琳", "小说",30);
        books[1] = new Book("三国演义", "罗贯中","小说", 35);
        books[2] = new Book("红楼梦", "曹雪芹", "小说",25);
        books[3] = new Book("鲁宾逊漂流记", "丹尼尔·笛福", "小说",40);
        this.bookNumber = 4;
    }

    public int getBookNumber() {
        return bookNumber;
    }

    public void setBookNumber(int bookNumber) {
        this.bookNumber = bookNumber;
    }

    public Book getBook(int index) {
        Book book = books[index];
        return book;
    }

    public void setBooks(Book book,int index) {
        books[index] = book;
    }
}

3.2 operation包

3.2.1 AddBook类代码

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:33
 */
public class AddBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        int currentNum = bookList.getBookNumber();
        Scanner scanner = new Scanner(System.in);
        System.out.println("*************Add Book*************");

        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,type,price);
        bookList.setBooks(book,currentNum);
        bookList.setBookNumber(currentNum+1);
    }
}

3.2.2 BorrowBook类代码

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:35
 */
public class BorrowBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Borrow Book*************");
        System.out.println("请输入您所要借阅图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                if(book.getBorrowed() == true) {
                    System.out.println("很抱歉!!!该书籍已被其他人借阅!!!");
                    return;
                }else {
                    book.setBorrowed(true);
                    System.out.println("借阅成功!");
                    return;
                }
            }
        }
        System.out.println("没有查找到该书籍!!!");
    }
}

3.2.3 DeleteBook类代码

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:34
 */
public class DeleteBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Borrow Book*************");
        System.out.println("请输入您所要删除图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        int index = -1;
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("没有查找到该图书!!!");
        }else {
            for (int i = index; i < currentNum-1; i++) {
                Book book = bookList.getBook(i+1);
                bookList.setBooks(book,i);
            }
            bookList.setBooks(null,currentNum);
            bookList.setBookNumber(currentNum-1);
            System.out.println("成功删除该图书!!!");
        }
    }
}

3.2.4 ExitSystem类代码

package operation;

import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 16:22
 */
public class ExitSystem implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!图书管理系统随时欢迎您的下次到来!");
        System.exit(0);
    }
}

3.2.5 FindBook类代码

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:34
 */
public class FindBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Find Book*************");
        System.out.println("请输入您所要查找图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                System.out.println("查找到该书!该书相关信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有查找到该书籍!!!");
    }
}

3.2.6 IOperation接口代码

package operation;

import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:38
 */
public interface IOperation {
    void work(BookList bookList);
}

3.2.7 ReturnBook类代码

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:35
 */
public class ReturnBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Return Book*************");
        System.out.println("请输入您所要归还图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                if(book.getBorrowed()) {
                    book.setBorrowed(false);
                    System.out.println("图书归还成功!!!欢迎您的下次光临!!!");
                    return;
                }else {
                    System.out.println("查找到该书,但该书未被借出!!!");
                    return;
                }
            }
        }
        System.out.println("没有查找到该书籍!!!");
    }
}

3.2.8 ShowBook类代码

package operation;

import book.Book;
import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 17:21
 */
public class ShowBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        int currentNum = bookList.getBookNumber();
        System.out.println("***************展示书籍***************");
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
        System.out.println("***************展示书籍***************");
    }
}

3.3 user包

3.3.1 User类代码

package user;

import book.BookList;
import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:30
 */
public abstract class User {
    protected String name;

    IOperation[] iOperations;

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

    public void youOperate(int choice,BookList bookList) {
        this.iOperations[choice].work(bookList);
    }
}

3.3.2 Administrator类代码

package user;

import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:31
 */
public class Administrator extends User {
    public Administrator(String name) {
        super(name);
        iOperations = new IOperation[]{
                new ExitSystem(),
                new AddBook(),
                new FindBook(),
                new ShowBook(),
                new DeleteBook()
        };
    }

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

3.3.3 NormalUser类代码

package user;

import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:30
 */
public class NormalUser extends User {
    public NormalUser(String name) {
        super(name);
        iOperations = new IOperation[]{
                new ExitSystem(),
                new FindBook(),
                new BorrowBook(),
                new ReturnBook(),
                new ShowBook()
        };
    }

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

3.4 默认包

3.4.1 Main类代码

import book.BookList;
import user.Administrator;
import user.NormalUser;
import user.User;

import java.util.Random;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:图书管理系统
 * User: dings
 * Date: 2024-05-13
 * Time: 13:18
 */
public class Main {
    public static User login() {
        System.out.println("请输入您的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("Hello! "+name+"! "+"欢迎来到图书系统!");
        System.out.println("请选择您的身份:1->图书管理员  2->普通用户");
        int choice = scanner.nextInt();
        if(choice == 1) {
            return new Administrator(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.youOperate(choice,bookList);
        }
    }
}

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

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

相关文章

CentOS 7.9检测硬盘坏区、实物定位(三)

系列文章目录 CentOS 7.9上创建JBOD&#xff08;一&#xff09; CentOS 7.9上创建的JBOD阵列恢复&#xff08;二&#xff09; 文章目录 系列文章目录前言一、在系统中找到硬盘对应的盘符二、使用命令定位实物1.badblocks检测坏块2.对2T以上的硬盘检测&#xff08;对本篇非必要…

【Mongodb-01】Mongodb亿级数据性能测试和压测

mongodb数据性能测试 一&#xff0c;mongodb数据性能测试1&#xff0c;mongodb数据库创建和索引设置2&#xff0c;线程池批量方式插入数据3&#xff0c;一千万数据性能测试4&#xff0c;两千万数据性能测试5&#xff0c;五千万数据性能测试6&#xff0c;一亿条数据性能测试7&am…

力扣每日一题 6/17 枚举+双指针

博客主页&#xff1a;誓则盟约系列专栏&#xff1a;IT竞赛 专栏关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 522.最长特殊序列II【中等】 题目&#xff1a; 给定字符串列表 strs &…

.Net OpenCVSharp生成灰度图和二值图

文章目录 前言一、灰度图二、二值图 前言 使用OpenCVSharp生成图片的灰度图和二值图 .Net 8.0版本&#xff0c;依赖OpenCvSharp4和OpenCvSharp4.runtime.win组件。 原图&#xff1a; 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面案例可供参考 一、灰度图 /// &…

亚马逊跟卖ERP系统,怎样提升新手卖家选品的质量与效率!

今天给新手卖家推荐一款实用ERP系统&#xff0c;相信新手卖家在碰到最难的问题应该就是选品了吧&#xff0c;那我们该如何快速解决我们找品难找品慢的问题呢&#xff0c;现在有一套专门为跟卖打造的一款全功能erp系统&#xff0c;从我们选品上传&#xff0c;到我们采购&#xf…

针对微电网中可时移,柔性,基础负荷的电价响应模型---代码解析

前言&#xff1a; 在上两篇帖子中&#xff0c;讲解了我对于粒子群算法的理解&#xff0c;站在巨人的肩膀上去回望&#xff1a;科研前辈们确实非常牛逼&#xff0c;所以它才成为了非常经典的算法。这篇帖子主要是想分享一下&#xff0c;对于微电网、电力系统的论文中&#xff0c…

JavaScript事件类型和事件处理程序

● 之前我们用过了很多此的点击事件&#xff0c;这次让我们来学习另一种事件类型 mouseenter “mouseenter” 是一个鼠标事件类型&#xff0c;它在鼠标指针进入指定元素时触发。 const h1 document.querySelector(h1); h1.addEventListener(mouseenter, function (e) {aler…

用Python pillow 创建和保存GIF动画

使用pillow库来创建和保存gif GIFs:图形交换格式(gif)是一种位图图像格式&#xff0c;由美国计算机科学家Steve Wilhite于1987年6月15日领导的在线服务提供商CompuServe的一个团队开发。 一个GIF文件通常存储一个图像&#xff0c;但该格式允许在一个文件中存储多个图像。该格…

用Vue3和p5.js打造一个交互式数据可视化仪表盘

本文由ScriptEcho平台提供技术支持 项目地址&#xff1a;传送门 基于 Vue.js 集成 p5.js 实现交互式波形图 应用场景介绍 在数据可视化领域&#xff0c;波形图广泛应用于展示动态变化的数据&#xff0c;如声音信号、心跳曲线等。通过动态绘制波形图&#xff0c;用户可以直观…

25天录用!快到飞起的宝藏SSCI,免版面费,1天见刊!毕业评职即刻拿下

本周投稿推荐 SSCI • 中科院2区&#xff0c;6.0-7.0&#xff08;录用友好&#xff09; EI • 各领域沾边均可&#xff08;2天录用&#xff09; CNKI • 7天录用-检索&#xff08;急录友好&#xff09; SCI&EI • 4区生物医学类&#xff0c;0.5-1.0&#xff08;录用…

关于禁止word的无用插入模式

这是我的word版本号 点击左上角文件选项 找到左侧最下方的选项 点击高级 把这两个叉掉

✅生产问题之Emoji表情如何操作存储,MySQL是否支持

针对 Emoji 表情 MySQL 存储是否支持的问题&#xff0c;结论是&#xff1a; MySQL 中可以存储 emoji 表情&#xff0c;但需要使用 UTF8MB4 字符编码。如果使用 UTF8MB3&#xff0c;存储这些扩展字符会导致解析错误。 课外补充 MySQL 对 Unicode 的支持 Unicode 字符集已成为…

整合第三方技术-整合JUnit

黑马程序员Spring Boot2 文章目录 名称&#xff1a;SpringBootTest类型&#xff1a;测试类注解位置&#xff1a;测试类定义上方作用&#xff1a;设置JUnit加载的SpringBoot启动类范例&#xff1a;

Stable-Baseline3 x SwanLab:可视化强化学习训练

Stable Baselines3 (SB3) 是一个强化学习的开源库&#xff0c;基于 PyTorch 框架构建。它是 Stable Baselines 项目的继任者&#xff0c;旨在提供一组可靠且经过良好测试的RL算法实现&#xff0c;便于研究和应用。StableBaseline3主要被应用于机器人控制、游戏AI、自动驾驶、金…

[面试题]Spring

[面试题]Java【基础】[面试题]Java【虚拟机】[面试题]Java【并发】[面试题]Java【集合】[面试题]MySQL[面试题]Maven[面试题]Spring Boot[面试题]Spring Cloud[面试题]Spring MVC[面试题]Spring[面试题]MyBatis Spring 是一个很庞大的技术体系&#xff0c;可以说包容一切&…

【PL理论】(22) 函数式语言:多参数 | 柯里化 (Currying) : 将多参数函数实现为返回一个函数的函数

💭 写在前面:本章我们将继续讲解函数式语言,介绍多参数,着重讲解柯里化的概念,将多参数函数实现为返回一个函数的函数。目录 0x00 多参数(Multiple Arguments) 0x01 柯里化(Currying) 0x00 多参数(Multiple Arguments) 我们可以定义一个带有多个参数的函数吗?比…

计算机组成原理之存储器(一)

文章目录 存储器概述存储器的分类情况按照存储器在系统中的作用分类按存储介质分类按存取方式分类 主存储器的技术指标 存储器概述 程序的局部性原理&#xff08;构成多级存储系统的依据&#xff09;&#xff1a;在某一个时间段你频繁访问某一局部的存储器地址空间&#xff0c;…

短剧系统搭建全攻略:功能齐全,一步到位

前言 近年来&#xff0c;短剧系统以其独特魅力&#xff0c;成为大众消遣娱乐的热门选择。简单来说&#xff0c;短剧系统就是用来看短剧的小程序&#xff0c;它汇集了丰富多彩的短剧资源&#xff0c;让观众随时随地享受观影乐趣。本文将为你详细解析短剧系统的搭建全攻略&#…

web前端之vue一键部署的shell脚本和它的点.bat文件、海螺AI、ChatGPT

MENU 前言vite.config.ts的配置deploy文件夹的其他内容remote.shpwd.txtdeploy.bat 前言 1、在src同级新建deploy.bat文件&#xff1b; 2、在src同级新建deploy文件夹&#xff0c;文件夹中新建pwd.txt和remote.sh文件&#xff1b; 3、配置好后&#xff0c;直接双击deploy.bat文…

生成式人工智能备案办理指南,深度解析大模型备案全流程

早在2023年年初&#xff0c;国家互联网信息办公室、工业和信息化部、公安部针对深度合成服务制定的《互联网信息服务深度合成管理规定》&#xff08;“《深度合成管理规定》”&#xff09;顺利施行&#xff0c;其明确了深度合成服务相关方的义务与主体责任&#xff0c;强化了对…