Library Management System Teach you step-by-step how to implement library management system design in Java

 

There are no shortcuts to the ladder to heaven, only hard climbing. Come on, friends! !


Table of contents

1. Implementation ideas:

2. So how to find a partner?

 3. Implementation of Book class

Book class total code:

4. Implementation of BookList class

Total code of BookList class:

5. User operations

5.1 AddOperation class

5.2 FindOperation class

 5.3 DelOperation class

5.4 ShowOperation class

5.5 ExitOperation class

5.6 BorrowedOperation class 

5.7 ReturnOperation class 

5.8 Key ideas 

6. User

 6.1 User class

6.2 Administrator class

 6.3 Ordinary user class

 7. Login design 

7.1 Design of user identity selection

7.2 Menu design 

7.3 Design for users to choose operations 

 7.4 Experiment

8. Improve related operation functions

 8.1 Find books 

Implementation code 

 8.2 Add new books

Implement code

8.3 Display books

Implement code

8.4 Delete books

 Implementation code

 8.5 Borrowing books 

 Implementation code

 8.6 Returning books

 Implementation code

9. Total code


 The total code link is attached at the end of the article.

1. Implementation ideas:

1). The library management system has different users, one is administrator user, and the other is Ordinary users;

2). The operations of administrators and ordinary users are different;

3). To implement it with an object-oriented approach, we need tofind objects, create objects, and use objects!

2. So how to find a partner?

We can easily think of two objects:Books and people

So we first need to create a book package, and then create a book class in the package

Then we can’t have only one book, right? Our library management system can’t only manage one book, right?

So we need something to store books, which can be roughly understood as a bookshelf. This bookshelf can hold several books, so we need to use arrays to operate, so we create a class to store arrays, so in our book package There are two classes

 

(Note: The picture comes from the Internet. If there is any infringement, please contact us to delete it) 

 3. Implementation of Book class

 Let’s think about what attributes the Book class has?     Book title? author? price? type? Is it being lent?

//Book.java
package book;

public class Book {
    private String name;//书名
    private String autho;//作者
    private int price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否被借出
}

Since these attributes are private modified, we need to provide getter < a i=4>and setter method

//Book.java
package book;

public class Book {
    private String name;//书名
    private String autho;//作者
    private int price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否被借出

    public String getName() {
        return name;
    }

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

    public String getAutho() {
        return autho;
    }

    public void setAutho(String autho) {
        this.autho = autho;
    }

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

These properties of ours need to construct the input object, so we need to provideconstructor method for initialization

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

 Note one thing: we have no structure here isBorrowed! ! why?

isBorrowed is a boolean type, the default is false  ,If we instantiate a book, does it mean that the book has not been lent by default? So our construction method does not need to add isBorrowed

At the same time, we also need to rewrite atoString method!

@Override
public String toString() {
        return "Book{" +
        "name='" + name + '\'' +
        ", autho='" + autho + '\'' +
        ", price=" + price +
        ", type='" + type + '\'' +
        ", isBorrowed=" + isBorrowed + 
        '}';
}

Our Book class is complete! ! !

Book class total code:

package book;

public class Book {
    private String name;//书名
    private String autho;//作者
    private int price;//价格
    private String type;//类型
    private boolean isBorrowed;//是否被借出

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

    public String getName() {
        return name;
    }

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

    public String getAutho() {
        return autho;
    }

    public void setAutho(String autho) {
        this.autho = autho;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", autho='" + autho + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }
}

4. Implementation of BookList class

 To store a number of books on the bookshelf, it must be an array of Book type. At the same time, we also need to record how many books are currently stored on the bookshelf.

public class BookList {
    private Book[] books;
    private int usedSize;//记录当前存放的书籍数量
}

The next step is to create a constructor method for initialization.

public BookList() {
    this.books = new Book[10];
    this.usedSize = 0;
}

Since we don’t know what books are in the books yet, we will put them first, but we know the bookshelf.

public int getUsedSize() {
    return usedSize;
}

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

 The BookList is temporarily completed!​ 

Total code of BookList class:

package book;

public class BookList {
    private Book[] books;
    private int usedSize;//记录当前存放的书籍数量

