首页 > 基础资料 博客日记

【JavaSE】图书管理系统

2024-07-04 17:00:06基础资料围观210

本篇文章分享【JavaSE】图书管理系统,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识


最终效果

图书管理系统

book包

该包中有两个类,用来对书进行管理。

Book类

从开头视频可以看见,每本书有书名,作者,类型,价格,是否借出。我们将它们定义出来,并有快捷键获得get和set方法。
在生成一个构造方法,含书名,作者,类型,价格。
然后再快捷键重写Object的equals方法,重写toString方法。
注意:
因为直接重写的equals方法是对所有成员变量都进行对比,但是我们不需要对比借出状态(如果对比了那还怎么进行借阅和归还操作)。
还有toString方法我们要将借出的boolean类型转换为是否借出输出,就需要改为3目操作符( (this.isBorrowed)?", 已借出":", 未借出")

最终该类代码就是:

package book;

import java.util.Objects;
public class Book {
    private String name;
    private String author;
    private String type;
    private int price;
    private boolean isBorrowed;

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

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return price == book.price && Objects.equals(name, book.name) && Objects.equals(author, book.author) && Objects.equals(type, book.type);
    }


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

BookList类

在这个类中我们就将书给管理起来(简易顺序表),所以成员就用一个Book数组来存书,在用一个usedSize来表示存入书的数目,提供get和set方法。并在构造方法中初始化几本书。

BookList类最终代码:

package book;
public class BookList {
    private int usedSize;
    Book[] books = new Book[10];

    public int getUsedSize() {
        return usedSize;
    }

    public BookList() {
        this.books[0] = new Book("西游记","吴承恩","小说",66);
        this.books[1] = new Book("红楼梦","曹雪芹","小说",77);
        this.books[2] = new Book("三国演义","罗贯中","小说",88);
        this.books[3] = new Book("坤拳录","cxk","修仙秘籍",25);
        this.usedSize = 4;//在创建书架时直接默认存在3本书
    }

    public Book[] getBooks() {
        return books;
    }

    public void setBooks(Book[] books) {
        this.books = books;
    }

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

user包

该包用来管理使用者。含有3个类,抽象类User来作为管理员类和普通用户类的父类抽取共性。

User类

每一个管理员和普通用户都有名字,还有展示效果中选择了自己身份后会跳出菜单来让你选择操作。再用一个方法来表示调用的操作(因为我们将每一个操作封装为一个类来操作),用IOpertion数组来存入子类的操作。

User类最终代码:

package user;

import book.BookList;
import operation.IOperation;
public abstract class User {
    String name;
    IOperation[] iOperations;
    public abstract int menu();
    public abstract void doOpertion(int choice, BookList bookList);
    public User(String name) {
        this.name = name;
    }
}

AdmiUser类(管理员类)

该类继承User类并且将父类的抽象方法重写,在该方法中调用对应类的具体工作方法(自己实现的work方法)。并且在自己的构造方法中将IOpertion接口实现的数组初始化(根据提供的操作对应的数字,数字对应下标初始为对应操作实现的类)。

AdmiUser类最终代码:

package user;

import book.BookList;
import operation.*;

import java.util.Scanner;

import java.util.Scanner;
public class AdmiUser extends User  {
    public AdmiUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOpertion(),
                new FindOpertion(),
                new AddOpertion(),
                new DeletOpertion(),
                new ShowOpertion()
        };

    }
    @Override
    public void doOpertion(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
    @Override
    public int menu() {
        int choice = -1;
        while(true) {
            System.out.println("欢迎 " + this.name + " 来到图书管理系统");
            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("-----------------------------");
            System.out.println("请输入您的操作");
            Scanner scanner = new Scanner(System.in);
            choice = scanner.nextInt();
            if(choice >=0 && choice <= 4){
                break;
            }else {
                System.out.println("输入错误");
            }
        }
        return choice;
    }
}

NormalUser类(普通用户类)

在该类中与管理员类实现一致,只是在设计实现自己的操作时改一下就好。

NormalUser类最终代码:

package user;

import book.BookList;
import operation.*;

import java.util.Scanner;
public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOpertion(),
                new FindOpertion(),
                new BorrowOpertion(),
                new ReturnOpertion(),
                new ShowOpertion()
        };
    }

    @Override
    public int menu() {
        int choice = -1;
        while(true) {
            System.out.println("欢迎 " + this.name + " 来到图书管理系统");
            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("-----------------------------");
            System.out.println("请输入您的操作");
            Scanner scanner = new Scanner(System.in);
            choice = scanner.nextInt();
            if(choice >=0 && choice <= 4){
                break;
            }else {
                System.out.println("输入错误");
            }
        }
        return choice;
    }

    @Override
    public void doOpertion(int choice, BookList bookList) {
        this.iOperations[choice].work(bookList);
    }
}

opeeration包

