Java implements a simple library management system (with source code inside)

Introduction

Hello everyone, the author also talked about a lot of knowledge points in Java before. In order to consolidate the previous knowledge points and let us go deep into the basic characteristics of Java object-oriented, let us complete a small library management system. project it.

Project introduction: Through the two operation interfaces of administrators and ordinary users, the management of books is realized by using the methods and the interaction between objects .

source code

book bag

Mainly includes book object and book List object and Main method

Book class

Describe the relevant information of the book, the construction method and various getter and setter methods

public class Book {
    //书的属性:名字,作者,价格,类型,借出情况
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean borrowed;

    public Book(){

    }

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

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return author
     */
    public String getAuthor() {
        return author;
    }

    /**
     * 设置
     * @param author
     */
    public void setAuthor(String author) {
        this.author = author;
    }

    /**
     * 获取
     * @return price
     */
    public int getPrice() {
        return price;
    }

    /**
     * 设置
     * @param price
     */
    public void setPrice(int price) {
        this.price = price;
    }

    /**
     * 获取
     * @return type
     */
    public String getType() {
        return type;
    }

    /**
     * 设置
     * @param type
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * 获取
     * @return borrowed
     */
    public boolean isBorrowed() {
        return borrowed;
    }

    /**
     * 设置
     * @param borrowed
     */
    public void setBorrowed(boolean borrowed) {
        this.borrowed = borrowed;
    }

    public String toString() {
        return "Book{name = " + name + ", author = " + author + ", price = " + price + ", type = " + type + "," +/*", borrowed = " + borrowed*/
                ((isBorrowed() == true) ? " 已被借出" : " 未被借出")+ "}";
    }
}

BookList

As a bookshelf, use an array to store books. 

public class BookList {
    //定义数组成员表示存放书的数组
    public Book[] books;
    //表示书架上存放书的数量
    private int useSize;
    //设置最大容量
    private static final int DEFAULT_CAPACITY = 10;

    public BookList() {
        this.books = new Book[DEFAULT_CAPACITY];
        //先提前放好三本书
        this.books[0] = new Book("三国演义", "罗贯中", 10, "小说");
        this.books[1] = new Book("西游记", "吴承恩", 9, "小说");
        this.books[2] = new Book("红楼梦", "曹雪芹", 19, "小说");

        this.useSize = 3;
    }

    //书架上书的数量的getter和setter方法
    public int getUseSize() {
        return useSize;
    }

    public void setUseSize(int useSize) {
        this.useSize = useSize;
    }

    //通过下标获取对应书籍的getter方法
    public Book getBook(int pos) {
        return books[pos];
    }

    //通过下标和传入的书对象设置对应书籍的setter方法
    public void setBooks(int pos, Book book) {
        books[pos] = book;
    }

    public Book[] getBooks() {
        return books;
    }
}

Main method

main operation logic 

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

import java.util.Scanner;

public class Main {
    //可以利用返回值的向上转型 达到发挥的一致性
    public static User Login() {
        System.out.println("请输入你的姓名:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("请输入你的身份:1.管理员 2.普通用户 ->");
        int choice = sc.nextInt();
        if (choice == 1) {
            //管理员
            return new AdminUser(name);
        } else if (choice == 2) {
            //普通用户
            return new NormalUser(name);
        }
        return null;
    }

    public static void main(String[] args) {
        BookList booklist = new BookList();
        //user指向哪个对象 就看返回值
        User user = Login();
        while(true) {
            int choice = user.menu();
            //System.out.println("choice:" + choice);

            //根据choice来决定调用哪个方法
            user.doOperation(choice, booklist);
        }
    }
}

user package

Mainly includes user and related objects

parent class User

Contains basic attributes: name, menu (menu) method declaration, doOperation (execute method operation) method declaration.

import book.BookList;
import operation.IOPeration;

public abstract class User {
    protected String name;

    protected IOPeration[] ioperations;
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    public void doOperation(int choice, BookList booklist) {
        ioperations[choice].work(booklist);bao
    }
}

Subclass AdminUser

Contains an array of methods used by the admin and the admin menu.

import operation.*;

import java.util.Scanner;

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

    //管理员专用菜单
    public int menu() {
        System.out.println("*************管理员界面*************");
        System.out.println("hello " + this.name +"欢迎来到管理员菜单!");
        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 sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

Subclass NormalUser

Contains an array of common user methods and a common user menu.

package user;
import operation.*;

import java.util.Scanner;

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.ioperations = new IOPeration[]{new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperatoin(),
        };
    }

    //普通用户用菜单
    public int menu() {
        System.out.println("*************普通用户界面*************");
        System.out.println("hello " + this.name +"欢迎来到普通用户菜单!");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("************************************");
        System.out.println("请输入你的操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

Operation package

Mainly includes various operation methods

IOPeration interface

Subsequent use all methods to inherit this interface and perform related work on the bookList object.

package operation;

import book.BookList;

public interface IOPeration {
    void work(BookList booklist);
}

AddOperation Method

How to add books

package operation;

import book.BookList;
import book.Book;

import java.util.Scanner;

public class AddOperation implements IOPeration{
    @Override
    public void work(BookList booklist) {
        System.out.println("增加书籍");
        Scanner sc = new Scanner(System.in);
        int currentSize = booklist.getUseSize();
        //用户输入书籍信息
        System.out.println("请输入书籍的名称:");
        String name =sc.nextLine();
        System.out.println("请输入书籍的作者:");
        String author = sc.nextLine();
        System.out.println("请输入书籍的类型:");
        String type = sc.nextLine();
        //注意:这里将回车放到最后,如果放在前面,下一个nextLine()会读入回车
        System.out.println("请输入书籍的价格:");
        int price = sc.nextInt();

        //创建一个新的book对象导入刚才的信息
        Book book = new Book(name, author, price, type);
        //检查书架当中是否有这本书
        for (int i = 0; i < currentSize; i++) {
            Book book1 = booklist.getBook(i);
            //判断书架中的书与新导入书的引用是否相等
            if(book1 == book) {
                System.out.println("书架上有这本书,添加失败");
                return;
            }
        }
        //判断添入书是否超出书架最大容量,未超出则添入书籍
        if(currentSize == booklist.getBooks().length) {
            System.out.println("书架已满,添加失败");
        } else {
            //在书架中添入书籍
            booklist.setBooks(currentSize, book);
            //存放书的数量+1
            booklist.setUseSize(currentSize + 1);
            System.out.println("添加成功");
        }
    }

}

FindOperation method

how to find books

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOPeration{
    @Override
    public void work(BookList booklist) {
        System.out.println("寻找书籍");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要寻找的书籍");
        String name = sc.nextLine();
        int currentSize = booklist.getUseSize();
        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;
            }
        }
        System.out.println("没有找到这本书");
    }
}

DelOperation method

The method used to delete the book

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        //1.找到有没有这本书
        System.out.println("请输入要删除书的名字");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                //2.如果有,从这本书后面开始,从前往后向前覆盖
                for (; i < currentSize; i++) {
                    Book book1 = bookList.getBook(i + 1);
                    bookList.setBooks(i, book1);
                }
                //3.将最后一本书置为空
                bookList.setBooks(currentSize - 1, null);
                //数组最大容量减少
                bookList.setUseSize(currentSize - 1);
                System.out.println("删除成功!");
                return;
            }
        }
        //未删除成功的情况
        System.out.println("该书不存在,删除失败");
        return;
    }
}

ShowOperation method

How to display all books

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        //1.找到有没有这本书
        System.out.println("请输入要删除书的名字");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = bookList.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)) {
                //2.如果有,从这本书后面开始,从前往后向前覆盖
                for (; i < currentSize; i++) {
                    Book book1 = bookList.getBook(i + 1);
                    bookList.setBooks(i, book1);
                }
                //3.将最后一本书置为空
                bookList.setBooks(currentSize - 1, null);
                //数组最大容量减少
                bookList.setUseSize(currentSize - 1);
                System.out.println("删除成功!");
                return;
            }
        }
        //未删除成功的情况
        System.out.println("该书不存在,删除失败");
        return;
    }
}

BorrowOperation Method

How to Borrow Books

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOPeration{
    @Override
    public void work(BookList booklist) {
        System.out.println("借出书籍");
        /*
        * 1.你要借阅什么书
        * 2.你要借阅的书存不存在
        * 3.如何完成借阅过程 isBorrowed->true 已借出 isBorrowed->false 未借出
        * */
        System.out.println("请输入你要借阅的图书名字");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = booklist.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            //该图书存在的情况
            if(book.getName().equals(name) && book.isBorrowed() == false) {
                book.setBorrowed(true);
                System.out.println("借出成功");
                System.out.println(book);
                return;
            }
        }

        //未被借出的情况
        System.out.println("该图书已被借出或者不存在,借阅失败");
        return;
    }
}

Return Operation method

How to Return Books

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnOperatoin implements IOPeration{
    @Override
    public void work(BookList booklist) {
        System.out.println("归还书籍");
        System.out.println("请输入你要归还的图书");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = booklist.getUseSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            //该图书存在的情况
            if(book.getName().equals(name) && book.isBorrowed() == true) {
                book.setBorrowed(false);
                System.out.println("归还成功");
                System.out.println(book);
                return;
            }
        }

        //未被借出的情况
        System.out.println("该图书已被归还或者不存在,归还失败");
        return;
    }
}

ExitOperation method

The method used to log out of the system.

package operation;

import book.BookList;

public class ExitOperation implements IOPeration{
    @Override
    public void work(BookList booklist) {
        System.out.println("退出系统");
        //退出系统指令
        System.exit(0);
    }
}

Operation example

Here is some operation logic of the administrator interface:

 

 

Replenish

Disadvantages: no persistent storage. Can be upgraded later: store data in a database or folder

           Currently only arrays are used. Later, it can be made into a web page interaction.

Well, that's all for the library management system, goodbye everyone!

Guess you like

Origin blog.csdn.net/asdssadddd/article/details/132483202