Java实现图书管理系统

一、框架

1. 创建类

用户:管理员AdminUser 普通用户NormalUser 继承抽象类User

书:书Book 书架BookList

操作对象:书Book

2. 知识点

主要涉及的知识点:数据类型 变量 if for 数组 方法 类和对象 封装继承多态 抽象类和接口

3.利用数组放置图书

Test.Java

BookList.java

方法调用

BookList.java

Book.java

4.利用向上转型实现用户身份的选择

User.java

添加图书操作AddIOperation等类实现接口IOperations

IOperations.java

每个功能操作均需重写接口IOperations的work方法--此处以添加操作为例

NormalUser.java

AdminUser.java

Test.java

二、功能的实现------重写Work()方法

1.退出操作ExitOperation

java 复制代码
public class ExitOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

2.展示操作ShowOperation

java 复制代码
public class ShowOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("展示书籍");
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            System.out.println(bookList.getBooks()[i].toString());
        }
    }
}

3.查找操作FindOperation

java 复制代码
public class FindOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找书籍");
        System.out.println("请输入您要查找的图书名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].getName().equals(name)) {
                System.out.println(bookList.getBooks()[i].toString());
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

4.借阅操作BorrowOperation

java 复制代码
public class BorrowOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍");
        System.out.println("请输入你要借阅的图书名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if (bookList.getBooks()[i].getName().equals(name)) {
                bookList.getBooks()[i].setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("没有找到这本书");
    }
}

5.归还操作ReturnOPeration

java 复制代码
public class ReturnIOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还书籍");
        System.out.println("请输入你要归还的图书名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if (bookList.getBooks()[i].getName().equals(name)) {
                if(bookList.getBooks()[i].isBorrowed()) {
                    bookList.getBooks()[i].setBorrowed(false);
                    System.out.println("归还成功");
                    return;
                }else {
                    System.out.println("这本书已经归还了,无需再次归还");
                    return;
                }
            }

        }
        System.out.println("没有找到这本书");
    }
}

6.添加功能AddOperation

java 复制代码
public class AddIOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("增添书籍");
        //判满
        int currentSize = bookList.getUsedSize();
        if (currentSize == bookList.getBooks().length) {
            System.out.println("书架已满,添加失败");
            return;
        }
        System.out.println("请输入图书名字:");
        Scanner scanner = new Scanner(System.in);
        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);

        //判断书架有没有这本书
        for (int i = 0; i < bookList.getBooks().length; i++) {
            if(newBook.equals(bookList.getBooks()[i])) {
                System.out.println("该书已存在,添加无效");
                return;
            }
        }
        bookList.setBooks(currentSize,newBook);
        bookList.setUsedSize(currentSize+1);
        System.out.println("添加成功");
    }
}

7.删除操作DelOperation

java 复制代码
public class DelOperation implements IOperations{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        System.out.println("请输入您要删除的图书名字");
        Scanner scanner = new Scanner(System.in);
        int pos = -1;
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int i = 0;
        for (; i < currentSize; i++) {
            if(name.equals(bookList.getBooks()[i].getName())) {
                pos = i;
                break;
            }
        }
        if (i == currentSize) {
            System.out.println("没有你想删除的图书");
            return;
        }
        int j = pos;
        for (;j < bookList.getUsedSize()-1; j++) {
            Book book = bookList.getBooks()[j+1];
            bookList.setBooks(j,book);
        }
        bookList.setBooks(j,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
相关推荐
程序员zgh3 分钟前
C++ 互斥锁、读写锁、原子操作、条件变量
c语言·开发语言·jvm·c++
小灰灰搞电子18 分钟前
Qt 重写QRadioButton实现动态radioButton源码分享
开发语言·qt·命令模式
by__csdn25 分钟前
Vue3 setup()函数终极攻略:从入门到精通
开发语言·前端·javascript·vue.js·性能优化·typescript·ecmascript
喵了meme33 分钟前
C语言实战5
c语言·开发语言
盖世英雄酱581361 小时前
springboot 项目 从jdk 8 升级到jdk21 会面临哪些问题
java·后端
济南壹软网络科技有限公司1 小时前
企业级盲盒系统:Java高并发架构在多元化抽奖电商中的设计与实践
java·架构·开源源码·盲盒源码·盲盒h5·盲盒app
廋到被风吹走1 小时前
【Java】常用设计模式及应用场景详解
java·开发语言·设计模式
一条可有可无的咸鱼1 小时前
企业招聘信息,企业资讯进行公示
java·vue.js·spring boot·uni-app
Sammyyyyy1 小时前
DeepSeek v3.2 正式发布,对标 GPT-5
开发语言·人工智能·gpt·算法·servbay
Luna-player1 小时前
在前端中,<a> 标签的 href=“javascript:;“ 这个是什么意思
开发语言·前端·javascript