Code implementation - library management system

Table of contents

1 Introduction

2. Logic

2.1 Effect preview

2.2 Logic block diagram

2.3 Display of related packages

3. book bag

3.1 Book class

3.2 Booklist class

4. operation package 

4.1 IOperation interface

4.2 AddOperation

4.3 BorrowOperatio

4.4 ReturnOperation

4.5 DelOperation

 4.6 DisplayOperation

4.7 ExitOperation

4.8 FindOperation 

5. user package

5.1 User

5.2 NormalUser

5.3  AdminUser

6. Main class


1 Introduction

This time, I will share a code of a library management system that I have recently studied. I will connect the previous "classes and objects", "abstract class interfaces", and "inheritance and polymorphism" in series, so that everyone can understand more deeply. Note: In this code, we only pay attention to the collaboration of code logic and related objects, not business logic, which is just a simple implementation of functions.

2. Logic

2.1 Effect preview

2.2 Logic block diagram

2.3 Display of related packages

These include:

  • The book package stores book information, the operation package stores book operations, the user package stores user information, and the Main class integrates all information.

3. book bag

3.1 Book class

  1. We define the book class to store all the data of a book, including members such as name, author, price, type, and whether it is lent;
  2. We give the constructors of these member variables;
  3. Because these members are private modified, we give get and set methods for external classes to call;
  4. isBorrowed does not need to set the construction method, because the default is false, we can change this part to borrowed or not borrowed in toString, and the ternary operator is used here.
ublic class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;

    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)?"已经借出":"未借出" ) +
                '}';
    }
}

3.2 Booklist class

  1. Define the Booklist class, which is equivalent to a bookshelf, where books can be found or placed;
  2. The purpose of providing the construction method of Booklist is: when instantiating Booklist (instancing in the main method under the Main class), there will be three books in it;
  3. Provide the method of obtaining the book and the method of placing the book in the array respectively, pos is the subscript of the array;
  4. Provide methods to obtain the number of current books and change the number of current books;
public class BookList {
    private Book[] books = new Book[10];

    private int usedSize;//实时记录当前书还有多少本

    //初始书架中就有三本书,提供BookList的构造方法:
    public BookList(){
        books[0] = new Book("三国演义","罗贯中",19,"小说");
        books[1] = new Book("西游记","吴承恩",29,"小说");
        books[2] = new Book("红楼梦","曹雪芹",9,"小说");
        usedSize = 3;

    }

    //提供get方法,可以找到书架中的书,返回书的类型,pos是数组的下标
    public Book getBook(int pos){
        return books[pos];
    }
    //放置一本书
    public void setBook(int pos, Book book){
        books[pos] =book;
    }

    //获取当前书的本数
    public int getUsedSize() {
        return usedSize;
    }
    //实时修改当前书的个数
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
}

4. operation package 

4.1 IOperation interface

  1. In IOperation, the operation on the bookshelf is abstracted and defined as an interface because all operations are to operate the array of books, that is, to operate the bookshelf, and abstract it to form an interface. 
  2. Write an abstract method and pass in the Booklist object.
public interface IOperation {
    void work(BookList bookList);
}

4.2 AddOperation

  1. Realize the operation of adding books to the bookshelf, in which the user needs to input the name, author, type, price and other information of the book.
  2. Use the setBook method in booklist to place this new book;
  3. Finally, the number of current books needs to be +1, so just call the setUsedSize method.
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();

        int currentSize = bookList.getUsedSize();//获取当前的书的本数
        Book book = new Book(name,author,price,type);
        bookList.setBook(currentSize,book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增书籍成功!");

    }
}

4.3 BorrowOperatio

  1. Implement the operation of lending a book
  2. To traverse the books on the current bookshelf, use the book title to do the matching;
  3. If the book is found, then change the isBorrowed member in the book to true through the setBorrowed method, and the book borrowing operation is completed.
public class BorrowOperatio 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(name.equals(book.getName())){
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                break;
            }
        }

    }
}

4.4 ReturnOperation

  1.  Realize the function of returning books
  2. The implementation method is the same as that of lending a book, so I won't repeat it here.
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(name.equals(book.getName())){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                break;
            }
        }
    }
}

