library management system


basic introduction

Book management system: As the name suggests, it is a simple system that can store, add, delete, modify and other operations on book information. After we have learned the basics of Java, we can implement the code.

1. Basic idea

First of all, let's sort out logically and think about its function.

Logically: It is not difficult to find that it is very unrealistic to put all the functions needed in one class and one file, and it is very unfavorable for our code writing and thinking. Therefore, it is particularly important to encapsulate and list different operations separately in corresponding files.

Functionally: We can give different functions to different users. It can be divided into two categories: administrators and ordinary users.

Administrator: There are five functions of adding, deleting, searching, displaying and exiting.
User: There are four functions: search, borrow, return, and exit.

At this point, the basic ideas have been sorted out, and then I will use the code to implement one by one

Second, the classification of object-oriented packages

We can divide surface objects into three categories, namely: users, operations, and books.

1.books package


Book: This class is mainly used to abstract the information of the object and generate the corresponding constructor. code show as below:

package books;

public class Book {
    //abstract book information
    //This is set to private to avoid unnecessary tampering
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrow;   //Whether to be lent, defaults to false when uninitialized

    //Generate the corresponding constructor
    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }
	//Here use get, set to access and modify data
	//Although not necessarily all can be used, but it is generated for easy reference
    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 isBorrow() {
        return isBorrow;
    }

    public void setBorrow(boolean borrow) {
        isBorrow = borrow;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ((isBorrow == true)? "loaned":"not lent")+
                '}';
    }
}

BookList class: The role of this class here is equivalent to the role of the bookshelf, which is used to store the corresponding information and modify the information. code show as below

package books;

//bookshelf
public class BookList {
    //Use an array to store book information
    public Book[] Books = new Book[10];
    private int usedSize;    //Used to record how many books are currently stored

    //Create a construction method to store book information
    public BookList(){
        //Put away 3 books first
        Books[0] = new Book("Three Kingdoms","Luo Guanzhong",89,"novel");
        Books[1] = new Book("Journey to the West","Wu Chengen",78,"novel");
        Books[2] = new Book("A Dream of Red Mansions","Cao Xueqin",49,"novel");
        this.usedSize = 3;
    }
	//Get the number of books here
    public int getUsedSize() {
        return usedSize;
    }
	//Modify the number of books information here
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
	//Get book information
    public Book getpos(int pos){
        return Books[pos];
    }

    //Store a book in the corresponding location
    public void setBooks(Book book,int pos){
        Books[pos] = book;
    }
}

2.operation package


1. Implementation of the IOPeration interface

package operations;

import books.BookList;

//Define an interface to store methods that are commonly used
//Use this interface to connect the bookshelf with the following methods
public interface IOPeration {
    void work (BookList bookList);
}

2. Implementation of the Add class

package operations;

import  java.util.Scanner;

import books.Book;
import books.BookList;

public class AddOperation implements IOPeration{

    //Add books
    public void work (BookList bookList){
        System.out.println("Add books");
        System.out.println("Please enter the name of the new book:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        System.out.println("Please enter the author of the new book:");
        String author = scanner.nextLine();

        System.out.println("Please enter the type of new book:");
        String type = scanner.nextLine();

    //Note: If the number is placed before the receiving character, it will cause an error in the character acceptance of the carriage return when entering the number.
        System.out.println("Please enter the price for the new book:");
        int price = scanner.nextInt();

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

        //1. Get the current location where the book can be stored
        int currentSize = bookList.getUsedSize();
        //2. Put the book in the designated position
        bookList.setBooks(book,currentSize);
        //3. Add 1 to the valid number of books
        bookList.setUsedSize(currentSize+1);
    }
}

3. Implementation of the Borrow class

package operations;

import books.Book;
import books.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOPeration{
    public void work (BookList bookList){
        System.out.println("borrow books");
        System.out.println("Please enter the title of the book you want to borrow");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //Traverse the array, check if there is a book you are looking for, record the subscript
        int index = -1;
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getpos(i);
            if(name.equals(book.getName())){
                book.setBorrow(true);
                return;
            }
        }
        System.out.println("No books to borrow");
    }
}

4.Delete class

package operations;

import java.util.Scanner;

import books.Book;
import books.BookList;

public class DelOperation implements IOPeration{

    public void work (BookList bookList){
        //delete book
        System.out.println("delete book");
        System.out.println("Please enter the book you want to delete");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        //Traverse the array, check if there is data you want to delete, record the subscript
        int index = -1;
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getpos(i);
            if(name.equals(book.getName())){
                index = i;
                break;
            }
        }
        //At this point, the data stored in the index is the data to be deleted.
        if(index == -1){
            System.out.println("no books to delete");
        }
        //Delete is to overwrite the previous element by the next element to be deleted
        for (int i = index; i < currentSize-1; i++) {
            Book book = bookList.getpos(i+1);
            bookList.setBooks(book,i);
        }

