基于Java封装继承多态实现的一个简单图书系统

首先我们大概了解下图书系统的需求

1.要有两种身份 管理员和普通用户。普通用户和管理员分别对应的功能不一样,需要分开实现

  1. 图书系统肯定要有图书,和存放图书的地方,存放就用数组来实现

3.实现对应用户的功能

接下来我们第一步:

首先创建一个book的包,里面存放图书类和存放图书的列表

在图书类中我们要有图书的属性,我们这边简易版只有图书名,作者,类型,价格和图书状态(借出,未被借出)

因为是自己使用,为了安全性使用private修饰,然后构造get,set方法来进行set值和get值,在构造一个toString方法,方便后面来打印,还有一个带参数的构造方法,方便后面进行新建图书

接下来实现存放图书

首先需要创建一个数组来存放书,还需要记录当前书的数量,然后我们可以给数组一个默认的长度,和初始化图书,在初始化时就有几本默认的书,我们需要一个通过下标获取到数组中的书

图书的定义初始化和存放做完后,我们来实现用户

用户分为管理员用户和普通用户,管理员用户和普通用户都是用户,我们就可以定义一个父类User类,然后字类AdminUser和NormalUser来继承父类中共有的方法

父类中定义姓名,然后创建一个User的构造方法,doOperation方法后面会讲到

现在我们要创建我们要实现功能的类,因为每个类都要使用work(BookList bookList)的方法,我们封装一个接口给其他功能类使用

然后逐次创建,继承IOPeration接口,创建完后我们来实现主函数

主函数里要定义一个登陆方法,然后返回用户给系统

因为登陆函数返回了用户,我们主函数调用登陆方法,第一步知道是进入哪个用户,然后通过choice来决定调用的是哪个方法,doOperation这个方法在上面已经实现,传入choice和书的列表,choice则是通过在普通用户类和管理员用户类中的menu()方法,然后输入,通过choice来接收,传给了doOperation方法,根据choice选择对应的方法

接下来我们挨个实现展示图书,查找图书,新增图书,归还图书,借阅图书,退出图书系统,删除图书的各项功能。

展示图书:

查找图书:

新增图书:

借阅图书:

删除图书:

退出系统:

归还图书:

接下来我给完整版代码:

Book类:

java 复制代码
package book;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 21:49
 */
public class Book {
    private String name;
    private String author;
    private String type;
    private int price;
    private boolean isBorrowed;


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

    public Book(String name, String author, String type, int price) {
        this.name = name;
        this.author = author;
        this.type = type;
        this.price = price;
    }

    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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }
    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }
}
复制代码
BookList类:
java 复制代码
package book;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 21:49
 */
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("西游记","吴承恩","小说",120);
        this.books[2] = new Book("红楼梦","曹雪芹","小说",110);
        this.books[3] = new Book("斗破苍穹","天蚕土豆","小说",20);

        this.usedSize = 4;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    //获取数组某一个下标的书
    public Book getBook(int pos){
//        if (pos >= 0){
//            return books[pos];
//        } else {
//            System.out.println("下标错误");
//            //return new Exception();
//            //throw new Exception("下标错误"); //抛出异常
//        }
        return books[pos];
    }

    //通过数组的下标的位置来放书
    public void setBooks(int pos,Book book){
        books[pos] = book;
    }

    public Book[] getBooks() {
        return books;
    }
}
复制代码
User类:
java 复制代码
package user;

import book.BookList;
import operation.IOPeration;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:12
 */
public abstract class User {
    protected  String name;

    //定义一个存放方法的数组
    protected IOPeration[] ioPerations;

    public User(String name){
        this.name = name;
    }

    public abstract int menu();

    //通过传入choice来判断调用的方法
    public void doOperation(int choice, BookList bookList)  {
        ioPerations[choice].work(bookList);
    }
}
复制代码
AdminUser类:
java 复制代码
package user;

import operation.*;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:14
 */
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                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("*************");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的操作");
        int choice = scanner.nextInt();

        return choice;
    }
}

NormalUser类:

java 复制代码
package user;

import operation.*;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:14
 */
public class NormalUser extends User {
    public NormalUser(String name) {
        super(name);
        this.ioPerations = new IOPeration[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()

        };

    }

    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("*************");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的操作");
        int choice = scanner.nextInt();

