java使用面向对象实现图书管理系统

꒰˃͈꒵˂͈꒱ write in front ꒰˃͈꒵˂͈꒱
ʕ̯•͡˔•̯᷅ʔ大家好,我是xiaoxie.希望你看完之后,有不足之处请多多谅解,让我们一起共同进步૮₍❀ᴗ͈ . ᴗ͈ აxiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客
本文由xiaoxieʕ̯•͡˔•̯᷅ʔ 原创 CSDN 如需转载还请通知˶⍤⃝˶
个人主页:xiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客
系列专栏:xiaoxie的JAVA系列专栏——CSDN博客●'ᴗ'σσணღ*
我的目标:"团团等我💪( ◡̀_◡́ ҂)" 

( ⸝⸝⸝›ᴥ‹⸝⸝⸝ )欢迎各位→点赞👍 + 收藏⭐️ + 留言📝​+关注(互三必回)!

 一.Book类

首先我们需要先创建一个Book类

public class Book {
    private String name;
    private String author;
    private double price;
    private String type;
    private boolean  isBorrowed;
   // 构造函数
    public Book(String name, String author, double 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 double getPrice() {
        return price;
    }

    public void setPrice(double 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;
    }
  // 重写toString方法
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ( (isBorrowed == true) ? ",已借出" : ",未借出" )+
                //", isBorrowed=" + isBorrowed +
                '}';
    }
}

二.BookList类

public class BookList {
    public Book[] List;
    public int size;
    public BookList() {
       List = new Book[10];
       List[0] = new Book("西游记", "吴承恩", 10.25, "小说");
       List[1] = new Book("三国演义","罗贯中",20.50,"小说");
       List[2] = new Book("红楼梦","曹雪芹",50.9,"小说");
       List[3] = new Book("水浒传","施耐庵",15.5,"小说");

        size = 4;
    }
    public boolean isFull() {
        return size >= List.length;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public void newLength() {
        List = new Book[size+10];
    }

    public Book getList(int i) {
        return List[i];
    }

    public int getSize() {
        return size;
    }

    public void setList(Book book , int pos) {
        List[pos] = book;
    }

    public void setSize(int size) {
        this.size = size;
    }
}

三.User类

public abstract class User {
 public IOperation[] iOperations;
 public String name;
    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice,BookList bookList) {
        IOperation operation = this.iOperations[choice];
        operation.work(bookList);
    }
}

四.管理员类

public class Admin extends User{
    public Admin(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DeleteOperation(),
                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("************************");
        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 ExitOperation(),
               new  FindOperation(),
               new BorrowedOperation(),
               new ReturnOperation()
       };
    }
    @Override
    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("***************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

六.实现操作的接口

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

 七.各种操作类

import java.util.Scanner;

// 添加图书的操作

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        if(bookList.isFull()) {
            bookList.newLength();
        }
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要添加的书名");
        String name = scan.nextLine();
        System.out.println("请输入你要添加图书的作者");
        String author = scan.nextLine();
        System.out.println("请输入你要添加图书的价格");
        double price = scan.nextDouble();
        scan.nextLine();
        System.out.println("请输入你要添加图书的类型");
        String type = scan.nextLine();
        Book tmp = new Book(name,author,price,type);
        int count = bookList.getSize();
        for (int i = 0; i < count; i++) {
            if(tmp.getName().equals(bookList.getList(i).getName())) {
                System.out.println("请勿重复添加");
                System.exit(0);
            }
        }
        bookList.setList(tmp,count);
        bookList.setSize(count+1);
    }
}
import java.util.Scanner;

// 借出书籍

public class BorrowedOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        Scanner scan = new Scanner(System.in);
        int count1 = bookList.getSize();
        System.out.println("以下是图书馆的书单: ");
        for (int i = 0; i < count1; i++) {
            if (bookList.getList(i) != null) {
                System.out.println(bookList.getList(i));
            }
        }
        System.out.println("请输入你要借阅的图书名字:");
        String name = scan.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if (bookList.getList(i).getName().equals(name) ) {
                if(bookList.getList(i).isBorrowed()  ) {
                    System.out.println("书已被借走,请等归还后再来借阅");
                    return;
                }
                bookList.getList(i).setBorrowed(true);
                System.out.println("借阅成功,请于7天时间内归还");
                return;
            }
        }

        if(i == count) {
            System.out.println("没有找到这本书");
        }
    }
}
import java.util.Scanner;


 //删除书籍的操作
 
public class DeleteOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        if(bookList.isEmpty()) {
          throw  new NullException("书架为空");
        }
        Scanner san = new Scanner(System.in);
        System.out.println("请输入你要删除的图书名字");
        String name = san.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if(bookList.getList(i).getName().equals(name)) {
               break;
            }
        }
        if(i == count) {
            System.out.println("没有找到这本书");

        } else {
            while (i < count) {
                bookList.List[i++] = bookList.List[i+1];
            }
            System.out.println("删除成功");
            bookList.setSize(count-1);
        }
    }
}
// 退出操作

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.exit(0);
    }
}
import java.util.Scanner;