4.5 DelOperation

    1. The method is to find the position i of the book first, assign the value of i to j, and put the book at position j+1 into position j each time (getbook means to put the book at position j+1 into the book, Then use setBook to put the book at position j in the array), and loop currentSize-1 times, because there is j+1 to fear that the array will go out of bounds

    2. After deleting a book, the currentSize of the current book should be -- and the new book should be passed to setUsedSzie.

    3. When the break is not executed, i will be added to the position of currentSize in the end. At this time, it can be judged that the book has not been found.

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();
        int i = 0;
        for ( i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                for (int j = i; j <currentSize-1 ; j++) {
                    book = bookList.getBook(j+1);
                    bookList.setBook(j,book);
                }
                currentSize--;
                bookList.setUsedSize(currentSize);
                System.out.println("删除成功");
                break;
            }
        }
        if(i == currentSize){
            System.out.println("没有此书");
            return;
        }
    }
}

 4.6 DisplayOperation

  1. Realize the function of displaying books;
  2. In fact, it is to traverse the bookshelf, get the books through the getBook() method and print them out.
public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

4.7 ExitOperation

  1.  Equivalent to return 0 in C, indicating that the program exits normally;
public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
        System.exit(0);
    }
}

4.8 FindOperation 

  1. Realize the function of finding a book;
  2. Traverse the entire array and match by name;
  3. The currentSize variable obtains the current number of books. Through the getUsedSize method, it is directly obtained because it is privately modified, so it cannot be obtained, so the provided get method can only be used, which will reduce the number of cycles. 

  4. equals calls the equals of the String method here, no need to rewrite.

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();
        int i =0;
        for (i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("找到了这本书!");
                System.out.println(book);
                //break;
            }
        }
        if(i == currentSize){
            System.out.println("没有你要找的这本书!");
        }

    }

}

5. user package

5.1 User

1. User is the parent class, which stores the commonality of subclasses (ordinary users and administrator users), including an abstract class method menu, which is convenient for calling user in the main function, and the method of menu can be called, so in User Declare this menu method in .

2. Define an interface type array iOperations, the purpose is to allow subclasses to have their own methods for operating bookshelves.

3. this.iOperations is the interface array of administrators or ordinary users (this is selected from the login method in the main method), and choice is what we choose and return in the menu method, corresponding to an operation in the array, and the call is The work method of this class (combined with the Main class to analyze)

4. If the this.iOperations[2].work(bookList) of a common user is called, then the Borrow class is accessed. Then visit the work method in this Borrow class. (Combined with the NormalUser class and the BorrowOperation class to analyze)

public abstract class User {
    protected String name;
    public abstract int menu();
    protected IOperation[] iOperations;

    public User(String name) {
        this.name = name;
    }
    public void doOperation(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}

5.2 NormalUser

1. Realize the relevant operations of ordinary users on the bookshelf;

2. Create an object for the interface array defined in the parent class, and store the related bookshelf operations of ordinary users in the array;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation(),
                new DisplayOperation(),
        };
    }

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

5.3  AdminUser

1. Realize the relevant operations of the administrator user on the bookshelf;

public class AdminUser extends User{
    //构造方法,并且要先初始化父类的构造方法
    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new DisplayOperation(),

        };
    }

    //重写父类的抽象方法menu
    @Override
    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;
    }
}

6. Main class

1. The return value of login is given to the user. At this time, the plastic improvement occurs. At this time, the ordinary user or the administrator user has been created, and the initialization (including the iOperation array) and the menu menu have been established. Call When user.menu, dynamic binding occurs, who is the user now, that is whose menu. 

2. The user.menu function returns the number of the menu you selected, which is given to the doOperation method. 

public class Main {

    //返回是管理员用户还是普通用户
    //用static修饰的,否则main里面不能直接调用
    public static User login(){
        //先输入管理员和普通用户的姓名
        System.out.println("请输入你的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();


        System.out.println("请输入你的身份:1:-》管理员,0-》普通用户");
        Scanner scanner1 = new Scanner(System.in);
        int choice = scanner1.nextInt();
        if(choice == 1){
            //name在这里用到
            return new AdminUser(name);
        }
        else{
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        //Main main = new Main();
        User user = login();
        while(true){
            int choice = user.menu();//发生向上整形。
            user.doOperation(choice ,bookList);
        }

    }

Guess you like

Origin blog.csdn.net/weixin_44580000/article/details/124986220