Realization of Library Management System by Java Sequence Table

  • 2021-12-13 08:04:26
  • OfStack

This article example for everyone to share the Java sequence table to achieve the specific code of the library management system, for your reference, the specific content is as follows

1. Introduction

The purpose of this project is to consolidate and understand the previous knowledge points: classes, abstract classes, encapsulation, inheritance, polymorphism, interfaces and so on

2. Core requirements

Management side

Consult books
Add books
Delete a book
Print a list of books
Exit the system

Client side

Inquire about books
Borrowing books
Return a book
Print a list of books
Exit the system

3. Design of classes

1. Create a book class

The book class contains information about the book's name, price, type, author, and whether or not it was loaned, and generates constructors, Getter () and Setter () methods, and toString methods. (Note that member variables should be decorated with the private keyword whenever possible.)


public class Book {
    private String name;
    private double price;
    private String type;
    private String author;
    private boolean isBorrowed;

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

    public void setBorrowed(boolean borrowed) {
        isBorrowed = borrowed;
    }

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

2. Create a book list class

The book list class is used to store books. We can initialize several books in the list to facilitate subsequent testing


public class BookList {
    private Book[] books = new Book[10];

    private int usedSize;
    public BookList(){
        books[0] = new Book("3 Romance of the Kingdom ",19," Novel "," Luo Guanzhong ");
        books[1] = new Book(" Outlaws of the Marsh ",29," Novel "," Shi Naian ");
        books[2] = new Book(" Journey to the West ",39," Novel "," Wu Chengen ");
        usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public Book getBook(int pos){
        return books[pos];
    }

    public void setBook(int pos,Book book) {
        books[pos] = book;
    }
}

3. Create a user class

Create a user class and define it as an abstract class, and then create a common user class and an administrator class to inherit from the user class:

Create a user class and define it as an abstract class:


public abstract class User {
    protected String name;
    protected IOperation[] iOperations;
    public abstract int menu();
    public void doWork(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
    public User(String name) {
        this.name = name;
    }
}

Create an administrator user class:


public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
            new ExitOperation(),
            new FindOperation(),
            new AddOperation(),
            new DisplayOperation(),
            new DelOperation()
        };
    }

    @Override
    public int menu(){
        System.out.println("=========== Administrator menu ============");
        System.out.println(" How do you do ,  Administrator  "+this.name+":");
        System.out.println(" Welcome to the library !");
        System.out.println("1.  Find Books ");
        System.out.println("2.  New books ");
        System.out.println("3.  Display books ");
        System.out.println("4.  Delete a book ");
        System.out.println("0.  Exit the system ");
        System.out.println("=================================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

To create a generic user class:


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

    @Override
    public int menu(){
        System.out.println("=========== General user menu ============");
        System.out.println(" How do you do, user  "+this.name+":");
        System.out.println(" Welcome to the library !");
        System.out.println("1.  Display books ");
        System.out.println("2.  Find Books ");
        System.out.println("3.  Borrowing books ");
        System.out.println("4.  Return a book ");
        System.out.println("0.  Exit the system ");
        System.out.println("=================================");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

4. Create action-related classes

First, create an interface to implement polymorphism:


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

Create the Add Book class:


public class AddOperation implements IOperation{
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Please enter a book name :");
        String name = scanner.nextLine();
        System.out.println(" Please enter the price :");
        double price = scanner.nextDouble();
        System.out.println(" Please enter a type :");
        String type = scanner.next();
        System.out.println(" Please enter an author :");
        String author = scanner.next();
        Book book = new Book(name,price,type,author);
        int usedSize = bookList.getUsedSize();
        bookList.setBook(usedSize,book);
        bookList.setUsedSize(++usedSize);
        System.out.println(" Book Added Successfully !");
    }
}

Create the Find Books class:


public class FindOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println(" Please enter the title of the book :");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        for(int i=0;i<bookList.getUsedSize();i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())){
                System.out.println(book);
                return;
            }
        }
        System.out.println(" Can't find   " "+name+" "   This book ");
    }
}

Create a borrowed book class:


public class BorrowOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println(" Please enter the books you want to borrow :");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int i = 0;
        for (i = 0; i < bookList.getUsedSize() - 1; i++) {
            Book book = bookList.getBook(i);
            if (name.equals(book.getName()) && !book.isBorrowed()) {
                book.setBorrowed(true);
                System.out.println(" Successful borrowing !");
                return;
            }
            if (name.equals(book.getName()) && book.isBorrowed()) {
                System.out.println(" The book has been loaned out ");
                return;
            }
        }
        System.out.println(" Can't find the books you want to borrow !");
    }
}

Create a return book class:


public class ReturnOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println(" Please enter the book you want to return :");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.next();
        int i=0;
        for(i=0;i<bookList.getUsedSize()-1;i++){
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())&& book.isBorrowed()){
                book.setBorrowed(false);
                System.out.println(" Successful return !");
                return;
            }
            if(name.equals(book.getName())&& !book.isBorrowed()){
                System.out.println(" This book is in an unloaned state !");
                return;
            }
        }
        System.out.println(" Can't find the book you want to return !");
    }
}

Create and delete book classes:


public class BookList {
    private Book[] books = new Book[10];

    private int usedSize;
    public BookList(){
        books[0] = new Book("3 Romance of the Kingdom ",19," Novel "," Luo Guanzhong ");
        books[1] = new Book(" Outlaws of the Marsh ",29," Novel "," Shi Naian ");
        books[2] = new Book(" Journey to the West ",39," Novel "," Wu Chengen ");
        usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public Book getBook(int pos){
        return books[pos];
    }

    public void setBook(int pos,Book book) {
        books[pos] = book;
    }
}
0

Create a printed book list class:


public class BookList {
    private Book[] books = new Book[10];

    private int usedSize;
    public BookList(){
        books[0] = new Book("3 Romance of the Kingdom ",19," Novel "," Luo Guanzhong ");
        books[1] = new Book(" Outlaws of the Marsh ",29," Novel "," Shi Naian ");
        books[2] = new Book(" Journey to the West ",39," Novel "," Wu Chengen ");
        usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public Book getBook(int pos){
        return books[pos];
    }

    public void setBook(int pos,Book book) {
        books[pos] = book;
    }
}
1

Exit system class:


public class BookList {
    private Book[] books = new Book[10];

    private int usedSize;
    public BookList(){
        books[0] = new Book("3 Romance of the Kingdom ",19," Novel "," Luo Guanzhong ");
        books[1] = new Book(" Outlaws of the Marsh ",29," Novel "," Shi Naian ");
        books[2] = new Book(" Journey to the West ",39," Novel "," Wu Chengen ");
        usedSize = 3;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

    public Book getBook(int pos){
        return books[pos];
    }

    public void setBook(int pos,Book book) {
        books[pos] = book;
    }
}
2

Main function class:


public class Main {
    public static User work(){
        System.out.println(" Please enter your name :");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println(" Please enter identity : 1->  Administrator login   0->  User login ");
        int choice = scanner.nextInt();
        if(choice==1){
            return new AdminUser(name);
        }
            return new NormalUser(name);
    }

    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = work();
        while (true) {
        int choice = user.menu();
            user.doWork(choice, bookList);
        }
    }
}

The end


Related articles: