Library Management System-Java

Table of contents

1. The style of library management system

Second, the specific implementation of the library management system

   2.1 book bag

  2.2 user package

  2.3 Main class

  2.4 operation package


1. The style of library management system

Management system renderings:

         From the effect diagram, we can see that the first thing to do is to log in and get the user's name and identity. Depending on the user's identity, the menu bar and the operations that the user can perform are different .

 Second, the specific implementation of the library management system

        Mainly set the following packages and classes:

                                        

        Among them, the book package stores information about books, the operation package stores various operations performed by users, user stores information about users, and Main is a test class.

   2.1 book bag

                        

        The Book class is used to store detailed information and operation methods about a book, and the BookList is a bookshelf class that records the stored books.

public class Book {
    private String name;//Book title
    private String author;//Author
    private int price;//price
    private String type;//type
    private boolean isborrowed;//whether to borrow
        
    // book initialization
    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }
    //Print the information of the book
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +"Book status: "+
                ((isborrowed==true)? "has been borrowed":"not borrowed")+
                '}';
    }
    // get book title
    public String getName() {
        return name;
    }
    //Set book title
    public void setName(String name) {
        this.name = name;
    }
    // Get the author
    public String getAuthor() {
        return author;
    }
    // set the author
    public void setAuthor(String author) {
        this.author = author;
    }
    // get the price
    public int getPrice() {
        return price;
    }
    // set the price
    public void setPrice(int price) {
        this.price = price;
    }
    //get type
    public String getType() {
        return type;
    }
    //set type
    public void setType(String type) {
        this.type = type;
    }
    //The status of the book, whether it is lent
    public boolean isIsborrowed() {
        return isborrowed;
    }
    //Set the state of the book
    public void setIsborrowed(boolean isborrowed) {
        this.isborrowed = isborrowed;
    }
}
public class BookList {
    //Books stored in the bookshelf (many books)
    private Book[] books;
    //The current number of books on the shelf
    int NowSize;
    //initialize bookshelf
    public BookList() {
         this.books=new Book[20];
         // Initially store three books
         books[0]=new Book("Romance of the Three Kingdoms", "Luo Guanzhong", 13, "Novel");
         books[1]=new Book("Journey to the West", "Wu Chengen", 15, "Novel");
         books[2]=new Book("A Dream of Red Mansions", "Cao Xueqin", 12, "Novel");
         NowSize=3;
    }
    //Know the position of the book (array subscript) to get a book from the bookshelf
    public Book getBook(int pos){
        return books[pos];
    }
    //Place a book in the corresponding position of the bookshelf
    public void SetBook(int pos,Book book){
        books[pos]=book;
    }
    //Get the number of books on the current shelf
    public int getNowSize() {
        return NowSize;
    }
    //Set the number of books on the current bookshelf
    public void setNowSize(int nowSize) {
        NowSize = nowSize;
    }
}

    2.2 user package

                        

         The User class is the parent class of all types of users, AdminUser is an administrator type user, and NormalUser is a normal user type user.

//Abstract class
public abstract class User {
    //user name
    protected String name;
    //Multiple operations of the user
    protected Ioperation[] ioperations;


    //user initialization
    public User(String name) {
        this.name = name;
    }
    // user's menu
    public  abstract int menu();
    //Action to be performed by the user
    public void doperation(int choice, BookList bookList){
        ioperations[choice].work(bookList);
    }
}
//admin user
public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        // specific operation of the user
        this.ioperations=new Ioperation[]{
                new ExitOperation(), //exit the system
                new SearchOperation(), //Find books
                new AddOperation(), //Add books
                new DeleteOperation(), //Delete books
                new ShowOperation(), //View all books
        };
    }
    //admin user menu
    @Override
    public int menu() {
        System.out.println("********************");
        System.out.println("hello "+name+" "+"Welcome to the administrator menu");
        System.out.println("1. Find books");
        System.out.println("2. Add books");
        System.out.println("3. Delete book");
        System.out.println("4. Display books");
        System.out.println("0. Exit the system");
        System.out.println("********************");
        System.out.println("Please enter your choice:");
        Scanner scanner=new Scanner(System.in);
        //user input options
        int choice=scanner.nextInt();
        return choice;
    }
}
//general user
public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        //Ordinary user specific operation
        this.ioperations=new Ioperation[]{
                new ExitOperation(), //exit the system
                new SearchOperation(), //Find books
                new BorrowOperation(), //borrow books
                new ReturnOperation(), //Return books
        };
    }
    //Ordinary user menu
    @Override
    public int menu() {
        System.out.println("********************");
        System.out.println("hello "+name+" Welcome to the normal user menu");
        System.out.println("1. Find books");
        System.out.println("2. Borrowing books");
        System.out.println("3. Return books");
        System.out.println("0. Exit the system");
        System.out.println("********************");
        System.out.println("Please enter your choice:");
        Scanner scanner=new Scanner(System.in);
        //user action options
        int choice=scanner.nextInt();
        return choice;
    }
}

 2.3 Main class

