【JAVASE】图书管理系统 上

简介: 【JAVASE】图书管理系统

1. 设计思路图


图书管理系统的使用者有两类,一类是图书管理员,一类是借书用户。

他们在管理系统中的需求与操作是不同的,所以要分别进行设计。

766f97dc5c5087b9c2c7c40575241433_3a15e6f1867643f6bfe1c052dd46ed4c.png


2. 创建 book 包


建立一个 book 包,用来存放与书相关的内容。

04dd3bf94b1ce3f39921fc37a93c1f6f_32dba47f65954121bcf9e674834020fd.png


2.1 Book 类

Book 类中存放与书有关的属性,而一本书的属性有很多,比如:书名、作者、价格、类型、编号…


代码实现:

  //属性
  private String name;//书名
    private String author;//作者
    private double price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否被借出

对以上私有的属性提供 Getter and Setter 方法:


右击选择 Generate,点击 Getter and Setter方法,然后选择所有属性


得到:

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

构造书的对象(没有必要写书是否被借出,只需提供书的信息即可):


右击选择 Generate,点击 Constructor方法,然后选择属性


得到:


//构造书的对象--没必要写书是否被借出
      public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

打印一本书:


右击选择 Generate,点击 toString方法,然后选择所有属性


得到:


//打印图书
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price='" + price + '\'' +
                ", type='" + type + '\'' +
                ((isBorrowed == true) ? " 已经借出" : " 未被借出") +
                '}';
    }

整合代码:


package 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;
    }
    //打印图书
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price='" + price + '\'' +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }
}

2.2 BookList 类

BookList 类(书架)用来存放书与借书,而对于书架上的书,我们需要知道一个书架能放多少本书,有几本书未被借出,书架上所存放的书都有哪些,书架上还能继续存放几本书等。


基于这些问题,我们先定义一个 books 数组来控制书架上存放书的数量,


  private Book[] books ;
    private int usedSize;//记录当前书架上 实际存放的书的数量
    private static final int DEFAULT_CAPACITY = 10;
    public BookList() {
        this.books = new Book[DEFAULT_CAPACITY];//初始化书的数量
    }

因为usedSize是private修饰的,我们就可以提供Getter and Setter 方法:


  public int getUsedSize() {
        return usedSize;
    }
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

而对于books数组,当我们想获取或设置某个下标的书时,也可以提供Getter and Setter方法,但是需要进行改进:

  public Book getBook(int pos) {
        return books[pos];//返回书的位置
    }
    public void setBooks(int pos,Book book) {
        books[pos] = book;//将books的pos位置存放一本书
    }

提前存入一些书进去:

this.books[0] = new Book("三国演义","罗贯中",10,"小说");
this.books[1] = new Book("西游记","吴承恩",9,"小说");
this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
this.usedSize = 3;

整合代码:


package book;
//书架
public class BookList {
    private Book[] books ;
    private int usedSize;//记录当前书架上 实际存放的书的数量
    private static final int DEFAULT_CAPACITY = 10;
    public BookList() {
        this.books = new Book[DEFAULT_CAPACITY];//初始化书的数量
        //放好书!!
        this.books[0] = new Book("三国演义","罗贯中",10,"小说");
        this.books[1] = new Book("西游记","吴承恩",9,"小说");
        this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
        this.usedSize = 3;
    }
    public int getUsedSize() {
        return usedSize;
    }
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    public Book getBook(int pos) {
        return books[pos];
    }
    public void setBooks(int pos,Book book) {
        books[pos] = book;
    }
    public Book[] getBooks() {
        return books;
    }
}


3. 创建 operation 包


存放操作相关的内容:


管理员:查找图书、增加图书、删除图书、显示图书、退出系统

用户:查找图书、借阅图书、归还图书、退出系统

其中查找图书、退出系统两者都具备,可以在一个类中进行操作。


在上述操作中都需要对 BookList 进行操作,为了统一管理,我们在 operation 包内定义一个接口 IOPeration,通过IOPeration就可以对这些操作进行组织,从而实现了接口的统一性:

fedae25ebfc31aacafa0fef832b974b0_c0bf2cd87bf94ec1bab62279422f4307.png


在该接口中定义一个 work方法,对书架上的书进行统一管理:

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

3.1 FindOperation 类-查找图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class FindOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        //通过书名查找图书
        Scanner scanner = new Scanner(System.in);
        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)) {
                System.out.println("找到了这本书,信息如下:");
                System.out.println(book);
                return;
            }
        }`在这里插入代码片`
        System.out.println("没有找到这本书!");
    }
}

3.2 AddOperation 类-增加图书

import book.Book;
import book.BookList;
import java.util.Scanner;
public class AddOperation implements IOPeration{
    @Override
    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();
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)) {
                System.out.println("有这本书,不进行存放了!");
                return;
            }
        }
        if(currentSize == bookList.getBooks().length) {
            System.out.println("书架满了!");
        }else {
            bookList.setBooks(currentSize,book);
            bookList.setUsedSize(currentSize+1);
        }
    }
}

3.3 DelOperation 类-删除图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class DelOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书:");
        String name = scanner.nextLine();
        int pos = -1;
        int currentSize = bookList.getUsedSize();
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                pos = i;
                break;
            }
        }
        if(i == currentSize) {
            System.out.println("没有你要删除的图书!");
            return;
        }
        //开始删除
        int j = pos;
        for (; j < currentSize-1; j++) {
            //[j] = [j+1]
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setBooks(j,null);
        bookList.setUsedSize(currentSize-1);
    }
}

3.4 ShowOperation 类-显示图书

因为我们的书都是在数组中保存,所以可以通过数组元素的打印方法,来显示所有图书:


package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书!");
        /**
         * 1. 你要借阅哪本书?
         * 2. 你借阅的书有没有?
         * 3. 借阅的方式是什么? -》 isB.... = true
         */
        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("你借阅的图书 不存在!! ");
    }
}

3.5 BorrowOperation 类-借阅图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class BorrowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书!");
        /**
         * 1. 你要借阅哪本书?
         * 2. 你借阅的书有没有?
         * 3. 借阅的方式是什么? -》 isB.... = true
         */
        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("你借阅的图书 不存在!! ");
    }
}

3.6 ReturnOperation 类-归还图书

package operation;
import book.Book;
import book.BookList;
import java.util.Scanner;
public class ReturnOperation implements IOPeration{
    @Override
    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("你归还的图书 不存在!! ");
    }
}

相关文章
|
2月前
|
弹性计算 运维 小程序
阿里云轻量应用云服务器租用价格表(一年/按月/按小时报价明细)
阿里云轻量应用服务器 适合个人建站、测试项目、小程序后端和中小型业务,价格最低、运维最省心;如果业务需要稳定独享带宽或高并发承载,建议升级到ECS标准实例。省钱的核心不是单纯比价,而是让配置与业务负载匹配,再叠加新用户权益、优惠券、大促节点和年付折扣。
462 0
|
2月前
|
人工智能 IDE API
阿里云 Qoder CN 智能编码平台实战教程:本地 IDE 插件配置、终端 CLI 操作、云端 Agent 部署与开放 API 接入全流程
AI编程已经从简单的代码片段生成,进化到智能体自主完成工程任务的新阶段。Qoder CN不是简单的代码补全插件,而是一套完整面向软件研发的AI智能体平台,覆盖IDE桌面、终端CLI、云端托管、数字员工多条产品线,深度对接百炼平台订阅体系,依托强大的工程上下文感知、Quest自主任务模式、MCP扩展能力,把自然语言需求转化为可运行的工程代码。对于个人开发者,它降低编码负担,专注业务逻辑设计;对于中小型团队,不需要复杂二次开发,就可以引入AI研发能力,缩短项目交付周期;对于大型企业,企业级版本提供权限管控、审计、私有知识库,在合规前提下放大AI带来的研发收益。随着智能体技术持续迭代,编程智能体会深
717 0
|
9月前
|
机器学习/深度学习 存储 自然语言处理
从位宽到梯度:深度学习底层精度的“平滑”艺术
本文介绍了浮点数的规格化与非规格化,以及混合精度的适用场景和步骤,并介绍了常见的FP16、FP32等的数据格式
750 3
|
3月前
|
存储 人工智能 弹性计算
阿里云服务器多少钱一年?2026年最新优惠服务器配置排行榜,共15台!
2026年阿里云服务器价格全面优化,覆盖38元/年入门款至万元级AI算力。本文整理15款高性价比配置,分三梯队:个人开发(No.1–5)、中小企业(No.6–10)、高性能/AI(No.11–15),含价格、配置、适用场景及避坑指南,助您精准选型。
652 0
|
6月前
|
供应链 API 开发者
电商平台api:1688 阿里巴巴平台商品详情全解析
本文深度解析1688商品详情全量数据结构(含阶梯价、MOQ、代发价、工厂信息等B2B特有字段),详解官方API(alibaba.product.get)调用与签名机制,提供Python实战采集代码,并对比淘宝、京东等主流平台接口差异,助力开发者高效构建B2B选品与供应链系统。(239字)
|
9月前
|
人工智能 弹性计算
阿里云权益中心2026年最新:个人、学生及企业用户上云补贴及活动政策解读
2026阿里云最新权益汇总:新用户38元抢轻量服务器,老用户99元享ECS,企业199元起;学生领300元无门槛券;开通百炼免费得7000万tokens;AI先锋企业享1300元抵扣金。优惠券速领→aly.wiki(239字)
1356 12
|
Ubuntu Linux Shell
Apache介绍和安装
Apache是开源的高性能Web服务器,支持跨平台部署,具备模块化、高安全性和稳定性等特点,广泛用于企业网站、电商及内容管理系统。本文详细介绍其安装配置方法,涵盖主流Linux系统下的源码编译与包管理安装步骤。
1494 0
Apache介绍和安装
|
安全 Go 开发者
“不要通过共享内存来通信”——深入理解Golang并发模型与CSP理论
Golang 采用 CSP 理念,主张“通过通信共享内存”,以消息传递替代共享内存,避免数据竞争。其核心为 Goroutine 与 Channel:轻量协程并发执行,通道安全传递数据,将并发复杂性转为通信编排,提升程序清晰度与可维护性。
993 0