Home > Java > javaTutorial > Detailed example of implementing a simple version of the library management system in Java

Detailed example of implementing a simple version of the library management system in Java

WBOY
Release: 2022-07-15 20:33:10
forward
1805 people have browsed it

This article brings you relevant knowledge about java, which mainly organizes the related issues of implementing a simple version of the library management system, including analyzing the functions of the library management system, and in IDEA Let’s take a look at the creation of functional classes, user-related processing, etc. I hope it will be helpful to everyone.

Detailed example of implementing a simple version of the library management system in Java

Recommended study: "java video tutorial"

1. Analyze the functions of the library management system

us Let’s first analyze the functions that a library management system should have and build a simple framework.

(1) Login

Normally, only two types of people can use the library management system, one is the student and the other is the librarian

This is the login interface of my school’s online library. Students can search for books through the Internet.

But I can’t see the administrator’s login interface here. But there will definitely be a window for the backend administrator to log in for system maintenance

So depending on the user, you have to choose whether you are an ordinary user or an administrator when logging in.

(2) Analysis function

The functions that a simple library management system should have,

2. Create functional classes in IDEA

2.1 Create a package named book, which stores book-related information

( 1) Create a Book class to display the attributes of the book

To search for a book, a book should have these attributes

    private String name;//书名
    private String author;//作者
    private int price;//价格
    private String type;//类型
    private boolean isBorrowed;//借阅情况
Copy after login

Note that the control characters provided for the book here are private and cannot be accessed outside the class

So we need to provide get() and set() to set and obtain the properties

    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 int getPrice() {
        return price;
    }

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

    public String getType() {
        return type;
    }

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

    public boolean isBorrowed() {
        return isBorrowed;
    }

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

Provide a construction method for the book's attributes.

Note here that isBorrowed is not added to the book in the construction method. isBorrowed is of boolean type and defaults to false. It just hasn’t been lent out. If you condition a book, it will default to not being lent out

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

Finally, provide a toString method to display the book information

    @Override
    public String toString() {
        return "BookList{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }
Copy after login

(2) Create a BookList class, this is the book library

Because it is a book library and it needs to store books, so set up an array to store the books

   //最多存放20本书
    private Book[] books = new Book[20];
Copy after login

And provide a member variable, To record the number of books in the current books array in real time

 private int usedSize;//实时记录 当前Books这个数组中有多少本书
Copy after login

The following can provide a construction method to store a few books in it

    public BookList() {
        books[0] = new Book("西游记","吴承恩",25,"小说");
        books[1] = new Book("红楼梦","曹雪芹",26,"小说");
        books[2] = new Book("三国演义","罗贯中",27,"小说");
        books[3] = new Book("水浒传","施耐庵",28,"小说");
        usedSize = 4;//当前有4本书
    }
Copy after login

Provide a method, if a legal Array subscript, you can find this book

    public Book getBook(int pos) {
        return books[pos];
    }
Copy after login

Provide a method, give a legal array subscript, and a book, you can store the book in the library

    public void setBooks(int pos,Book book) {
        books[pos] = book;
    }
Copy after login

Provide a method and a parameter to modify the number of books on the current bookshelf in real time

    public void setUsedSize(int size) {
        usedSize = size;
    }
Copy after login

2.2 Create a package named Operation, which stores all operations on books

(1) Create an IOperation interface to implement the operation reference of the array

Because whether it is an administrator or an ordinary user, the operations on books are performed in the array books of the BookList class ,

Therefore, an IOperation interface can be provided to implement the operation of the array,

public interface IOperation {
    /*
    * 对书的所有操作都是通过BookList类里面的books数组进行操作
    * 可以将这个写成一个接口
    **/
    void work(BookList bookList);
}
Copy after login

(2) Create various classes to implement All operations on books

For example, ordinary users and administrators have to perform display operations on books. This display is an effect,

So you only need to write a class, ordinary Both users and administrators can call it.

Together, just create these classes,

然后就可以对这些类引用接口了,再重写一下

比如新增图书     

public class AddOperation implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书!");
    }
}
Copy after login

 3.进行用户相关的处理

也就是对普通用户和管理员进行处理

(1)创建一个user的包,在包中创建一个类

这里只创建一个类,是因为对于普通用户和管理员来说,他们两个都是用户

所以创建一个成员变量,来表示用户

    //写protected是后面要继承
    protected String name;//用户名
