Library management system Part3 (practice of encapsulation, inheritance, polymorphism and interface)

Library management system is a common small project in learning programming. After learning classes and objects, encapsulation, inheritance, polymorphism and interfaces, we can use these ideas to realize our library management system. Here I will use two blogs to explain the specific implementation methods. According to the first two blogs, we have straightened out all the ideological logic. In this article, we only need to complete the preparation of each specific operation.

First two Blogs:

We put all operations in the Operation package, and all operations use the IOperation interface. Each Operation class has a work method to implement.

1.IOperation interface

This interface is very simple. Just write a work method with a parameter of BookList type according to their characteristics.

public interface IOperation {
    void work(BookList bookList);
}

2.FindOperation class

This class is the operation of finding books.

Here we find books according to the book name. It's very simple. First print and prompt him to enter the book name. We use name to accept his input. We can easily go through the array and find the information of the displayed book. If we can't find the display, we can't find it.

public class FindOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Find books!");
        System.out.println("Please enter the book you want to find:");
        Scanner scan=new Scanner(System.in);
        String name=scan.nextLine();
        int count= bookList.getUsedSize();
        for (int i = 0; i < count; i++) {
            Book book=bookList.getBookList(i);
            if(book.getName().equals(name)){
                System.out.println("Book information:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("Without this book!");
    }

Note: 1 Count is the usedSize defined in the BookList, that is, the number of current books on the bookshelf. We need to use the getUseSize method to get it.

           2.BookList is essentially an array of Book type. We need to use the getBookList method to obtain it. The parameter is the array subscript. After obtaining the Book with the current i subscript, we use the getName method to obtain the name and compare it with the user's input.

3.AddOperation class

This class is the operation of adding books.

To add a new book, you need to enter all the member variables of book type, that is, the attributes of the book. Enter the name, author, type and price of the book in turn, and then new a new book type. The parameters are our input. When we get the current number of books, the books you want to add are added after all the books, so here your number is just used as your array subscript. We use the setBookList method in BookList to add a Book object, and the parameter is the book object we just got.

public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Add books!");
        Scanner scn=new Scanner(System.in);
        System.out.println("Please enter the name of the new book:");
        String name= scn.nextLine();
        System.out.println("Please enter the author of the new book:");
        String author= scn.nextLine();
        System.out.println("Please enter the type of new book:");
        String type= scn.nextLine();
        System.out.println("Please enter the price of the new book:");
        int price= scn.nextInt();
        Book book=new Book(name,author,price,type);
        int currentSize= bookList.getUsedSize();
        bookList.setBookList(book,currentSize);
        bookList.setUsedSize(currentSize+1);//Successfully put in a Book
        System.out.println("Successfully added!");
    }
}

Note: 1 The attribute of the book also includes whether to lend it. Since it defaults to false, we don't need to operate it.

           2. After adding a new book, the number should also be added. Use the setUsedSize method.

4.DeleteOperation class

This class is for deleting books.

To delete a book, we also delete it according to its name. First, we need to get the name of the deleted book and use name to get it. Then we need to facilitate the array to see whether there is this book. First, we define an index and initialize it to - 1. If there is this book, we will give the index the index of the array subscript, that is, the value of i. after the cycle, if it is still - 1, it means there is no book. If it is not - 1, it means there is. We start deleting, Here, the operation of overwriting the previous bit with the last bit of the array is used to delete. To delete, the book with index subscript is to be deleted, so the new cycle starts from the index. Because deleting a bit overwrites the previous bit with the last bit, we need to stop at the penultimate book position. Therefore, the end condition should be, i < number of books - 1.

public class DeleteOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Delete books!");
        Scanner scn=new Scanner(System.in);
        System.out.println("Please enter the name of the book you want to delete:");
        String name= scn.nextLine();
        int size=bookList.getUsedSize();
        int index=-1;
        for (int i = 0; i < size; i++) {
            Book book=bookList.getBookList(i);
            if(book.getName().equals(name)){
                index=i;
                break;
            }
        }

        if(index==-1){
            System.out.println("There are no books you want to delete!");
        }//Delete here
        else {
            for (int i = index; i < size-1; i++) {
                //bookList[i]=bookList[i+1]
                Book book=bookList.getBookList(i+1);
                bookList.setBookList(book, i);
            }
            bookList.setUsedSize(size-1);
            System.out.println("Delete succeeded!");
        }
    }
}

Note: 1 The convenience array used here is the same as that in the book search class. You can't write an array directly, but you need to call the get and set methods in the BookList class. It will also be used below, which will not be explained below.

          2. After the second cycle, that is, the end of deletion, we need to put the total number of books - 1.

5.DisplayOperation class

This class is used to display book operations.

The display of books is very simple. As for the convenience as before, you can print them once.

public class DisplayOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Show books!");
        int size=bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book=bookList.getBookList(i);
            System.out.println(book);
        }
    }
}

Note: since we have overridden the toString method in the Book class, we only need sout h to print.

6.BorrowOperation class

This class is the operation of borrowing books.

Borrowing books is also based on the Book name. We also get the name according to the input. Then, if it is found, since the isBorrowed of Book is initialized to false by default, that is, it is not lent, we need to modify it to true.

public class BorrowOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Borrow books!");
        Scanner scn=new Scanner(System.in);
        System.out.println("Please enter the name of the book you want to borrow:");
        String name= scn.nextLine();
        int size=bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book=bookList.getBookList(i);
            if(book.getName().equals(name)){
               book.setBorrowed(true);
                System.out.println("Borrowing succeeded,The information of books borrowed is as follows:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("Borrowing failed,Without this book!");
    }
}

7.ReturnOperation class

This class is the operation of returning books.

Just like borrowing, according to the title of the book, we can make it convenient first. If there is, just modify IsBorrowed back to false with the setBorrowed method.

public class ReturnOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("Return the books!");
        Scanner scn=new Scanner(System.in);
        System.out.println("Please enter the name of the book to be returned:");
        String name= scn.nextLine();
        int size=bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book=bookList.getBookList(i);
            if(book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("Return successful,The information of returned books is as follows:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("Return failed,Without this book!");
    }
}

8.ExitOperation class

This class is called exit system operation.

Since it is an endless loop in the main function, we just need to write a system exit(0); Just exit the cycle.

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

9. Summary

So far, all the codes of the first two blogs have been written. Generally speaking, the code is not difficult, but it will be a little windy to combine polymorphism, interface and encapsulation. It is very suitable to practice object-oriented characteristics.

10. Operation results

Find books # add new books

     

New books

      

Delete book

 

Show books

It can be found that the dream of Red Mansions is missing and a new java book is added

Borrow books

Return books

Exit the system

 

 

Keywords: Java Back-end

Added by bad_goose on Tue, 01 Mar 2022 12:39:36 +0200