[JAVA] Library management system (javaSE simple version includes drawing analysis) | Final assignment & course design

Author's homepage: paper jie's blog

Author of this article: Hello everyone, I am paper jie. Thank you for reading this article. Welcome to Yijiansanlian.

This article is included in the "JAVA" column. This column is carefully created for college students and programming novices. The author spent a lot of money (time and energy) to build it to cover all the basic knowledge of javaSE. I hope it can help readers.

Other columns: "Detailed Explanation of Algorithms", "C Language", "javaSE", etc.

Content Sharing: This issue will use javase to create a simple version of the library management system

Table of contents

design requirements

 Design ideas

The specific process of design and source code display

book bag 

Book class

Booklist class

user package

User class

AdminUser class

NormalUser class

operation package

IOoperation interface

Addoperation class

Deloperation class

Borrowoperation class

Exitoperation class

Findoperation class

Returnoperation class

Showoperation class

 Main class

Show results 

Drawing analysis of library management system


design requirements

We need to use javase to implement a simple version of the library management system, which needs to have the following functions:

1 First, you need to have two modes, one is the administrator mode and the other is the ordinary user mode.

2 It is necessary to have the functions of adding books, deleting books, searching books, displaying books, borrowing books, returning books, and exiting the system.

3. The above functions need to be divided into two parts. Add, delete, display, search, and exit are the permissions of the administrator, while search, borrow, return, and exit are the permissions of ordinary users.

 Design ideas

1 We first need to divide our users into two parts, one is the administrator and the other is the ordinary user. When using it, different interfaces and functions are displayed according to different needs. Here we can use inheritance, polymorphism, and upward transformation. Give them a parent class to implement

2 We need to implement multiple classes to implement the functions of deleting books, searching for books, displaying books, borrowing books, returning books, and exiting the system.

3 We create an interface, let the functional class implement this interface, and then use an array of the created interface type to store the new objects of the functional class, so that unified management can be carried out.

4 We also need to create book and bookshelf classes to encapsulate the properties of the books. The bookshelf class stores books and will be stored in an array of bookshelf type.

The specific process of design and source code display

In order to decomplicate the program, Yi Modification will use three packages and one main class to build this framework. There will be a book package, which stores books and bookshelf classes. The operation package stores various functional classes and interfaces. The user package contains the parent class User class, ordinary user class, administrator class, and finally a main class Main

book bag 

Book class

The function of the book class is to encapsulate the book class so that it can be easily placed on the bookshelf later. The construction method, getandset method and toString method here can be generated by the compiler, which is very fast. In the toString method, we need to change what the compiler generates. We can use the ternary operator to determine whether it is true or false to print whether it has been borrowed or not.

package book;


public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    //默认flose 未被借出
    private boolean isBorrowed;
    //get 和 set方法设置和返回成员变量
    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 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;
    }
    //打印
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", " + ((isBorrowed == true) ? "已经借出" : "未接出") +
                '}';
    }
}

Booklist class

You can also use the compiler to generate the required code here. What you should pay attention to here is the books array, which is used to store books. It needs to be initialized in the construction method, and the contents of several books can be stored.

package book;


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("西游记", "吴承恩", 20, "小说");
        this.books[2] = new Book("红楼梦", "曹雪芹", 30, "小说");
        this.usedsize = 3;
    }

    public int getUsedsize() {
        return usedsize;
    }

    public void setUsedsize(int usedsize) {
        this.usedsize = usedsize;
    }

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

    public void setBook(int pos, Book book) {
        books[pos] = book;
    }

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

user package

This package contains a parent class and two subclasses, attempting to switch between administrators and ordinary users through inheritance and polymorphism.

User class

What should be noted here is the IOoperation array, which will be initialized in a subclass of the User class to store objects that require functional classes. The dooperation method implements the required operations through the work method of the members in the IOoperations array.

package user;

import book.Booklist;
import operation.IOoperation;


public abstract class User {
    protected String name;
    protected IOoperation[] iOoperations;

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

    public void dooperation(int choise, Booklist booklist) {
        iOoperations[choise].work(booklist);
    }
}

AdminUser class

It and NormalUser are both subclasses of User. The construction method here performs IOoperation initialization on the members of the parent class, and implements the menu interface. The return value of the menu method is the function you choose to use.