    public BookList() {
        this.books = new Book[10];
        this.usedSize = 0;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

(Note: The picture comes from the Internet. If there is any infringement, please contact us to delete it) 

5. User operations

 What operations can administrator users perform? Looking for a book? Add a new book? Delete book? Show books? Exit system?

What operations can ordinary users do? Search for books? Borrowing books? Return books? Exit system?

So how do we separate the operations of different users?​ 

Some operations are common and some operations are separate. We need to find ways to organize these operations.

Idea:Each operation is independent and written as a class

We first create a package to create classes for related operations

5.1 AddOperation class

 

 This class is used to add new books. Where to add them? Added to BookList

package operation;

import book.BookList;

public class AddOperation {
    public void work(BookList bookList){
        System.out.println("新增图书!");
    }
}

5.2 FindOperation class

Same as above, we are operating BookList

package operation;

import book.BookList;

public class FindOperation {
    public void work(BookList bookList){
        System.out.println("查找图书!");
    }
}

 5.3 DelOperation class

package operation;

import book.BookList;

public class DelOperation {
    public void work (BookList bookList){
        System.out.println("删除图书!");
    }
}

5.4 ShowOperation class

package operation;

import book.BookList;

public class ShowOperation {
    public void work(BookList bookList) {
        System.out.println("显示图书!");
    }
}

5.5 ExitOperation class

package operation;

import book.BookList;

public class ExitOperation {
    public void work(BookList bookList){
        System.out.println("退出系统!");
    }
}

5.6 BorrowedOperation class 

package operation;

import book.BookList;

public class BorrowedOperation {
    public void work(BookList bookList){
        System.out.println("借阅图书!");
    }
}

5.7 ReturnOperation class 

package operation;

import book.BookList;

public class ReturnOperation {
    public void work(BookList bookList){
        System.out.println("归还图书!");
    }
}

5.8 Key ideas 

Idea: We can find that no matter which operation it is, it is a parameter of BookList type, so in the future we can organize several classes together and use interfaces to receive them, so we need To implement this interface

 

package operation;

import book.BookList;

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

Then implement all the previous operations to this interface, and then rewrite the work method

 Why do you do this?

If several classes can implement this interface, it will be useful to write code like this in the future:

This is done for the unification of types 

(Note: The picture comes from the Internet. If there is any infringement, please contact us to delete it) 

6. User

 The operation is already in place. Now we are doing the human-related ones. What are the human-related ones? Administrator? Ordinary user?

Let’s first create a user package

 6.1 User class

 What does the user have? username?

package user;

public class User {
    protected String name;

    public User(String name) {
        this.name = name;
    }
}

6.2 Administrator class

package user;

public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
    }
}

 6.3 Ordinary user class

package user;

public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
    }
}

 In this way, we have simply completed it. Now we return to the Main method class for login design.

 7. Login design 

7.1 Design of user identity selection

When entering the main method, we first define a method login to input a user name, and then let the user select an identity.Note: This method login can return the ordinary user class (NormalUser), or it can return Administrator (AdminUser), so the type of this method is defined as User, and upward transformation occurs here

import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {


    public static User login() {
        System.out.println("请输入姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入你的身份,1:管理员  2:普通用户 -》");
        int choice = scanner.nextInt();
        if(choice == 1){
            return new AdminUser(name);
        }else{
            return new NormalUser(name);
        }
    }

    public static void main(String[] args) {

    }
}

7.2 Menu design 

When login is called, our User will receive an object. Does this user point to an administrator object or a normal object? We don't know this, but no matter which object it points to, we need a menu, so we need to call the menu. Let ordinary users and administrators have their own menus, so we go back to the ordinary user class and the administrator class to create a menu respectively.

package user;

public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
    }
    public void menu(){
        System.out.println("******普通用户菜单******");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("*********************");
    }
}
package user;

public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
    }

    public void 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("********************");
    }
}

 This completes the menu. Can we directly call the menu through user.menu(); ? Let's try

reported an error! ! Why is an error reported? Let’s take a closer look, what is user? This is aparent class! Can a parent class directly call methods of a subclass? Obviously not, so here we need the parent class to have a menu, and then the subclass will rewrite it, so we go back to User to write the menu. Does this menu need to be implemented in detail? No need, so we set it as an abstract method.