在改包中实现具体的操作,对书的增加,删除,借阅,归还,查找,展示,退出程序。这7个类在加上接口。

IOperation接口

这个接口就抽取一个work的共性方法。
最终代码:

package operation;

import book.BookList;

public interface IOperation {
    void work(BookList bookList);
}

FindOpertion类(查找操作)

在该类中实现IOpertion接口,实现work方法,
先将要查找的书用临时变量存起来,然后遍历书架已有的书看是否有这本书,有就打印。

FindOpertion类最终代码:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要查找书的书名:");
        Scanner scanner = new Scanner(System.in);
        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);

        //看这本书是否已经存在
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].equals(book)){
                System.out.println(bookList.getBooks()[i].toString());
                return;
            }
        }

        System.out.println("没有该书");
    }
}

AddOpertion类(增加操作)

该操作与查找操作实现一致,只是在找到后就告诉已经有这本书了,没找到就添加,并将usedSize加1.

AddOpertion类最终代码

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要添加书的书名:");
        Scanner scanner = new Scanner(System.in);
        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);

        //看这本书是否已经存在
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].equals(book)){
                System.out.println("该书已经存在");
                return;
            }
        }
        //存入
        bookList.getBooks()[bookList.getUsedSize()] = book;
        bookList.setUsedSize(bookList.getUsedSize() + 1);
        System.out.println("添加成功");
    }
}

DeletOpertion类(删除操作)

该操作与查找操作实现一致,只是在找到后记录这个下标,并且再用一个循环(注意结束条件)用后面的书覆盖前面的,将usedSize减1,没找到就输出没有该书。

DeletOpertion类最终代码

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeletOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要删除书的书名:");
        Scanner scanner = new Scanner(System.in);
        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 now = -1;
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].equals(book)){
                now = i;
                break;
            }
        }
        //覆盖
        if(now != -1){
            for (int i = now; i < bookList.getUsedSize() - 1; i++) {
                bookList.getBooks()[i] = bookList.getBooks()[i+1];
            }
            bookList.setUsedSize(bookList.getUsedSize() - 1);
            System.out.println("删除成功");
        }else{
            System.out.println("没有该书");
        }
    }
}

BorrowOpertion类(借阅操作)

该操作与查找操作实现一致,只是在找到(这里的找到指有这本书并且状态是未借出)后就将这本书的状态改为已借出(将isBorrowed改为true),有但是已被借出就输出该书已被借出,没找到就输出没有。

BorrowOpertion类最终代码:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要借阅书的书名:");
        Scanner scanner = new Scanner(System.in);
        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);

        //看这本书是否有并未被借出,有就改状态
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].equals(book)
            && !bookList.getBooks()[i].isBorrowed()){

                bookList.getBooks()[i].setBorrowed(true);
                System.out.println("借阅成功");
                return;

            } else if (bookList.getBooks()[i].equals(book)
                    && bookList.getBooks()[i].isBorrowed()) {
                System.out.println("该书以被借出");
                return;
            }
        }
        System.out.println("没有该书");
    }
}

ReturnOpertion 类(归还操作)

该操作与查找操作实现一致,只是在找到(这里的找到指有这本书并且状态是已借出)后就将这本书的状态改为未借出(将isBorrowed改为false),有但是未被借出就输出该书未被借出,没找到就输出没有。

ReturnOpertion 类最终代码:

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要归还书的书名:");
        Scanner scanner = new Scanner(System.in);
        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);
        //看这本书是否有并未被借出
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if(bookList.getBooks()[i].equals(book)
                    && !bookList.getBooks()[i].isBorrowed()){

                System.out.println("该书并未被借出");
                return;

            } else if (bookList.getBooks()[i].equals(book)
                    && bookList.getBooks()[i].isBorrowed()) {
                bookList.getBooks()[i].setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("没有该书");
    }
}

ShowOpertion类(展示操作)

该类就只需遍历并输出就行。

package operation;

import book.BookList;

public class ShowOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            System.out.println(bookList.getBooks()[i].toString());
        }
    }
}

ExitOpertion类(退出操作)

直接调用退出函数就行

package operation;

import book.BookList;

public class ExitOpertion implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出成功");
        System.exit(0);
    }
}

Main类

在该类中写一个login函数来确定身份向上转型给User.
main主函数将操作串起来:

import book.BookList;
import user.AdmiUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {

    //使用这个函数来确定访问者身份
    private 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 AdmiUser(name);
        } else if (choice == 2) {
            return new NormalUser(name);
        }else {
            System.out.println("输入错误");
            return null;
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();

        User user = login();
        while( user == null){
            user = login();
        }//使用循环保证输入身份正确

        while (true){
        int choice = user.menu();
        user.doOpertion(choice,bookList);
        }
    }
}

最终的关系图


文章来源:https://blog.csdn.net/yj20040627/article/details/139198449
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云