        return choice;
    }
}

主函数:

java 复制代码
import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 23:18
 */
public class Main {
    public static User login(){
        System.out.println("请输入用户名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入你的身份: 1 管理员    2 普通用户");
        int choice = scanner.nextInt();

        if (choice == 1){
            //管理员
            return new AdminUser(name);
        } else{
            //普通用户
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        //user 指向哪个对象,看返回值是什么?
        User user = login();
        while (true){
            int choice = user.menu();
            //System.out.println("choice: " + choice);
            //根据choice的选择来决定调用的是哪个方法
            user.doOperation(choice,bookList);
        }



    }
}

接口

复制代码
IOPeration:
java 复制代码
package operation;

import book.BookList;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:06
 */
public interface IOPeration {
    void work(BookList bookList);
}

功能实现类:

AddOperation:

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:02
 */
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,type,price);

        //检查数组当中有没有这本书
        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);
            //将书的数量加1
            bookList.setUsedSize(currentSize+1);
        }
        System.out.println("新增成功");
    }
}
复制代码
BorrowOperation:
java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:04
 */
public class BorrowOperation 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 curredSize = bookList.getUsedSize();

        int i = 0;

        for (; i < curredSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                if (book.isBorrowed() == false){
                    book.setBorrowed(true);
                    System.out.println("借阅成功");
                    System.out.println(book);
                    return;
                } else {
                    System.out.println("图书已被借阅,不能再次借出");
                    return;
                }

            }
        }
        System.out.println("你借阅的图书不存在");

    }
}
复制代码
DelOperation:
java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:02
 */
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 i = 0;
        int currentSize = bookList.getUsedSize();
        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++) {
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }

        //将j置为空
        bookList.setBooks(j,null);
        //将书的数量减1
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}
复制代码
ExitOperation:
java 复制代码
package operation;

import book.BookList;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:03
 */
public class ExitOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出图书系统!");
        System.exit(0);
    }
}
复制代码
FindOperation:
java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:02
 */
public class FindOperation 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)){
                System.out.println("找到了这本书,信息如下");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有找到这本书");
    }
}
复制代码
ReturnOperation:
java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:04
 */
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 curredSize = bookList.getUsedSize();

        int i = 0;

        for (; i < curredSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                if (book.isBorrowed() == true){
                    book.setBorrowed(false);
                    System.out.println("归还成功");
                    System.out.println(book);
                    return;
                } else {
                    System.out.println("该图书没有被借出,无需归还");
                    return;
                }

            }
        }
        System.out.println("你归还的图书不存在");
    }
}
复制代码
ShowOperation:
java 复制代码
package operation;

import book.Book;
import book.BookList;

/**
 * @author xiaofan
 * @version 1.0
 * @date 2023/11/14 22:03
 */
public class ShowOperation implements IOPeration{
    @Override
    public void work(BookList bookList) {
        //System.out.println("展示图书!");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

仓库地址:Java学习仓库,从入门到精通: Java入门学习 - Gitee.com

相关推荐
残月只会敲键盘2 分钟前
面相小白的php反序列化漏洞原理剖析
开发语言·php
ac-er88884 分钟前
PHP弱类型安全问题
开发语言·安全·php
ac-er88885 分钟前
PHP网络爬虫常见的反爬策略
开发语言·爬虫·php
爱吃喵的鲤鱼15 分钟前
linux进程的状态之环境变量
linux·运维·服务器·开发语言·c++
LuckyLay21 分钟前
Spring学习笔记_27——@EnableLoadTimeWeaving
java·spring boot·spring
向阳121833 分钟前
Dubbo负载均衡
java·运维·负载均衡·dubbo
DARLING Zero two♡41 分钟前
关于我、重生到500年前凭借C语言改变世界科技vlog.16——万字详解指针概念及技巧
c语言·开发语言·科技
Gu Gu Study43 分钟前
【用Java学习数据结构系列】泛型上界与通配符上界
java·开发语言
芊寻(嵌入式)1 小时前
C转C++学习笔记--基础知识摘录总结
开发语言·c++·笔记·学习
WaaTong1 小时前
《重学Java设计模式》之 原型模式
java·设计模式·原型模式