package user;

public abstract class User {
    protected String name;

    public User(String name) {
        this.name = name;
    }

    public abstract void menu();
}

At this time, dynamic binding will occur when we call menu.

 (Note: The picture comes from the Internet, if there is any infringement, please contact us to delete it)

7.3 Design for users to choose operations 

When the menu printing is completed, we need to enter the operation, so we need to add a code after the menu to let the user know that the operation is to be entered at this time. We let choice receive this operation and return this operation, and we need to change the menu. If the type is int, then the menu in the parent class User also needs to be changed to int synchronously.

public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
    }

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

 Next comes the hard part, cheer up friends! ! How to organize the operations of administrators and the operations of ordinary users, and where to put them together? This goes back to the key ideas of  5.8   above.

 First define an array in User protected IOperation[ ] Ioperations; 

package user;

import operation.IOperation;

public abstract class User {
    protected String name;
    protected IOperation[] Ioperations;
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();
}

 Now there is a question: Has this array been initialized? No initialization! ! ! The initialization of an array either assigns a value to it or leaves it null. Each element here is not initialized, because no space is allocated to them , then we allocate space to them now , then how much space should we allocate to it from the User's perspective? 10? 20? We don’t know clearly. Taking NormalUser as an example, NormalUser inherits the name of the parent class and protected IOperation[ ] Ioperations; 

Then we will also face a problem, how much space should be allocated to it?

Pay attention to the difference between these two ways of writing. Array is directly assigned at the same time when it is defined.

So we have to design it like this:

 Because our operation classes all implement the interface IOperation, which is equivalent to a parent class reference referencing a subclass object, so we can do this:

 Why is it designed like this? Take a look at our menu:

Have you combined menu operations with array subscripts? Exit if the subscript is 0, search for books if the subscript is 1... 

Then the question arises again, won’t this design be mixed with the AdminUser menu? After all, our users’ menus are different.

 Let’s look at this:

 We have aif - else statement here. Will it return two objects at the same time? Either the administrator or the ordinary user will be returned here, and only one object will be returned. If we return AdminUser, will the NormalUser in the picture below be initialized? Won't! ! !

AdminUser also operates as above

 So now we have completed the operation arrays of the two users. When our program runs to login() in the figure below, does it return a menu of the corresponding object? It is either a menu for ordinary users or an administrator's menu. The data is ready at this time. If it is an administrator's menu, do you need to get a certain method in this array (the array in the picture above)? So we create a method in user, pass the choice in, select 1 to access the 1 subscript of this array, select 2 to access the 2 subscript of this array (as shown in Figure 2)

 

 When the user selects 1, then doOperation will return the 1 subscript and access the corresponding method, as shown below

 What we get is an object, how to access the object? This is equivalent to

this.ioperations[choice] = new ExitOperation();

This means that the previous part this.ioperations[choice] is equivalent to a reference. Does this object have any methods? There is a work method, so we can call it through the . number (The anonymous object uses the . access method), and the parameter of this work method is the bookshelf a>

 When we run the code there will be several books, here we create an object bookList

 Will automatically call the constructor without parameters

When we get here, can we initialize the corresponding book? We don't care about it. We return to the main method and pass in the second parameter. User also needs to pass in the second parameter.

 When choice is 1, call the 1 subscript of this array this.Ioperations[choice], and then access its work method

 The 1 subscript just said that it is FindOperation, and then give it bookList. Isn't this just calling the work method of the 1 subscript of our array this.Ioperations[choice]?

If you guys still can’t understand the line of code in the picture above, you can also write the line of code like this, which will make it easier to understand.

 7.4 Experiment

 Searching here once will end the process. We can loop the code

    public static void main(String[] args) {
        BookList bookList = new BookList();
        // user = new AdminUser(name);
        // user = new NormalUser(name);
        User user = login();
        while(true){
            int choice = user.menu();//根据菜单返回的choice来进行对应的操作
            user.doOperation(choice,bookList);
        }
    }
}