package user;

import operation.*;

import java.util.Scanner;


public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
        this.iOoperations = new IOoperation[]{new Exitoperation(),new Addopertaion(),
                            new Deloperation(), new Showoperation(),new Findoperation()};
    }

    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("************************************************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

NormalUser class

The implementation idea is the same as AdminUser

package user;

import operation.*;

import java.util.Scanner;


public class NormalUser extends User {

    public NormalUser(String name) {
        super(name);
        this.iOoperations = new IOoperation[]{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("**************************************************************");
        System.out.println("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

operation package

This package needs to implement the required functional classes and an interface that connects them all.

IOoperation interface

All functional classes must implement this interface and rewrite the work method to uniformly use the name work.

package operation;

import book.Booklist;


public interface IOoperation {
    public void work(Booklist booklist);
}

Addoperation class

New book features:

package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sun杰
 * Date: 2023-08-25
 * Time: 10:43
 */
public class Addopertaion implements IOoperation{
    @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 pos = booklist.getUsedsize();
        for (int i = 0; i < pos; i++) {
            Book book1 = booklist.getBook(i);
            if(book1.getName().equals(name)) {
                System.out.println("已有这本书,不再存放");
                return;
            }
        }
        if(pos == booklist.getBooks().length) {
            System.out.println("书架满了");
        }else {
            booklist.setBook(pos, book);
            booklist.setUsedsize(pos + 1);
        }
    }
}

Deloperation class

Delete book function:

import book.Booklist;

import java.util.Scanner;


public class Deloperation implements IOoperation{
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = booklist.getUsedsize();
        for (int i = 0; i < size; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)) {
                int pos = i;
                int j = 0;
                for ( j = pos; j < size - 1; j++) {
                    Book book1 = booklist.getBook(j+1);
                    booklist.setBook(j, book1);
                }
                booklist.setBook(j, null);
                booklist.setUsedsize(size - 1);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("不存在这本书");

    }
}

Borrowoperation class

Borrowing books function:

package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

public class Borrowoperation implements IOoperation {
    @Override
    public void work(Booklist booklist) {
        System.out.println("借阅图书:");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要借阅的图书:");
        String name = scanner.nextLine();
        int size = booklist.getUsedsize();
        for (int i = 0; i < size; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)) {
                book.setBorrowed(true);
                System.out.println(book);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("该书不存在");
    }
}

Exitoperation class

Exit system function:

package operation;

import book.Booklist;


public class Exitoperation implements IOoperation{
    @Override
    public void work(Booklist booklist) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

Findoperation class

Find book function:

package operation;

import book.Book;
import book.Booklist;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import javax.imageio.metadata.IIOMetadataController;
import java.util.Scanner;


public class Findoperation implements IOoperation{
    @Override
    public void work(Booklist booklist) {
        System.out.println("查找图书:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int size = booklist.getUsedsize();
        for (int i = 0; i < size; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("找到了,信息如下:");
                System.out.println(book);
                return;
            }

        }
        System.out.println("上述不存在");
    }
}

Returnoperation class

Return book function:

package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;


public class Returnoperation implements IOoperation{
    @Override
    public void work(Booklist booklist) {
        System.out.println("归还图书:");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要归还的图书:");
        String name = scanner.nextLine();
        int size = booklist.getUsedsize();
        for (int i = 0; i < size; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)) {
                book.setBorrowed(false);
                System.out.println(book);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("该书不存在");
    }
}

Showoperation class

Show book features:

package operation;

import book.Book;
import book.Booklist;


public class Showoperation implements IOoperation {
    @Override
    public void work(Booklist booklist) {
        System.out.println("显示图书:");
        int size = booklist.getUsedsize();
        for (int i = 0; i < size; i++) {
            Book book = booklist.getBook(i);
            System.out.println(book);
        }
    }
}

 Main class

Program execution is organized through the Main class:

import book.Book;
import book.Booklist;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;


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 = login();
        while(true) {
            int choice = user.menu();
            //choice来决定调用哪个方法类
            user.dooperation(choice, booklist);
        }

    }
}

Show results 

Drawing analysis of library management system


Guess you like

Origin blog.csdn.net/paperjie/article/details/132565428