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

Posted by Klojum on Mon, 28 Feb 2022 20:52:45 +0100

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.

Blog link:

1. Business requirements

You can see that there are two types of users. Different users display different menus and can perform different operations, such as borrowing, returning, displaying, deleting, adding, etc.

 

 

2. Construction of overall framework

Here we use Idea to write all our code. Because our main goal is to practice polymorphism, interface and other ideas, the construction of the overall framework is particularly important.

First of all, we will create a new Mai class under the project to write the main method. We will create a new Book package. In the package, we will create two classes, a Book, for all the information of each Book, BookList and bookshelf. In the future, we will create an array of Book type to store all books. In defining a User package, we define three classes, because we have two kinds of users, ordinary users and administrators. We define two PublicUser and AderminUser classes as subclasses, User class as their parent class, and then define an Operation package. The classes defined in the package are methods of various operations, such as borrowing, returning, displaying, deleting, adding, etc. So far, all our classes have been created.

Among them, the Operation package contains all operations to be performed. We name the Operation methods of each class in the package work, and all operations are completed in bookList, so all classes have the conditions of interface. We can write an interface, and all classes in the package use this interface, for example:

public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Add books!");
    }
}


public class BorrowOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Borrow books!");
    }
}


public class DeleteOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("Delete books!");
    }
}


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

In this way, we can flexibly use interfaces and polymorphism.

3.Book package

The book bag contains information about each of our specific books and shelves.

3.1Book type

Each book has its name, author, price, type, and whether it is lent. First, we create these variables, and then write its construction method. Note that the state here is not lent. We directly define that it is not initialized, which is just false, which meets our requirements. Because encapsulation should be reflected here, all variables are decorated with private, After providing the corresponding get and set methods to facilitate printing in the future, we are generating a toString method here.

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;//Types of books
    private boolean isBorrowed;//The default is false

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }


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

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

3.2BookList class

The booklist class is used as a bookshelf. We first define an array of Book type to store books and the usedSize variable to describe the number of books

public Book[] bookList=new Book[20];
    private int usedSize;
Then provide him with construction methods. Here, we initialize four books as four famous works, and then provide corresponding get and set methods for his variables. Note that when providing get and set methods of BookList, because the BookList type is essentially an array of Book type, the return value here is Book type, and the parameters are array subscripts.
public class BookList {
    public Book[] bookList=new Book[20];
    private int usedSize;

    public BookList() {
        this.bookList[0] = new Book("Romance of the Three Kingdoms","Luo Guanzhong",18,"novel");
        this.bookList[1] = new Book("Journey to the West","Wu Chengen",38,"novel");
        this.bookList[2] = new Book("Water Margin","Shi Naian",28,"novel");
        this.bookList[3] = new Book("The Dream of Red Mansion","Cao Xueqin",48,"novel");
        this.usedSize = 4;
    }
//  Get books in pos location
    public Book getBookList(int pos) {
        return bookList[pos];
    }
    //  Modify books in pos location
    public void setBookList(Book book,int pos) {
        this.bookList[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }


}

I will continue to elaborate on other packages and classes in the follow-up blog.

Topics: Java Back-end IDEA