// 通过图书的名字来查找图书

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要查找的图书名字");
        String name = scan.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if(bookList.getList(i).getName().equals(name)) {
                System.out.println("图书信息如下:");
                System.out.println(bookList.getList(i));
                break;
            }
        }
        if(i == count) {
            System.out.println("没有找到这本书");
        }
    }
}
//归还操作
public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要归还的图书名字:");
        String name = scan.nextLine();
        int count = bookList.getSize();
        int i = 0;
        for (; i < count; i++) {
            if (bookList.getList(i).getName().equals(name) && bookList.getList(i).isBorrowed()) {
                bookList.getList(i).setBorrowed(false);
                System.out.println("归还成功,欢迎下次光临");
                return;
            }
        }
        if(i == count) {
            System.out.println("没有找到这本书");
        }
    }
}
//显示图书的操作

public class ShowOperation implements IOperation{

    @Override
    public void work(BookList bookList) {
        int count = bookList.getSize();
        System.out.println("图书信息如下: ");
        for (int i = 0; i < count; i++) {
            if (bookList.getList(i) != null) {
                System.out.println(bookList.getList(i));
            }
        }
    }
}
//异常
public class NullException extends RuntimeException {
    public NullException() {
    }
    public NullException(String message) {
        super(message);
    }
}

八.主函数