At the same time, we should pay attention to one point. When the process ends, we need to manually recycle the self-source. Or some people say that this will not be automatically recycled? But in fact, no one can recycle it. It is best for us to recycle it ourselves, instead of always relying on the system! ! !

public class ExitOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("退出系统!");
        //应该要对 bookList 资源 手动回收
        System.exit(0);
    }
}

(Note: The picture comes from the Internet. If there is any infringement, please contact us to delete it) 

8. Improve related operation functions

 To facilitate testing, let’s first put a few books on the bookshelf

 8.1 Find books 

 We need to rely on book titles to find books, so should we traverse the array? Currently we only have 3 books, and the array space is 10, then we only need to traverse the current number of books. At this time, we need to reference a The variables designed before

But there is another problem: usedSize and books are modified by private, so do we provide a getter method? 

Since the getter method is provided, we can use it like this 

 int currentSize = bookList.getUsedSize();

 And books does not provide a getter method, so we have to add it.

In this way we can get the array subscript, because name isString type, so we need to use equals to determine whether it is the book you are looking for 

Implement code 

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        System.out.println("请写出你要查找图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for(int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("存在这本书,信息如下:");
                System.out.println(book);
                return;
            }
        }
        //说明代码没有return出去,所以没有你要找的书
        System.out.println("没有你要找的这本书,书名为:" + name);
    }
}

 

(Note: The picture comes from the Internet. If there is any infringement, please contact us to delete it) 

 8.2 Add new books

 Books have four attributes: title, author, price, and type, so we need to enter these attributes. Do we need to remind the user to enter these attributes first?

import javax.swing.*;
import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookList bookList){
        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.next();

    }
}

With these four attributes, we can instantiate a book. Only with the book object can we add a new book.

Book book = new Book(name,author,price,type);

 Then the question is, where should a new book be placed in the array? It is not complicated here. We will put it at the last position by default. In the data storage structure , assuming subscript 3 and subscript 4, when the position of subscript 3 is empty, storage at the position of subscript 4 is not allowed! !

 When we add new books, we can use the currentSize variable to record the number of books. When we add a new book, let it ++. Before adding a book, we still need to judge whether this book has been saved? The code for this judgment is actually not much different from that used to search for books.

    //此时就可以构造出一个书的对象
    Book book = new Book(name,author,price,type);
    int currentSize = bookList.getUsedSize();

    for(int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if (tmp.getName().equals(name)) {
                System.out.println("存在这本书,不能重复添加!");
                return;
            }
    }

If there are no repeated additions, then go outside the loop and add new books. Where should the new additions be placed in the array? We need to set up a method in BookList to provide a location for Book. If we want to store a book at the pos location, then we need to pass the book to it in the parameter part.

public void setBook(Book book,int pos) {
    books[pos] = book;
}
//没有重复的书,开始新增
bookList.setBook(book,currentSize);
bookList.setUsedSize(currentSize + 1);

Implement code

package operation;

import book.Book;
import book.BookList;

import javax.swing.*;
import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookList bookList){
        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.next();

        //此时就可以构造出一个书的对象
        Book book = new Book(name,author,price,type);
        int currentSize = bookList.getUsedSize();

        for(int i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if (tmp.getName().equals(name)) {
                System.out.println("存在这本书,不能重复添加!");
                return;
            }
        }
        //没有重复的书,开始新增
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize + 1);
    }
}

8.3 Display books

In fact, it is what I wrote earlierFindOperation class 

Implement code

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation {
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        System.out.println("请写出你要查找图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for(int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("存在这本书,信息如下:");
                System.out.println(book);
                return;
            }
        }
        //说明代码没有return出去,所以没有你要找的书
        System.out.println("没有你要找的这本书,书名为:" + name);
    }
}

8.4 Delete books

To delete a book, we must first determine whether the book exists. Can we delete it without the book? So first let’s use a for loop to determine whether this book exists. This code is actually the code for the Add function. If this book is found, we need to return the subscript where the book is located. Here index = -1, which is actually Used to determine whether the book is found. If the book is found, the index will change and the subscript of the book to be deleted will be recorded.

System.out.println("请输入你要删除图书的书名:");
int currentSize = bookList.getUsedSize();

