图书管理系统

简介: 图书管理系统

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


目录
相关文章
|
Java 关系型数据库 MySQL
pikachu 启航篇之靶场环境准备
pikachu 启航篇之靶场环境准备
364 0
pikachu 启航篇之靶场环境准备
|
消息中间件 供应链 前端开发
业务团队如何统一架构设计风格?
首次上线应用,面对业务框架搭建你是否曾感到无从下手?维护线上应用,面对大量历史包袱你是否正避坑不及深陷泥潭?为何同样是业务应用,不同人的设计风格千差万别?为何最初的设计经过多个迭代后总是面目全非?新人来到团队,怎样才能快速了解业务,不被大量技术细节折磨?如果你也有这些困扰,希望本文能提供些许帮助。
业务团队如何统一架构设计风格?
|
JavaScript Unix 关系型数据库
Unix时间戳 POSIX时间 Unix时间
时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)以来的秒数,也被称为 Unix 时间戳(Unix Timestamp)。Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。
206950 2
|
10天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
9天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
417 130
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
3天前
|
存储 安全 前端开发
如何将加密和解密函数应用到实际项目中?
如何将加密和解密函数应用到实际项目中?
199 138
|
9天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
381 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)