Copy after login

下面提供一个构造方法对其初始化

    //提供一个构造方法,用来初始化当前对象name属性
    public User(String name) {
        this.name = name;
    }
Copy after login

(2)在user包中再创建两个类

子类NormalUser继承父类User,提供一个构造方法来显示帮助父类进行构造

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
    }
}
Copy after login

 子类AdminUser继承父类User和前面一样

下来就是打印菜单了,根据两个用户所需功能进行打印菜单

先看AdminUser管理员的

    public int menu() {
        System.out.println("hello " + this.name + "欢迎进入图书管理系统!");
        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);
        int choice = scanner.nextInt();
        return choice;
        //因为这个是个菜单,所以要把这个输入的值传出去,才能使用
    }
Copy after login

再看NormalUser普通用户的

        System.out.println("hello " + this.name + "欢迎进入图书管理系统!");
        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);
        int choice = scanner.nextInt();
        return choice;
        //因为这个是个菜单,所以要把这个输入的值传出去,才能使用
Copy after login

 (3)单独创建一个Main的类,将前面所有整合起来

菜单用户都有了,下面就是要把这些都整合起来,

先准备图书

  BookList bookList = new BookList();//准备图书
Copy after login

结下来就是登录了,

先写一个判断你是普通用户还是管理员的方法

    public static User login() {
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入你的身份:1:-》管理员.0:-》普通用户");
        int choice = scanner.nextInt();

        if (choice == 1) {
             return new AdminUser(name);
        }else {
            return new NormalUser(name);
        }
    }
Copy after login

注意观察这段代码,返回类型是User,这是因为不论if中返回是AdminUser还是NormalUser,User作为父类都可以接收,这个过程就发生了向上转型

然后再在main方法中引用这个login()方法,就可以实现选择登录了

        //登录-》user这个引用 引用哪个对象看前面if
        User user = login();
Copy after login

选择完你是哪种用户后,就打印对应功能菜单

但是注意,刚刚把菜单写在了对应子类中去了

如果现在要在父类中访问,是访问不了的,所以就要在父类中也引用出菜单

public abstract class User {
    //写protected是后面要继承
    protected String name;//用户名
    //提供一个构造方法,用来初始化当前对象name属性
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();
}
Copy after login

看代码中,只需将父类写成抽象类,然后在抽象类中,引出抽象方法的菜单,

就可以在Main类中通过父类访问到菜单,这就实现了动态绑定

    public static void main(String[] args) {
        //开始整合
        BookList bookList = new BookList();//准备图书
        //登录-》user这个引用 引用哪个对象看前面if
        User user = login();

        user.menu();//动态绑定
        //要想访问子类中的菜单,那就要将父类写成抽象类,
        //然后子类重写父类的方法,才可以访问菜单
    }
}
Copy after login

然后此时代码就可以运行了

4.开始实现对书所有具体的操作功能

4.1先实现可以调用具体操作的功能方法

先在User中写一个方法,这个方法的作用是

通过某个用户,访问这个用户对应方法功能的数组下标,然后通过调用work方法,来实现功能

    public void doOperation(int choice, BookList bookList) {
        this.iOperations[choice].work(bookList);
    }
Copy after login

然后在mian中,通过选择用户引用这个方法

    public static void main(String[] args) {
        //开始整合
        BookList bookList = new BookList();//准备图书
        //登录-》user这个引用 引用哪个对象看前面if
        User user = login();
        int choice = user.menu();//动态绑定

        user.doOperation(choice,bookList);
    }
Copy after login

 细节可以看这个图片

下面来看一下具体细节分析

(1)mian函数先调用

(2)现在user引用,有可能是两个对象 

(3)

当引用doOperation时,根据菜单选择来访问数组元素

 (4)具体选择哪个用户根据,前面login()中输入的选择对象

 (5)根据前面选择需要的功能,调用work方法

比如这个

4.2 测试一下

现在已经整合完成了,就差具体操作功能实现了,先运行代码试试

 代码成功运行起来了,但是就用了一个功能就结束了,

   所以我们可以加一个循环,来使用多个功能

    public static void main(String[] args) {
        //开始整合
        BookList bookList = new BookList();//准备图书
        //登录-》user这个引用 引用哪个对象看前面if
        User user = login();
        while(true){
            int choice = user.menu();//动态绑定
            user.doOperation(choice,bookList);
        }
    }
