Java library management system

Java library management system

⚡ Preface ⚡ ️

After reading this column, you can summarize the knowledge you have learned from the main application of Java, and hope to deepen your knowledge after reading this column.

🍉 Blog home page: 🍁 [warm the sun like the wind]🍁
🍉 Boutique Java column [Javase],[Java data structure]
🍉 Welcome to like 👍 Collection ⭐ Message comments 📝 Private letters must be answered 😁

🍉 This article was originally written by [Rufeng Wenyang], and was first launched in CSDN 🙉

🍉 Bloggers will continue to update their learning records. If you have any questions, you can leave a message in the comment area

🍉 The source code involved in the blog and the blogger's daily practice code have been uploaded Code cloud (gitee)

🍉 1. System guide

The most important feature of Java is object-oriented. This little exercise is to strengthen our understanding of object-oriented, so the implementation of the system must realize the system functions by dividing into multiple packages and classes.

The of the system is realized by four parts:
1. Under the book package is the storage of book information
2.user information and operation logic under user package
3. The main file is the logical realization of the whole system
4. The specific realization of system functions is under the operation package

The first is to log in to the page, enter the name and select the identity, then jump to different menus (two menus for administrators and ordinary users), and then realize the corresponding functions after the user further selects the functions.

🍉 2. Create classes required by the system

2.1 book information (book package)

The system is realized through the sequence table. The information of each book is stored in the array of the sequence table, and then the function of addition, deletion, query and modification is realized through the sequence table (the sequence table can understand the previous articles of bloggers) [sequence table of Java data structure])

Book.java

The book needs to contain information such as the title, author, price, type and whether it is lent out

package book;
public 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() {//The following getter s and setter s are implemented through the shortcut key alt+insert in idea
        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
    //The toString() method is rewritten to print the specific information of the book. If it is not rewritten, the specific content of the book will not be printed
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                 ((isBorrowed==true)?"Has been lent out":"Not lent") +
                '}';
    }
}

BookList.java

package book;
public class BookList {
    private Book [] books=new Book[10];
    //For the sequence table array, set the length to be small for the time being. This part will continue to be improved with the following learning
    private int useSize;
    //Record the current number of books
    public BookList() {
    //In the construction method, the information of three books is stored first
        books[0] = new Book("Romance of the Three Kingdoms","Luo Guanzhong",17,"novel");
        books[1] = new Book("Journey to the West","Wu Chengen",47,"novel");
        books[2] = new Book("Water Margin","Shi Naian",37,"novel");
        this.useSize=3;
    }
    public int getUseSize() {
        return useSize;
    }
    public void setUseSize(int useSize) {
        this.useSize = useSize;
    }
    //The following two methods are hand tapping (not shortcut keys)
    public Book getPos(int pos) {
        return this.books[pos];
    }//Get the book with the corner marked pos position
    public void setBook(int pos,Book book) {
        this.books[pos] = book;
    }//Change the book whose corner mark is pos position to the book passed by function parameters
}

2.2 user information (user package)

Because the User has two objects: manager and ordinary User, but most of them have the same attributes and have detailed different methods, extract the same code to create an abstract class User, and then create AdminUser and NormalUser to inherit User respectively.

User.java

package user;

import book.BookList;
import operation.IOperation;

public abstract class User {
    String name;//User name
    IOperation[] iOperations;
    //Create a user operation array, which is used to store multiple functions
    public User(String name) {
        this.name = name;
    }
    public abstract int menu();
    //The menus of the two objects are different, so an abstract method is created
    public void doWork(int choice, BookList bookList) {
        iOperations[choice].work(bookList);
    }//This method is to perform different operations according to the selection of the corresponding operation array after the user makes a selection
}

AdminUser.java

package user;

import operation.*;

import java.util.Scanner;

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()
        };
        //Instantiate different operation menus corresponding to different objects in the construction method
    }
    //Print different menus
    public int menu() {
        System.out.println("===========Administrator menu===========");
        System.out.println("hello " + this.name +" Welcome here!");
        System.out.println("1.Find books");
        System.out.println("2.New books");
        System.out.println("3.Delete book");
        System.out.println("4.Show books");
        System.out.println("0.Exit the system");
        System.out.println("==============================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
        //The user makes a selection, returns the choice value after selection, and calls to realize the corresponding function
    }
}

package user;

import operation.*;

import java.util.Scanner;

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

    public int menu() {
        System.out.println("===========Menus for ordinary users===========");
        System.out.println("hello " + this.name +" Welcome here!");
        System.out.println("1.Find books");
        System.out.println("2.Borrow books");
        System.out.println("3.Return books");
        System.out.println("0.Exit the system");
        System.out.println("==============================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

🍉 3. System logic (Main.java)

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

import java.util.Scanner;

public class Main {
//No matter who the object is, you need to show the login interface first
    public static User login() {
        System.out.println("Please enter your name:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("Please enter your identity: 1->administrators,0->Ordinary users");
        int choice = scanner.nextInt();
        if(choice == 1) {
        //Instantiate different objects and return them according to different values of choice
            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();//Dynamic binding - polymorphism occurred
            //Call the appropriate operation according to your choice
            user.doWork(choice, bookList);
        }
    }
}

🍉 4. Function realization (operation package)

Because the system has multiple functions, under this package, each function is independently created as a class to be specifically and clearly implemented. For the reuse of code in these classes, an interface (functions shared by multiple classes) is abstracted

Ioperation.java

package operation;
import book.BookList;
import java.util.Scanner;
public interface IOperation {
    Scanner scanner = new Scanner(System.in);
    void work(BookList bookList);
}

AddOperation.java

To add a new book, you need a new book and initialize the information, and then put it at the last position of the sequence table

package operation;
import book.Book;
import book.BookList;
public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("New books!");
        System.out.println("Please enter the title of the book:");
        String name = scanner.next();
        System.out.println("Please enter the author:");
        String author= scanner.next();
        System.out.println("Please enter the type:");
        String type=scanner.next();
        System.out.println("Please enter price:");
        int price=scanner.nextInt();
        Book book=new Book(name, author,price,type);
        //Instantiate new books
        int size= bookList.getUseSize();
        //After calling the package, call bookList.. getUseSize(); Function to determine the storage location of new books
        bookList.setBook(size,book);
        //New books
        bookList.setUseSize(size+1);
        //Valid bibliography plus one
        System.out.println("Successfully added books!");
    }

}

BorrowOperation.java

Enter the name of the borrowed book and traverse the array to find it. After finding it, change the borrowing situation of the book

package operation;

import book.Book;
import book.BookList;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Borrow books!");
        System.out.println("Please enter the name of the book you want to borrow");
        String name=scanner.nextLine();
        int size=bookList.getUseSize();
        for (int i = 0; i < size; i++) {
            Book book=bookList.getPos(i);
            if (name.equals(book.getName())) {
                book.setBorrowed(true);
                System.out.println("Borrowing succeeded");
                System.out.println(book);
                return;
            }
        }
        System.out.println("There are no books you want to borrow");
    }
}

DelOperation.java

package operation;

import book.Book;
import book.BookList;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Delete books!");
        System.out.println("Please enter the book to delete");
        String name=scanner.next();
        int size= bookList.getUseSize();
        //Set the cycle to search the bibliography first
        for (int i = 0; i < size; i++) {
            Book book=bookList.getPos(i);
            //When found, move the array forward to overwrite the book
            if (name.equals(book.getName())) {
                for (int j = i; j < size-1; j++) {
                    Book bookNext=bookList.getPos(j+1);
                    bookList.setBook(j,bookNext);
                }
                bookList.setBook(size,null);
                bookList.setUseSize(size-1);
                //Effective bibliography after deleting bibliography-1;
                System.out.println("Delete complete");
                return;
            }
        }
        System.out.println("The book does not exist");
    }
}

DisplayOperation.java

Print all the existing bibliographic information

package operation;

import book.Book;
import book.BookList;

import java.util.Arrays;

public class DisplayOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Browse books!");
        int size= bookList.getUseSize();
        for (int i = 0; i < size; i++) {
            Book book=bookList.getPos(i);
            System.out.println(book);
        }
    }
}

ExitOperation.java

Exit the system

package operation;

import book.BookList;

public class ExitOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Exit the system!");
        System.exit(0);
    }
}

FindOperation.java

package operation;

import book.Book;
import book.BookList;

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Find books!");
        System.out.println("Please enter the book you want to find:");
        String name=scanner.next();
        int size= bookList.getUseSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getPos(i);
            if (name.equals(book.getName())) {
                System.out.println("I found this book. The information of the book is as follows:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("Can't find");
    }
}

ReturnOperation.java

package operation;

import book.Book;
import book.BookList;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Return the books!");
        System.out.println("Please enter the name of the book to be returned");
        String name=scanner.nextLine();
        int size=bookList.getUseSize();
        for (int i = 0; i < size; i++) {
            Book book=bookList.getPos(i);
            if (name.equals(book.getName())) {
                book.setBorrowed(true);
                System.out.println("Return successful");
                System.out.println(book);
                return;
            }
        }
        System.out.println("There are no books you want to return");
    }
}

⚡ Last words ⚡ ️

It's not easy to summarize. I hope uu you won't be stingy with your work 👍 Yo (^ u ^) yo!! If you have any questions, please comment in the comment area 😁

Keywords: Java Back-end JavaSE

Added by 25lez25 on Sun, 06 Feb 2022 21:02:24 +0200