        //Empty every time you delete
        bookList.setBooks(null,currentSize-1);

        bookList.setUsedSize(currentSize-1);

        System.out.println("successfully deleted");
    }
}

5.Display class

package operations;

import books.Book;
import books.BookList;

public class DisplayOperation implements IOPeration{
    public void work (BookList bookList){
        System.out.println("show all books");

        //Display the number of books
        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getpos(i);
            System.out.println(book);
        }
    }
}

6.Exit class

package operations;

import books.BookList;

public class ExitOperation implements IOPeration{
    public void work (BookList bookList){
        System.out.println("Exit the book system");
        System.exit(0);
    }
}

7.Find class

package operations;

import java.util.Scanner;

import books.Book;
import books.BookList;

public class FindOperation implements IOPeration{

    //Find books
    public void work (BookList bookList){
        System.out.println("Find books");
        System.out.println("Please enter the title of the book you are looking for");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getpos(i);
            if(name.equals(book.getName())){
                System.out.println("Found this book!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("could not find it. . .");
    }
}

8.return class:

package operations;

import books.Book;
import books.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOPeration{
    public void work (BookList bookList){
        System.out.println("return books");

            System.out.println("Please enter the title of the book you want to return");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();

            //Traverse the array, check if there is a book you are looking for, record the subscript
            int index = -1;
            int currentSize = bookList.getUsedSize();
            for (int i = 0; i < currentSize; i++) {
                Book book = bookList.getpos(i);
                if(name.equals(book.getName())){
                    book.setBorrow(false);
                    return;
                }
            }
            System.out.println("No books to return");
    }
}

2.User package

User: As the parent class of the above two, it provides a constructor.

package user;

import books.BookList;
import operations.IOPeration;

//Object Oriented 1. User
public abstract class User {
    //protected can be accessed by the same class in the same package, different classes in the same package, and subclasses (inheritance) in different packages
    protected String name;
    protected IOPeration[] ioPerations;    // Just define that the array is not initialized, and memory is not allocated, allocated in subclasses as needed

    //provide a constructor
    public User (String name){
        this.name = name;
    }

    public abstract int menu();

    public void doOperation(int choice, BookList bookList){
        ioPerations[choice].work(bookList);
    }
}

AdminUser: Class for admin operations.

package user;

import operations.*;

import java.util.Scanner;

//administrator
//Inherit common ground in User
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()
        };
    }

    public int menu(){
        System.out.println("-------------------------------");
        System.out.println("hello "+name+"Welcome to the system!");
        System.out.println("1. Find books");
        System.out.println("2. Add Books");
        System.out.println("3. delete book");
        System.out.println("4. show books");
        System.out.println("0. Exit system");
        System.out.println("-------------------------------");
        System.out.println("Please enter your action:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

NormalUser: A class for normal user operations.

//Inherit common ground in User
public class NormalUser extends User{

    //After inheritance, the subclass helps the parent class to construct
    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("hello " + name + "Welcome to the system!");
        System.out.println("1. Find books");
        System.out.println("2. borrow books");
        System.out.println("3. return books");
        System.out.println("0. Exit system");
        System.out.println("-------------------------------");
        System.out.println("Please enter your action:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

Third, use the main function to integrate the operation

import books.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {
    //find object-oriented steps
    //find a partner
    //create object
    //user target audience

    //Returns the corresponding user type through User, using up-transformation
    public static User login(){
        System.out.println("Please type in your name");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();

        System.out.println("Please enter your identity: 1->admin, 0->general user");
        int choice = scanner.nextInt();
        //The reference object here is different, the printed menu is different
        if(choice == 1){
            return new AdminUser(userName);
        }else{
            return new NormalUser(userName);
        }
    }


    public static void main(String[] args) {
        //Prepare data
        BookList bookList = new BookList();

        //Log in
        User user = login();
        while(true){
            int choice = user.menu();

            //Here you can consider putting the operations of different users into the interface
            user.doOperation(choice,bookList);
        }
    }
}

4. Summary

The library management system is a comprehensive application of the previous knowledge points, mainly classes and objects, inheritance polymorphism and other knowledge points. This article also ends here, if you have any questions, please correct me, thank you!

Tags: Java jvm servlet

Posted by ranbla on Sat, 03 Sep 2022 08:38:50 +0930