public class Main {
    public static User menu() {
        int choice = 0;
        String possWord = "123456";
        System.out.println("请输入你的身份:");
        System.out.println("1.管理员   2.普通用户 ");
        Scanner scan = new Scanner(System.in);
        choice = scan.nextInt();
        scan.nextLine();
        if (choice == 1) {
            System.out.println("请输入密码:");
            int count = 3;
            while (count > 0) {
                String MyPossWord = scan.nextLine();
                if (MyPossWord.equals(possWord)) {
                    count = -1;
                    break;
                } else {
                    --count;
                    System.out.println("密码错误,你还有" + count + "次机会");
                }
            }
            if (count != -1) {
                System.out.println("密码输入错误超过3次,请您24小时后在来");
                System.exit(0);
            }
            return new Admin("admin");
        }
        return new NormalUser("noraluser");
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = Main.menu();
        while (true) {
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
        }
}

九.说明

以上就是java使用面向对象的知识来实现图书管理系统的全部内容了,此代码仅仅只是对初学Java的读者有帮助,可以通过借鉴此代码,再根据自己所学的知识自己构建一个图书管理系统,这个 图书管理系统也是差不多涵盖了JavaSE所有内容,博主相信你自己下去编写一个图书管理系统,会对Java的掌握更上一步。 

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

2023年12月21日历史上的今天大事件早读

1375年12月21日 意大利文艺复兴的代表人物薄伽丘逝世 1620年12月21日 英国抵达第一个北美殖民地普利茅斯 1879年12月21日 斯大林出生 1921年12月21日 苏俄电气化计划批准 1928年12月21日 天津劝业场开业 1928年12月21日 中国核化学家王方定出生 1935年12月21日 载客21人的…

飞书+ChatGPT搭建智能AI助手,无公网ip实现公网访问飞书聊天界面

飞书ChatGPT搭建智能AI助手&#xff0c;无公网ip实现公网访问飞书聊天界面 前言环境列表1.飞书设置2.克隆feishu-chatgpt项目3.配置config.yaml文件4.运行feishu-chatgpt项目5.安装cpolar内网穿透6.固定公网地址7.机器人权限配置8.创建版本9.创建测试企业10. 机器人测试 前言 …

qt-C++笔记之app.processEvents()和QApplication::processEvents()的区别

qt-C笔记之app.processEvents()和QApplication::processEvents()的区别 code review! 代码1&#xff1a; QApplication app(argc, argv); app.processEvents(); 代码2: QApplication::processEvents(); 区别 代码1和代码2的区别在于代码1中使用了一个具体的QApplication对…

Java项目的学习记录---12306购票系统的技术架构选型

后端技术架构 选择基于 Spring Boot 3 和 JDK17 进行底层建设 前端技术架构

征集倒计时 | 2023年卓越影响力榜单-第四届中国产业创新奖报名即将截止

第四届「ISIG中国产业智能大会」将于2024年3月16日在上海举办。2024 ISIG 以“与科技共赢&#xff0c;与产业共进”为主题&#xff0c;共设立RPA超自动化、 低代码、AIGC大模型、流程挖掘四大主题峰会。届时&#xff0c;大会组委会将颁发2023年度卓越影响力榜单—第四届中国产业…

动态内存分配(malloc和free​、calloc和realloc​)

目录 一、为什么要有动态内存分配​ 二、C/C中程序内存区域划分​ 三、malloc和free​ 2.1、malloc 2.2、free​ 四、calloc和realloc​ 3.1、calloc​ 3.2、realloc​ 3.3realloc在调整内存空间的是存在两种情况&#xff1a; 3.4realloc有malloc的功能 五、常见的动…

vue proxy代理 和 Nginx 配置跨域

vue.config.js文件中配置的代理&#xff1a; devServer: {port: 9095,// open: true, // 配置项目在启动时自动在浏览器打开proxy: {/yh: { // /api是代理标识&#xff0c;一般是每个接口前的相同部分target: "http://192.168.5.58:8002", // 请求地址&#xff0c;一…

Python3 标准库中推荐的命令行解析模块argparse的使用示例

import os import argparse import sys import requests import json import subprocess from datetime import datetime# 定义一个装饰器&#xff0c;在方法执行异常时跳过执行 def skip_on_exception(func):def wrapper(*args, **kwargs):try:return func(*args, **kwargs)ex…

uniapp uview 页面多个select组件回显处理,默认选中

<view class"add-item column space-around" click"selectClick(1)"><text class"w-s-color-3 f-28">商品分类</text><view class"w-100 space-between"><!-- 第一个参数为你的单选数组&#xff0c;第二个…

黑马头条--day07--app文章搜索

目录 一.安装elasticsearch 1.拉取镜像 2.创建存放数据及配置文件的文件夹&#xff0c;启动时挂载。 4.修改文件夹权限 5.启动容器 5.1参数解释 6.安装ik分词器 6.2测试一下Ik分词器 二.添加文章索引库 1查询所有的文章信息&#xff0c;批量导入到es索引库中 2)测试 …

自我学习--关于如何设计光耦电路

本人在项目中多次设计光耦电路&#xff0c;目前电路在项目中运行比较平稳&#xff0c;所以总结一下自己的设计经验&#xff0c;与大家交流一下&#xff0c;如有错误还希望大家指出改正&#xff0c;谢谢&#xff08;V&#xff1a;Smt15921588263&#xff1b;愿与大家多交流&…

红队打靶练习:DIGITALWORLD.LOCAL: DEVELOPMENT

信息收集 1、arp ┌──(root㉿ru)-[~/kali] └─# arp-scan -l Interface: eth0, type: EN10MB, MAC: 00:0c:29:69:c7:bf, IPv4: 192.168.12.128 Starting arp-scan 1.10.0 with 256 hosts (https://github.com/royhills/arp-scan) 192.168.12.1 00:50:56:c0:00:08 …

持续集成交付CICD:Jenkins使用GitLab共享库实现前端项目镜像构建

目录 一、实验 1. GitLab修改项目文件与Harbor环境确认 2.Jenkins使用GitLab共享库实现前端项目镜像构建 3.优化CI流水线封装Harbor账户密码 4.Jenkins再次使用GitLab共享库实现前端项目镜像构建 一、实验 1. GitLab修改项目文件与Harbor环境确认 &#xff08;1&#xf…

多维时序 | MATLAB实现BiTCN-Multihead-Attention多头注意力机制多变量时间序列预测

多维时序 | MATLAB实现BiTCN-Multihead-Attention多头注意力机制多变量时间序列预测 目录 多维时序 | MATLAB实现BiTCN-Multihead-Attention多头注意力机制多变量时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 多维时序 | MATLAB实现BiTCN-Multihea…

cisp和cissp区别,考证必学资料

CISP&#xff08;Certified Information Security Professional&#xff0c;认证信息安全专家&#xff09;和CISSP&#xff08;Certified Information Systems Security Professional&#xff0c;认证信息系统安全专业人员&#xff09;都是信息安全领域的重要认证&#xff0c;但…

学校教育培训课程课件报名营销学生作业习题小程序开发

学校教育培训课程课件报名营销学生作业习题小程序开发 学校教育培训课程课件报名营销学生作业习题小程序开发 以下是学校教育培训课程课件报名营销学生作业习题小程序的功能列表&#xff1a; 用户注册与登录功能&#xff1a;用户可以通过手机号或第三方账号注册和登录小程序。课…

2024年湖北建筑行业工程师职称/中级职称申报误区

2024年湖北建筑行业工程师职称/中级职称申报误区 关于职称申报误区是什么意思呢&#xff1f;当前你需要一个建筑类中级职称&#xff0c;你就在市面上随便找个JG对比价格跟周期符合就交资料等着中级职称出来。时间到了你等出来啥了&#xff0c;那就不好说。关于2024年中级职称申…

网络通信day5作业

1> 使用select完成TCP客户端程序 客户端: #include<myhead.h>#define FPORT 9999 #define FIP "192.168.125.130"#define KPORT 6666 #define KIP "192.168.125.130"int main(int argc, const char *argv[]) {//创建套接字文件描述符int cfd…

IP子网划分【专题突破】

1、IP地址基础 IPv4地址是32位&#xff0c;采用点分十进制方式表示&#xff0c;其次必须掌握二进制的转换。 IPv6地址是128位&#xff0c;采用冒号分隔的十六进制表示方法。 2、IP地址的分类 RFC1918规定的私有地址 A类地址范围&#xff1a;10.0.0.0-10.255.255.255(1个A类…

二叉树题目:输出二叉树

文章目录 题目标题和出处难度题目描述要求示例数据范围 前言解法一思路和算法代码复杂度分析 解法二思路和算法代码复杂度分析 题目 标题和出处 标题&#xff1a;输出二叉树 出处&#xff1a;655. 输出二叉树 难度 6 级 题目描述 要求 给定二叉树的根结点 root \textt…
最新文章