Copy after login

4.3 实现单独具体的操作的功能

(1)新增图书 AppOperation类

新增一本图书我们需要考虑输入这些

不用考虑isBorrowed 因为默认状态是未被借出的

将这些属性进行输入

   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();
        System.out.println("请输入图书的类型:");
        String type = scanner.nextLine();
Copy after login

然后将这些属性存放到new Book中 

        Book book = new Book(name,author,price,type);
Copy after login

获取当前下标,然后赋给currentSize,将前面输入的那本书放到数组下标为currentSize中,

然后给 currentSize加1

        int currentSize = bookList.getUsedSize();
        bookList.setBooks(currentSize,book);

        bookList.getUsedSize(currentSize+1);
Copy after login

 运行一下,试试看

 新增图书  AppOperation类的所有代码

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,price,type);
        int currentSize = bookList.getUsedSize();
        bookList.setBooks(currentSize,book);
        bookList.getUsedSize(currentSize+1);
        System.out.println("新增书籍成功!");
    }
}
Copy after login

(2)借阅图书 orrowOperation类

先输入要借阅图书的名字

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入借阅图书的名字:");
        String name = scanner.nextLine();
Copy after login

通过for循环遍历一遍,然后将遍历的每一本书赋给变量 book ,

再通过equals,来判断book和输入的书的名字是否相同,

如果相同就通过setBorrowed修改此时借阅状态,显示借阅成功

如果不同就显示没有这本书

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有这本书!");
Copy after login

运行一下,试试看 

借阅图书 orrowOperation类的所有代码

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 currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有这本书!");
    }
}
Copy after login

(3)删除图书 DelOperation类

输入删除图书的名字

  Scanner scanner = new Scanner(System.in);
        System.out.println("请输入删除图书的名字:");
        String name = scanner.nextLine();
Copy after login

找到图书,然后删除

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                for (int j = i; j < currentSize; j++) {
                    bookList.getBook(j);
                }
                bookList.getUsedSize(currentSize-1);//不要忘记更新图书种类

                bookList.getUsedSize(currentSize-1);
                System.out.println("删除成功!");
                return;
            }
        }
        System.out.println("没有找到要删除的图书!");
Copy after login

运行程序,试试看

 删除图书 DelOperation类的全部代码

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 currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                for (int j = i; j < currentSize; j++) {
                    bookList.getBook(j);
                }
                bookList.getUsedSize(currentSize-1);//不要忘记更新图书种类

                bookList.getUsedSize(currentSize-1);
                System.out.println("删除成功!");
                return;
            }
        }
        System.out.println("没有找到要删除的图书!");
    }
}
Copy after login

(4)显示图书 DisplayOperation类

将当前有几本书记录下来

        int currentSize = bookList.getUsedSize();
Copy after login

然后for循环全部遍历一遍就可以了 ,直接看代码吧

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书!");

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            System.out.println(bookList.getBook(i));
            }
        }
}
Copy after login

运行结果就是这样 

(5)退出系统 ExitOperation类

直接调用状态码exit来退出系统

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!");
        System.exit(0);
    }
}
Copy after login

 

(6)查找图书 FindOperation类

要查找图书,肯定是先要输入你需要查找书的名字

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书的名字:");
        String name = scanner.nextLine();
Copy after login

通过for循环遍历一遍,然后将遍历的每一本书赋给变量 book ,

再通过equals,来判断book和输入的书的名字是否相同,

如果相同就打印,并显示找到了,如果不相同,就直接显示没有找到,

但这里有一个问题在前面给每一本书默认都是false,现在打印还是这样,所以要修改一下

在Book类中,修改toString,给借阅状态一个三目运算符,来判断是否借出了

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

运行一下,试试

  查找图书 FindOperation类的全部代码就是

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("这本书没有找到!");
    }
}
Copy after login

(7)归还图书 ReturnOperation类

先输入要归还图书的名字

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入归还图书的名字:");
        String name = scanner.nextLine();
Copy after login

和前面借阅基本一样,修改一下setBorrowed的状态就可以了 

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
Copy after login

运行代码,试试看

<br>
Copy after login

 归还图书 ReturnOperation类的全部代码

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 currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
    }
}
Copy after login

推荐学习:《java视频教程

The above is the detailed content of Detailed example of implementing a simple version of the library management system in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template