Library management system (detailed explanation with pictures and text, source code attached)

Foreword: This article aims to use object-oriented programming to implement a library management system. The functions include addition, deletion and search. The complete source code is placed at the end of the article. Everyone can pick it up if needed


Table of contents

1. Overall framework

2. Books and bookshelves

Books

Bookshelf (BookRack)

3. Related operations on books

Operation interface (IOoperation)

Add books (AddOperation)

Borrow books (BorrowOperation)

Delete book(DeleteOperation)

Find books (FindOperation)

Return books (ReturnOperation)

Show books (ShowOperation)

Exit the system (ExitOperation)

4. User part

User abstract class (User)

Administrator class (Administrator)

Normal User (NormalUser)

5.main method (Test)

Complete code:


1. Overall framework

We adopt object-oriented programming ideas, abstract the entire library management system into multiple objects, and then complete our overall design requirements through the interaction between each object.

Our overall design framework is as follows:

We made the following design by extracting their commonalities

  • Our books are placed on shelves so they are in the same package
  • Our operations of adding, deleting, checking, and modifying are all user operations on books, so they are in the same package, which makes it more convenient for different users to call these operations.
  • Users are divided into ordinary users and administrator users. They are all direct operators of the library management system, so they are in the same package.

Corresponding to our above structure diagram, we design as follows:


2. Books and bookshelves

Books

We should provide information about books:

  • book title
  • author
  • price
  • book type
  • Borrowing status

In order toreflect the object-oriented encapsulation characteristics, we set these field information to private a> methods to provide access to other objects public Then set some

package BookRack;

//书籍
public class Book {
    private String name;//书名
    private String author;//作者名
    private int price;//价格
    private String type;//书的类型
    private boolean isBorrowed;//是否已经被借出
    
    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;
    }
    
    //构造方法初始化
    public Book(String name, String author, int price, String type) {
        //图书默认没有借出,所以isBorrowed默认false,不需要初始化
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }
    
    //方便我们打印整个书籍的全部信息
    @Override
    public String toString() {
        return "Book.Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                '}';
    }
}

Bookshelf (BookRack)

Bookshelf is mainly used to store books, so we only need to provide the following two pieces of information:

  • The stored books are an array of books. Each element in the array is a book object.
  • The number of books that have been stored

In order to reflectobject-orientedencapsulation, we Or set these field information to private and then set some public method to provide access to other objects. Here we store three books on the bookshelf, so we make corresponding initialization in the construction method

package BookRack;

//书架
public class BookRack {
    private Book[] books;//存放的所有的书
    private int uesdSize;//书架上已经放的书的数量
    
    public BookRack() {
        this.books = new Book[10];//默认书架可以放10本书
        this.books[0] = new Book("三国演义","罗贯中",20,"小说");
        this.books[1] = new Book("西游记","吴承恩",9,"小说");
        this.books[2] = new Book("红楼梦","曹雪芹",19,"小说");
        this.uesdSize = 3;//默认书架上有3本书
    }
    //拿到某个位置的书籍
    public Book getBooks(int pos) {
        return books[pos];
    }
    //设置某个位置的书籍
    public void setBooks(Book book,int pos) {
        books[pos] = book;
    }
    
    public int getUesdSize() {
        return uesdSize;
    }
    
    public void setUesdSize(int uesdSize) {
        this.uesdSize = uesdSize;
    }
}

3. Related operations on books

Operation interface (IOoperation)

All operations are performed on the bookshelf, so we provide an interface here for different operations to be implemented, and pass in the parameters of the bookshelf class to them.

package Operation;

import BookRack.BookRack;

//操作接口
public interface IOperation {
    //我们的任何增删查改的操作都是对于书架进行操作的,所以传入的参数是书架类
    void work(BookRack bookRack);
}

Add books (AddOperation)

First, we need to let the user input relevant information about the book they want to add, and then we create a new object for the book the user inputs. Next is the legality judgment. We take the newly created book object and the books on the bookshelf. Each book object is traversed and compared. If there are no duplicate books, this book can be stored. To save this book, just call the method provided in the bookshelf classsetBooks , after adding a new book, the number of corresponding books must also be increased, that is, calling the setUesdSize method to increase the number of books

package Operation;