//Test class
public class Main {
    //User login
    public static User logn(){
        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: Administrator 2: Ordinary user");
        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=logn();
        while (true){
            int choice=user.menu();
            System.out.println("choice :"+choice);
            / / According to the choice to determine which object to perform a specific operation
            user.doperation(choice,bookList);
        }
    }
}

 2.4 operation package

                

         AddOperation adds book operation, BorrowOperation lends book operation, DeleteOperation deletes book operation, ExitOperation exits system operation, ReturnOperation returns operation, SearchOperation finds operation, ShowOperation checks all book operations, and all operations are performed on the bookshelf, so build an interface of Ioperation and declare the operation Bookshelf method, each class implements this interface.

//interface
public interface Ioperation {
    void work(BookList bookList);
}
// add books
public class AddOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Add Book");
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please enter name:");
        String name=scanner.nextLine();
        System.out.println("Please enter author:");
        String author=scanner.nextLine();
        System.out.println("Please enter price: ");
        int price=scanner.nextInt();
        scanner.nextLine();
        System.out.println("Please enter type:");
        String type=scanner.nextLine();
        Book book=new Book(name,author,price,type);
        //Check if the bookshelf is full
        if(bookList.getNowSize()==Max_book_count){
            System.out.println("The bookshelf is full");
            return;
        }else {
            for (int i = 0; i <bookList.getNowSize(); i++) {
                //Determine if the bookshelf already has this book
                if((bookList.getBook(i).getName()).equals(name)){
                    System.out.println("This book is already in the bookshelf, no need to add it");
                    return;
                }
            }
            //place the book
            bookList.SetBook(bookList.getNowSize(), book);
            //Increase the number of books by 1
            int count= bookList.getNowSize();
            bookList.setNowSize(++count);
            System.out.println("Add book successfully");
        }
    }
}
// check out books
public class BorrowOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Check out books");
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please enter name:");
        String name=scanner.nextLine();
        for (int i = 0; i < bookList.getNowSize(); i++) {
            //Determine the existence of this book
            if ((bookList.getBook(i).getName()).equals(name)){
                bookList.getBook(i).setIsborrowed(true);
                System.out.println("Borrowing books successfully");
                return;
            }
        }
        System.out.println("The borrowed book does not exist");
    }
}
//delete books
public class DeleteOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Delete book");
        System.out.println("Please enter the title of the book to be deleted:");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        for (int i = 0; i < bookList.getNowSize(); i++) {
            // exists this book
            if ((bookList.getBook(i).getName()).equals(name)){
                for (int j = i; j < bookList.getNowSize()-1; j++) {
                    bookList.SetBook(j,bookList.getBook(j+1));
                }
                //The last book is empty
                bookList.SetBook(bookList.getNowSize(),null);
                //The number of books minus 1
                int count=bookList.getNowSize();
                bookList.setNowSize(--count);
                System.out.println("Delete books successfully");
                return;
            }
        }
        System.out.println("This book does not exist");
    }
}
// return the book
public class ReturnOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Return books");
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please enter name:");
        String name=scanner.nextLine();
        for (int i = 0; i < bookList.getNowSize(); i++) {
            // exists book
            if ((bookList.getBook(i).getName()).equals(name)){
                bookList.getBook(i).setIsborrowed(false);
                System.out.println("Return the book successfully");
                return;
            }
        }
        System.out.println("The returned book does not exist");
    }
}
//find books
public class SearchOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Find books");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.nextLine();
        for (int i = 0; i <bookList.getNowSize();i++) {
            //book exists
            if ((bookList.getBook(i).getName()).equals(name)){
                System.out.println(bookList.getBook(i));
                return;
            }
        }
        System.out.println("There is no such book in the library");
    }
}
//find all books
public class ShowOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("print book");
        for (int i = 0; i < bookList.getNowSize(); i++) {
            Book book=bookList.getBook(i);
            System.out.println(book);
        }
    }
}
//Exit system
public class ExitOperation implements Ioperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("Exit the system");
        System.exit(0);
    }
}

Guess you like

Origin blog.csdn.net/qq_64668629/article/details/132257099