图书管理系统

简介: 图书管理系统

Book

package Book;
public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;
    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 this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAuthor() {
        return this.author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public int getPrice() {
        return this.price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getType() {
        return this.type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public void setBorrowed(boolean borrowed) {
        this.isBorrowed = borrowed;
    }
    public boolean isBorrowed() {
        return this.isBorrowed;
    }
    public String toString() {
        return "Book{name='" + this.name + '\'' + ", author='" + this.author + '\'' + ", price=" + this.price + ", type='" + this.type + '\'' + "," + (this.isBorrowed ? "已经被借出" : "未被借出") + '}';
    }
}

BookList

package Book;
public class BookList {
    private Book[] books = new Book[10];
    private int size;
    public BookList() {
        this.books[0] = new Book("三国演义", "罗贯中", 20, "小说");
        this.books[1] = new Book("挪威的森林", "村上春树", 100, "小说");
        this.books[2] = new Book("活着", "余华", 20000, "小说");
        this.size = 3;
    }
    public Book getBook(int pos) {
        return this.books[pos];
    }
    public void setBook(Book book, int pos) {
        this.books[pos] = book;
    }
    public int getSize() {
        return this.size;
    }
    public void setSize(int size) {
        this.size = size;
    }
}

Operation

public class AddOperation implements IOperation {
    public AddOperation() {
    }
    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("请输入价格:");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入类型:");
        String type = scanner.nextLine();
        Book newbook = new Book(name, author, price, type);
        bookList.setBook(newbook, bookList.getSize());
        bookList.setSize(bookList.getSize() + 1);
        System.out.println("添加成功!");
    }
}
public class BorrowOperation implements IOperation {
    public BorrowOperation() {
    }
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        System.out.println("请输入你要借阅的图书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = bookList.getSize();
        for(int i = 0; i < size; ++i) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name) && !book.isBorrowed()) {
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("您所要借阅的书不存在或已经被借出");
    }
}
public class DelOperation implements IOperation {
    public DelOperation() {
    }
    public void work(BookList bookList) {
        System.out.println("删除图书!");
        int size = bookList.getSize();
        if (size == 0) {
            System.out.println("书架为空,无法删除!!");
        } else {
            System.out.println("请输入你要删除的书:");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            int index = -1;
            int i;
            Book book2;
            for(i = 0; i < size; ++i) {
                book2 = bookList.getBook(i);
                if (book2.getName().equals(name)) {
                    index = i;
                    break;
                }
            }
            if (index == -1) {
                System.out.println("没有你要删除的书");
            } else {
                for(i = index; i < size - 1; ++i) {
                    book2 = bookList.getBook(i + 1);
                    bookList.setBook(book2, i);
                }
                bookList.setSize(size - 1);
                System.out.println("删除成功");
            }
        }
    }
}
public class ExitOperation implements IOperation {
    public ExitOperation() {
    }
    public void work(BookList bookList) {
        int size = bookList.getSize();
        for(int i = 0; i < size; ++i) {
            bookList.setBook((Book)null, i);
        }
        bookList.setSize(0);
        System.exit(0);
    }
}
public class FindOperation implements IOperation {
    public FindOperation() {
    }
    public void work(BookList bookList) {
        System.out.println("查询图书!");
        System.out.println("请输入你要查找的书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = bookList.getSize();
        for(int i = 0; i < size; ++i) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                System.out.println(book);
                return;
            }
        }
        System.out.println("查无此书!");
    }
}
import Book.BookList;
public interface IOperation {
    void work(BookList var1);
}
public class ReturnOperation implements IOperation {
    public ReturnOperation() {
    }
    public void work(BookList bookList) {
        System.out.println("归还图书!");
        System.out.println("请输入你要归还的图书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = bookList.getSize();
        for(int i = 0; i < size; ++i) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name) && book.isBorrowed()) {
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
        System.out.println("您所要归还的图书已经被归还");
    }
}
public class ShowOperation implements IOperation {
    public ShowOperation() {
    }
    public void work(BookList bookList) {
        int size = bookList.getSize();
        for(int i = 0; i < size; ++i) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

User

public class AdminUser extends User {
    IOperation[] iOperations = new IOperation[]{new ExitOperation(), new FindOperation(), new DelOperation(), new AddOperation(), new ShowOperation()};
    public AdminUser(String name) {
        super(name);
    }
    public int menu() {
        System.out.println("============================");
        System.out.println("Hello " + this.name + " 欢迎来到图书管理系统!");
        System.out.println("请输入你要进行的操作:\n1.查询图书\n2.删除图书\n3.添加图书\n4.显示图书\n0.退出系统");
        System.out.println("============================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
    public void doOperation(int choice, BookList bookList) {
        this.iOperations[choice].work(bookList);
    }
}
package User;
import Book.BookList;
import Operation.BorrowOperation;
import Operation.ExitOperation;
import Operation.FindOperation;
import Operation.IOperation;
import Operation.ReturnOperation;
import java.util.Scanner;
public class NormalUser extends User {
    IOperation[] iOperations = new IOperation[]{new ExitOperation(), new FindOperation(), new BorrowOperation(), new ReturnOperation()};
    public NormalUser(String name) {
        super(name);
    }
    public int menu() {
        System.out.println("============================");
        System.out.println("Hello " + this.name + " 欢迎来到图书管理系统!");
        System.out.println("请输入你要进行的操作:\n1.查询图书\n2.借阅图书\n3.归还图书\n0.退出系统");
        System.out.println("============================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
    public void doOperation(int choice, BookList bookList) {
        this.iOperations[choice].work(bookList);
    }
}
public abstract class User {
    protected String name;
    public IOperation[] iOperations;
    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    public void doOperation(int choice, BookList bookList) {
        this.iOperations[choice].work(bookList);
    }
}


目录
相关文章
|
资源调度 监控 负载均衡
浅析PM2实用入门指南
PM2 是一个守护进程管理器,可以用它来管理你的node进程,负责所有正在运行的进程,并查看node进程的状态,也支持性能监控,负载均衡等功能。使用起来也是非常简单
2574 0
|
4月前
|
SQL 关系型数据库 MySQL
全量迁移时源库还在写,数据一致性怎么保证?
数据库迁移最怕的不是慢,是“搬完了发现数据不对”。全量迁移时源库还在写、增量同步时顺序乱了、异构数据库类型映射丢了精度——这些坑,在POC阶段很难暴露,一上生产就变成事故。本文从三种一致性风险场景出发,拆解全量校验、增量校验、抽样校验的完整方法论,帮助读者在迁移项目中做到“数据搬得对、心里有底”。
|
7月前
|
SQL 运维 监控
生产环境改了个配置,半小时后业务挂了——变更管理到底管什么
统计过去半年的P1/P2故障,接近4成是配置变更引发的。本文从一次连接池参数误改导致的生产事故出发,拆解最小可用的变更管理4个动作——分级、记录、通知、关联,不搞ITIL全套流程,先让变更可追溯、可关联、出了事能3分钟定位。
1071 1
生产环境改了个配置,半小时后业务挂了——变更管理到底管什么
|
6月前
|
SQL 关系型数据库 MySQL
别再被死锁坑了!3个最常见的死锁场景及一键避坑清单
详解MySQL死锁:本质是事务循环等待资源,InnoDB自动回滚牺牲者。通过`SHOW ENGINE INNODB STATUS`定位锁链,结合索引优化、统一加锁顺序、降隔离级(RC)、加重试等手段可有效防控。死锁非故障,而是高并发下的必然现象。
|
9月前
|
存储 弹性计算 缓存
阿里云服务器通用算力型u1、u2i、u2a有何区别?实例性能、适用场景及选购指南参考
阿里云通用算力型U实例包含u1、u2i和u2a三款云服务器,均具均衡资源配置和高性价比,但各有差异。u1是经典款,以稳定性和成本效益为优势,适合预算有限且对稳定性有要求的中小企业;u2i是新一代Intel平台实例,算力与I/O性能显著提升,适合对性能有更高要求的企业级应用;u2a则采用AMD平台,追求极致性价比,适合对成本敏感且追求高性能的用户。用户可根据需求和预算选择。
|
10月前
|
人工智能 运维 数据可视化
万小智到底有多好用?阿里云建站全攻略:场景、优势、价格、FAQ 一文搞定
阿里云万小智AI建站是零代码自助建站平台,输入需求即可AI生成网站初稿,支持可视化编辑与一键发布。覆盖企业官网、营销落地页、轻量电商、个人作品集四大场景,集成AI设计、配图、客服、内容创作能力,提供全托管服务,免运维、高安全、快访问。
943 5
|
人工智能 算法 架构师
你的团队是"精锐特种兵",还是"草台班子"?就差这一份"源代码"
针对技术团队管理混乱、过度依赖个人的痛点,提出用AI指令将经验转化为标准SOP的解决方案。通过工程化思维重构管理流程,实现团队经验的"开源"与"复用",释放核心人才价值。
831 10
|
人工智能 Java API
阿里云 0 元领取 100 万 Tokens,零门槛体验 DeepSeek-R1 满血版
阿里云开放DeepSeek-R1满血版体验,0门槛领取100万Tokens,参数规模6710亿,性能强劲且完全免费。用户可轻松写代码、做表格、写故事、逻辑推理等。重点是无需任何编程基础,学生党、职场新人皆可使用。通过注册阿里云账号、生成API Key并使用HiFox客户端,即可快速上手体验顶级AI助手的强大功能。限时福利,赶快行动!
2197 65
|
机器学习/深度学习 算法 安全
小场景大市场:猫狗识别算法在宠物智能设备中的应用
将猫狗识别算法应用于宠物智能设备,是AIoT领域的重要垂直场景。本文从核心技术、应用场景、挑战与趋势四个方面,全面解析这一融合算法、硬件与用户体验的系统工程。
1175 0
|
监控 安全 架构师
JVM 17 调优指南:如何进行JVM调优,JVM调优参数
在这篇文章中,我会详细介绍JVM调优的概念、重要性和具体的JVM调优参数。此外,我将提供12个实用的代码示例,每个示例都会包含JVM调优参数和相应的Java代码。JVM调优是调整和配置Java虚拟机(JVM)的过程,以便最大限度地提高应用程序的性能和效率。这涉及到调整内存设置、选择合适的垃圾收集器,以及配置各种性能参数。
1578 0