import BookRack.BookRack;
import BookRack.Book;
import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookRack bookRack) {
        System.out.println("新增图书操作进行中......");
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("请输入您要添加的图书的书名:");
        String name = scanner.nextLine();
        System.out.println("请输入您要添加的图书的作者名:");
        String author = scanner.nextLine();
        System.out.println("请输入您要添加的图书的价格:");
        int price = scanner.nextInt();
        System.out.println("请输入您要添加的图书的类型:");
        String type = scanner.nextLine();
        
        //因为我们的每一个书都是一个对象,书架是一个对象数组,我们新加图书的时候就应该新实例化一个对象
        Book book = new Book(name,author,price,type);
        
        //合法性判断
        int usedSize = bookRack.getUesdSize();//拿到当前书架内放了书的数量
        for (int i = 0; i < usedSize; i++) {
            //遍历书架中的图书挨个对比名字是否相同
            Book tempbook = bookRack.getBooks(i);
            if (tempbook.getName().equals(name)) {
                System.out.println("不能重复添加同一本书,请重试!");
                return;
            }
        }
        
        //可以添加新的图书
        bookRack.setBooks(book,usedSize);
        bookRack.setUesdSize(usedSize+1);
    }
}

Borrow books (BorrowOperation)

First, we need to let the user input relevant information about the book they want to add, then we create a new object for the book the user input, and then we traverse the books on the bookshelf one by one. If there are any, they can be borrowed. Just change the borrowing status of the book to true. If not, the user will be told that the book is not available and cannot be borrowed

package Operation;

import BookRack.BookRack;
import BookRack.Book;
import java.util.Scanner;

