Java implements library management system

1. Background introduction :
After studying java programming for a period of time, a more comprehensive example is needed for programming practice, which is an opportunity to sum up and improve the content learned some time ago. After choosing a library management system, the knowledge of javaSE that will be used includes: variables, packages, inheritance, classes, interfaces, loop structures, etc. It is a very comprehensive example.

2. Core requirements
1. Users can log in to the system and are divided into two roles: administrator and ordinary user. These two different roles can perform different operations according to their identities.

Ordinary users
a) Check the information of a book
b) Borrow books
c) Return books
d) Exit the program

The administrator
a) checks the information of a book
b) adds a book
c) deletes a book
d) checks the book list
e) exits the program

Program framework structure diagram
insert image description here
3, code and detailed explanation
1.User class

package booksystem;

import booksystem.operation.IOperation;

abstract public class User {
    
    
    protected String name;//定义书名
    protected IOperation[] operations;//定义一个接口数组
    public abstract int menu();//是用户和管理员的父类,不进行实例化,所以定义为抽象方法

    public void doOperation(int choice,BookList bookList){
    
    
        IOperation operation=this.operations[choice-1];
        operation.work(bookList);
    }
}

The User class is the parent class of the NormalUser class and the Admin class. Since no instantiation is required, the menu() function is defined as an abstract function.
2.NormalUser class

package booksystem;

import booksystem.operation.*;
import booksystem.operation.IOperation;

import java.util.Scanner;

public class NormalUser extends User {
    
    

    public NormalUser(String name) {
    
    
        this.name = name;
        this.operations = new IOperation[]
                {
    
    
        new FindOperation(),
        new BorrowOperation(),
        new ReturnOperation(),
        new ExitOperation(),
        };
    }
    @Override
    public int menu(){
    
    
        System.out.println("~~~~~~~~~~~~~~~~~~");
        System.out.println("Hello"+name+"Welcome to use booksyetem");
        System.out.println("1.查阅书籍信息");
        System.out.println("2.借阅书籍");
        System.out.println("3.归还书籍");
        System.out.println("4.退出系统");
        System.out.println("~~~~~~~~~~~~~~~~~~");
        System.out.println("请输入您的选择:");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;//返回一个整型数
    }
}

The NormalUser class is written for ordinary users. An interface array is defined in the code, and the four major functions of consulting, borrowing, returning, and exiting the system that ordinary users need to use are added, and the menu() function is also in accordance with the same In order, the menu() function overrides the parent class, so in order to remind the compiler, @Override is added before the function header.
3.Admin class

package booksystem;

import booksystem.operation.*;

import java.util.Scanner;

public class Admin extends User {
    
    
    public Admin(String name){
    
    
        this.name=name;
        this.operations=new IOperation[]{
    
    
        new FindOperation(),
        new AddOperation(),
        new DelOperation(),
        new DisplayOperation(),
        new ExitOperation(),
        };
    }
    @Override
    public int menu(){
    
    
        System.out.println("~~~~~~~~~~~~~~~~~~");
        System.out.println("Hello"+name+"Welcome to use booksyetem");
        System.out.println("1.查阅书籍信息");
        System.out.println("2.新增书籍信息");
        System.out.println("3.删除书籍信息");
        System.out.println("4.退出系统");
        System.out.println("~~~~~~~~~~~~~~~~~~");
        System.out.println("请输入您的选择:");
        Scanner scanner=new Scanner(System.in);
        int choice=scanner.nextInt();
        return choice;
    }
}

The idea of ​​writing the Admin class is the same as that of the NormalUser class. The difference is that the user interface is different, and the functions to be used are also different, namely query, add, delete and exit the system.
4. Book class

package booksystem;

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

    public Book(String name, String author, double 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 + '\'' + ",isBorrow=" + isBorrowed + '}';
    }

    public String getName() {
    
    
        return name;
    }

    public boolean isBorrowed(){
    
    
        return isBorrowed;
    }

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

}

For books, the Book class defines the attributes of books, author, price, name, category, rewrites the toString function, and several commonly used operating functions for books, getName, and judges whether to lend and defines book lending state function.
5. BookList class

package booksystem;

public class BookList {
    
    
    private  Book[] books=new Book[100];//定义一book数组
    private int size=0;

    public BookList(){
    
    
        books[0]=new Book("计算机网络教程","郝文源",125,"专业书籍");
        books[1]=new Book("盗墓笔记","南派三叔",150,"网络小说");
        books[2]=new Book("三体","刘慈欣",178,"科幻小说");
        size = 3;
    }//给book数组中初始化一些书
    public Book getBook(int index){
    
    
        return books[index];
    }
    public void setBook(int index,Book book)
    {
    
    
       books[index]=book;
    }
    public int getSize(){
    
    
        return size;
    }
    public void setSize(int size){
    
    
        this.size=size;
    }
}

A book array is defined in the BookList class, and some books are initialized in the array, and common function functions are defined

6. Main class

package booksystem;

import java.util.Scanner;
public class Main {
    
    
    public static void main(String[] args){
    
    
        Object o=null;
        BookList booklist= new BookList();

        User user=login();//上转型,这里先调用了login()函数,返回一个Admin对象或NormalUser对象

        while(true){
    
    
            int choice=user.menu();
            user.doOperation(choice,booklist);
        }//在进行退出系统的功能时,会一直进行循环,menu()函数最终会返回一个整型数,对应选择操作中的一项
    }

public static User login() {
    
    
    System.out.println("请输入您的姓名");
    Scanner scanner = new Scanner(System.in);
    String name = scanner.next();
    System.out.println("请输入您的角色:1 管理员 0 普通用户");//根据不同的选择创建对应的对象
    int who = scanner.nextInt();
    if (who == 1) {
    
    
        return new Admin(name);
    }
    return new NormalUser(name);
}
}

The main function mainly implements the login () function. According to the selection of the login system user, different identities are determined and one of the two objects is returned. In the while loop, as long as the exit function is not performed, the loop will continue to execute.
7. IOperation

package booksystem.operation;

import booksystem.BookList;

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

8.AddOperation

package booksystem.operation;

import booksystem.Book;

import booksystem.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {
    
    
    @Override
    public void work(BookList bookList){
    
    
        Scanner scanner=new Scanner(System.in);
        System.out.println("新增书籍");
        System.out.println("请输入新书籍的名称");
        String name=scanner.next();
        System.out.println("请输入新书籍的作者");
        String author=scanner.next();
        System.out.println("请输入新书籍的价格");
        double price=scanner.nextDouble();
        System.out.println("请输入新书籍的类别");
        String type=scanner.next();
        Book newBook=new Book(name,author,price,type);
        int curSize=bookList.getSize();
        bookList.setBook(curSize,newBook);
        bookList.setSize(curSize+1);
        System.out.println("新增书籍成功");
    }
}

9.BorrowOperation

package booksystem.operation;

import booksystem.Book;

import booksystem.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation{
    
    
    @Override
    public void work(BookList bookList)
    {
    
    
        Scanner scanner=new Scanner(System.in);
        System.out.println("借阅书籍");
        System.out.println("请输入要借阅的书籍的名称");
        String name=scanner.next();

        int i=0;
        for(;i<bookList.getSize();i++)
        {
    
    
            if(bookList.getBook(i).getName().equals(name)){
    
    
                break;
            }
        }
        if(i>=bookList.getSize()){
    
    
        System.out.println("未找到指定的书籍,无法借阅!");
        return;
        }
        Book currentBook=bookList.getBook(i);
        if(currentBook.isBorrowed()){
    
    
            System.out.println("该书籍已经被借出!");
            return;
        }
        bookList.getBook(i).setBorrowed(true);
        System.out.println("借书成功!");
    }


}

10.DelOperation

package booksystem.operation;

import booksystem.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    
    
    @Override
    public void work(BookList bookList)
    {
    
    
        Scanner scanner=new Scanner(System.in);
        System.out.println("删除书籍");
        System.out.println("请输入要删除的书籍的名称");
        String name=scanner.next();

        int i=0;
        for(;i<bookList.getSize();i++)
        {
    
    
            if(bookList.getBook(i).getName().equals(name)){
    
    
                break;
            }
        }
        if(i>=bookList.getSize()){
    
    
            System.out.println("您输入的书籍在+"+name+"在系统中没有找到!删除失败!");
            return;
        }
        if(i==bookList.getSize()-1)
        {
    
    
            bookList.setSize(bookList.getSize()-1);
            System.out.println("删除成功!");
            return;
        }
        bookList.setBook(i,bookList.getBook(bookList.getSize()-1));
        bookList.setSize(bookList.getSize()-1);
        System.out.println("删除成功!");
    }
}

11.ExitOperation

package booksystem.operation;

import booksystem.BookList;

public class ExitOperation implements IOperation{
    
    
    @Override
    public void work(BookList bookList)
    {
    
    
        System.out.println("退出程序");
        System.exit(0);
    }
}

12.FindOperation

package booksystem.operation;

import booksystem.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation {
    
    
    @Override
    public void work(BookList bookList)
    {
    
    
        System.out.println("查找书籍");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请问要查找的书名");
        String name=scanner.next();
        for(int i=0;i<bookList.getSize();i++)
        {
    
    
            if(bookList.getBook(i).getName().contains(name)){
    
    

                System.out.println(bookList.getBook(i));

            }
        }
    }
}

13.ReturnOperation

package booksystem.operation;

import booksystem.BookList;

import booksystem.Book;
import java.util.Scanner;

public class ReturnOperation implements IOperation{
    
    
@Override
    public void work(BookList bookList){
    
    
    Scanner scanner=new Scanner(System.in);
    System.out.println("归还书籍");
    System.out.println("请输入要还的书籍的名称");
    String name=scanner.next();
    int i=0;
    for(;i<bookList.getSize();i++)
    {
    
    
        Book book=bookList.getBook(i);
        if(book.getName().equals(i))
        {
    
    
            break;
        }
    }
    if(i>=bookList.getSize())
    {
    
    
        System.out.println("书籍没有找到,无法归还");
        return;
    }
    Book currentBook=bookList.getBook(i);
    if(!currentBook.isBorrowed())
    {
    
    
        System.out.println("这本书没有借出,无法归还");
    }
    currentBook.setBorrowed(false);
    System.out.println("归还书籍成功");
    return;
}
}

14.DisplayOperation

package booksystem.operation;

import booksystem.BookList;
public class DisplayOperation implements IOperation {
    
    
    @Override
    public void work(BookList bookList){
    
    
        System.out.println("显示书籍列表");
        for(int i=0;i<bookList.getSize();i++)
        {
    
    
            System.out.println(bookList.getBook(i));
        }
    }
}

4. Programming screenshots and test diagrams
insert image description here
Package and class placement diagrams
insert image description here
Running screenshots

Guess you like

Origin blog.csdn.net/qq_45742383/article/details/114379869