int index = -1;
String name = scanner.nextLine();
int i = 0;
for( i = 0; i < currentSize; i++) {
      Book tmp = bookList.getBook(i);
      if (tmp.getName().equals(name)) {
            index = i;
            break;//记录下来了 要删除图书的名字
      }
}

if(i == -1) {
     System.out.println("没有你要删除的图书!");
     return;
}
//可以删除了

So how to delete it? It's very simple. Suppose we want to delete Journey to the West, then we only need to let Dream of Red Mansions overwrite Journey to the West, and then Java will overwrite Dream of Red Mansions, currentSize--, that's it. At the same time, our index records the subscript of the data to be deleted. , which also facilitates our overwriting operation

 We first use the getter method to get the book at position j+1, and then use the setter method to get the position j to overwrite it.

for (int j = index; j < currentSize-1; j++) {
      //bookList[j] = bookList[j+1];
      Book book = bookList.getBook(j+1);
      bookList.setBook(book,j);
}

Note: The condition currentSize of the array here must be -1, otherwise it will go out of bounds. Look at the picture below. If index = 3, it will enter the loop, and then j + 1 = 4, and then it will go out of bounds?

 But there is still a problem here. The space in our 3 subscript is useless, but it still takes up space. This causes a memory leak, so we need to actively set it to null. At the same time, this can also prevent us from deleting or covering the java subscript 2 in the picture above with other books. Theoretically, the java book should not be saved, but the java book is still saved at the 3 subscript. Case

bookList.setUsedSize(currentSize-1);
bookList.setBook(null,currentSize-1);

 Implementation code

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work (BookList bookList){
        System.out.println("删除图书!");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你要删除图书的书名:");
        int currentSize = bookList.getUsedSize();

        int index = -1;
        String name = scanner.nextLine();
        int i = 0;
        for( i = 0; i < currentSize; i++) {
            Book tmp = bookList.getBook(i);
            if (tmp.getName().equals(name)) {
                index = i;
                break;//记录下来了 要删除图书的名字
            }
        }
        //
        if(i == -1) {
            System.out.println("没有你要删除的图书!");
            return;
        }
        //可以删除了

        for (int j = index; j < currentSize-1; j++) {
            //bookList[j] = bookList[j+1];
            Book book = bookList.getBook(j+1);
            bookList.setBook(book,j);
        }
        bookList.setUsedSize(currentSize-1);
        bookList.setBook(null,currentSize-1);
    }
}

(Note: The picture comes from the Internet. If there is any infringement, please contact us to delete it) 

 8.5 Borrowing books 

First, let the user enter the title of the book to be borrowed, then determine whether the book exists, and then use the setBorrowed method to set it to true, then it will be successful.

At the same time, pay attention to the situation where the loan is not successful and let the user know that the book is not available. 


System.out.println("没有你要借阅的图书:"+name);

 Let's test it

 Finally, whether to lend it out. Isn’t this ugly? Let's modify it, modify toString in the book class, and use the ternary operator

@Override
public String toString() {
     return "Book{" +
           "name='" + name + '\'' +
           ", autho='" + autho + '\'' +
           ", price=" + price +
           ", type='" + type + '\'' +
           ( (isBorrowed == true) ? "已借出" : "未借出") +
           ", isBorrowed=" + isBorrowed +
           '}';
}

 Implementation code

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowedOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("借阅图书!");
        System.out.println("请写出你要借阅图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for(int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                //有这本书
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
        System.out.println("没有你要借阅的图书:"+name);
    }
}

 8.6 Returning books

In fact, it is just a matter of fine-tuning the code for borrowing books in 8.5 and changing true to false.

 Implementation code

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList){
        System.out.println("归还图书!");
        System.out.println("请写出你要归还图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for(int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                //有这本书
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
        System.out.println("没有你要归还的图书:"+name);
    }
}

9. Total code

Login - Gitee.com

Click JAVA, and then find the library management system. All the codes are in it!

 

(Note: The picture comes from the Internet. If there is any infringement, please contact us to delete it)  

Hope this helps everyone, thanks for watching! ! !

Guess you like

Origin blog.csdn.net/A1546553960/article/details/134482245