public class BorrowOperation implements IOperation{
    @Override
    public void work(BookRack bookRack) {
        System.out.println("借阅图书操作进行中......");
        System.out.println("请输入您想借阅的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        
        //遍历整个书架
        for (int i = 0; i < bookRack.getUesdSize(); i++) {
            
            Book temp = bookRack.getBooks(i);
            if (name.equals(temp.getName())) {
                temp.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有查询到您想要借阅的图书,请重新尝试!");
    }
}

Delete book(DeleteOperation)

First of all, the first step to delete a book should be to find the book first. Therefore, just like borrowing the book just now, we first traverse the entire bookshelf to find the book, then record the location of the book, and then use it The bookshelf provides the setBooks method to store this book. If it is not found, tell the user and exit the operation

package Operation;

import BookRack.BookRack;
import BookRack.Book;
import java.util.Scanner;

public class DeleteOperation implements IOperation{
    @Override
    public void work(BookRack bookRack) {
        System.out.println("删除图书操作进行中......");
        System.out.println("请输入您想删除的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        
        //要删除的前提是先找到这本书
        int uesdSize = bookRack.getUesdSize();
        int flag = -1;
        int i = 0;
        for (; i < uesdSize; i++) {
            Book tempbook = bookRack.getBooks(i);
            if (tempbook.getName().equals(name)) {
                //找到这本书
                flag = i;
                break;
            }
        }
        if (i >= uesdSize) {
            System.out.println("查无此书,无法删除");
            return;
        }
        //存在这本书,进行删除,也就是将书架中的书从后向前依次覆盖
        for (int j = flag; j < uesdSize; j++) {
            Book tempbook = bookRack.getBooks(j+1);//拿到 j+1 位置的书
            bookRack.setBooks(tempbook,j);//和 j 位置的书交换
        }
        bookRack.setBooks(null,uesdSize-1);//将最后一个位置的图书置为空
        bookRack.setUesdSize(uesdSize-1);//图书数量减一
        System.out.println("删除成功!");
    }
}

Find books (FindOperation)

Searching for books is very simple. We have already completed this part of the operation in the deletion of books just now.

package Operation;

import BookRack.BookRack;
import BookRack.Book;
import java.util.Scanner;

public class FindOperation implements IOperation{
    @Override
    public void work(BookRack bookRack) {
        System.out.println("查找图书操作进行中......");
        System.out.println("请输入您想查找的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int usedSize = bookRack.getUesdSize();
        
        for (int i = 0; i < bookRack.getUesdSize(); i++) {
            Book temp = bookRack.getBooks(i);
            if (name.equals(temp.getName())) {
                System.out.println("存在这本书,信息如下:");
                System.out.println(temp);
                return;
            }
        }
        System.out.println("没有你要找的这本书,书名为:"+ name);
    }
}

Return books (ReturnOperation)

The operation is the same as our borrowing books. The only difference is that the borrowing status of the book is changed to false 

package Operation;

import BookRack.BookRack;
import BookRack.Book;
import java.util.Scanner;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookRack bookRack) {
        System.out.println("借阅图书操作进行中......");
        System.out.println("请输入您想借阅的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        
        //遍历整个书架
        for (int i = 0; i < bookRack.getUesdSize(); i++) {
            Book tempbook = bookRack.getBooks(i);
            if (name.equals(tempbook.getName())) {
                tempbook.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
        System.out.println("没有你要归还的图书:"+name);
    }
}

Show books (ShowOperation)

Just traverse the entire bookshelf and print out the book information one by one.

package Operation;
import BookRack.Book;
import BookRack.BookRack;

public class ShowOperation implements IOperation{
    @Override
    public void work(BookRack bookRack) {
        System.out.println("图书列表如下:");
        for (int i = 0; i < bookRack.getUesdSize(); i++) {
            Book tempbook = bookRack.getBooks(i);
            System.out.println(tempbook);
        }
    }
}

Exit the system (ExitOperation)

We can directly use exit  to end the entire program

package Operation;

import BookRack.BookRack;

public class ExitOperation implements IOperation{
    @Override
    public void work(BookRack bookRack) {
        System.out.println("退出系统...");
        System.exit(0);
    }
}

4. User part

User abstract class (User)

There are many commonalities between ordinary users and administrator users, so we set up an abstract class here for ordinary users and administrator users to inherit and use.

package Person;

import BookRack.BookRack;
import Operation.IOperation;

public abstract class User {
    protected String name;//姓名
    protected IOperation[] iOperations;//操作接口数组
    public abstract int menu();//菜单
    
    public User(String name) {
        this.name = name;
    }
    //供用户来选择操作,调用操作接口
    public void doOperation(int choice, BookRack bookRack) {
        IOperation ioperation = iOperations[choice];
        ioperation.work(bookRack);
    }
}

Administrator class (Administrator)

We set the menu for managing users, and then set the specific operations of the array of interface types corresponding to the menu.

package Person;

import Operation.*;

import java.util.Scanner;

public class Administrator extends User {
    public Administrator(String name) {
        super(name);
        iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DeleteOperation(),
                new ShowOperation()
        };
    }

    public int menu() {
        System.out.println("********管理员菜单********");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出系统");
        System.out.println("************************");
        System.out.println("请输入你的操作:");
        //通过输入来调用对用的功能
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

Normal User (NormalUser)

Just like the administrator user settings, we can store the corresponding operations in the interface array.

package Person;

import Operation.*;
import Person.User;

import java.util.Scanner;

public class NormalUser extends User {
    public NormalUser(String name) {
        super(name);
        iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }
    public int menu() {
        System.out.println("********普通用户菜单********");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("***************************");
        System.out.println("请输入你的操作:");
        //通过输入来调用对用的功能
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

5.main method (Test)

Let's set up a login program here. When logging in as an administrator, we create a new administrator object. When logging in as an ordinary user, we create a new ordinary user object. Then we call the options in our menu based on the user's input, which is what we just set. Operations in the interface operation array

import BookRack.BookRack;
import Person.Administrator;
import Person.NormalUser;
import Person.User;

import java.util.Scanner;

public class Test {
    public static User login() {
        System.out.println("请输入您的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        
        System.out.println("请确认您的身份:");
        System.out.println("1.管理员");
        System.out.println("2.普通用户");
        int choice = scanner.nextInt();
        if (choice == 1) {
            return new Administrator(name);
        }else{
            return new NormalUser(name);
        }
    }
    
    public static void main(String[] args) {
        BookRack bookRack = new BookRack();
        User user = login();//向上转型
        while (true) {
            int choice = user.menu();
            user.doOperation(choice, bookRack);
        }
    }
}

Complete code:

Just follow the author's corresponding package and class settings. Each class and package is completely given above.

If you find it troublesome, the author gives the corresponding code cloud here. You can get it yourself if necessary.

 LibrarySystem · Lu Ming/JavaSE




 That’s it for this sharing. I hope my sharing can be helpful to you. I also welcome everyone’s support. Your likes are the biggest motivation for bloggers to update! If you have different opinions, you are welcome to actively discuss and exchange in the comment area, let us learn and make progress together! If you have relevant questions, you can also send a private message to the blogger. The comment area and private messages will be carefully checked. See you next time!  

Guess you like

Origin blog.csdn.net/m0_69519887/article/details/134489792