图书管理系统代码(Java)

1、运行演示

QQ2024528-205028-HD

详细讲解在这篇博客:JavaSE:图书管理系统-CSDN博客

2、所建的包

3、Java代码

3.1 book包

3.1.1 Book类代码

java 复制代码
package book;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:16
 */
public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean isBorrowed;
    public Book(String name, String author, String type, int price) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    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 boolean getBorrowed() {
        return isBorrowed;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +", 状态:"+
                (isBorrowed ? " 该书已被借出" : "该书未被借出")+
                '}';
    }
}

3.1.2 BookList类代码

java 复制代码
package book;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:19
 */
public class BookList {
    private Book[] books = new Book[10];
    private int bookNumber;

    public BookList() {
        books[0] = new Book("哈利波特", "J.K.罗琳", "小说",30);
        books[1] = new Book("三国演义", "罗贯中","小说", 35);
        books[2] = new Book("红楼梦", "曹雪芹", "小说",25);
        books[3] = new Book("鲁宾逊漂流记", "丹尼尔·笛福", "小说",40);
        this.bookNumber = 4;
    }

    public int getBookNumber() {
        return bookNumber;
    }

    public void setBookNumber(int bookNumber) {
        this.bookNumber = bookNumber;
    }

    public Book getBook(int index) {
        Book book = books[index];
        return book;
    }

    public void setBooks(Book book,int index) {
        books[index] = book;
    }
}

3.2 operation包

3.2.1 AddBook类代码

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:33
 */
public class AddBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        int currentNum = bookList.getBookNumber();
        Scanner scanner = new Scanner(System.in);
        System.out.println("*************Add Book*************");

        System.out.println("请输入您所想增添图书的书名:");
        String name = scanner.nextLine();

        System.out.println("请输入您所想增添图书的作者:");
        String author = scanner.nextLine();

        System.out.println("请输入您所想增添图书的类型:");
        String type = scanner.nextLine();

        System.out.println("请输入您所想增添图书的价格:");
        int price = scanner.nextInt();

        Book book = new Book(name,author,type,price);
        bookList.setBooks(book,currentNum);
        bookList.setBookNumber(currentNum+1);
    }
}

3.2.2 BorrowBook类代码

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:35
 */
public class BorrowBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Borrow Book*************");
        System.out.println("请输入您所要借阅图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                if(book.getBorrowed() == true) {
                    System.out.println("很抱歉!!!该书籍已被其他人借阅!!!");
                    return;
                }else {
                    book.setBorrowed(true);
                    System.out.println("借阅成功!");
                    return;
                }
            }
        }
        System.out.println("没有查找到该书籍!!!");
    }
}

3.2.3 DeleteBook类代码

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:34
 */
public class DeleteBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Borrow Book*************");
        System.out.println("请输入您所要删除图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        int index = -1;
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                index = i;
                break;
            }
        }
        if(index == -1) {
            System.out.println("没有查找到该图书!!!");
        }else {
            for (int i = index; i < currentNum-1; i++) {
                Book book = bookList.getBook(i+1);
                bookList.setBooks(book,i);
            }
            bookList.setBooks(null,currentNum);
            bookList.setBookNumber(currentNum-1);
            System.out.println("成功删除该图书!!!");
        }
    }
}

3.2.4 ExitSystem类代码

java 复制代码
package operation;

import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 16:22
 */
public class ExitSystem implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统!图书管理系统随时欢迎您的下次到来!");
        System.exit(0);
    }
}

3.2.5 FindBook类代码

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:34
 */
public class FindBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Find Book*************");
        System.out.println("请输入您所要查找图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                System.out.println("查找到该书!该书相关信息如下:");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有查找到该书籍!!!");
    }
}

3.2.6 IOperation接口代码

java 复制代码
package operation;

import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:38
 */
public interface IOperation {
    void work(BookList bookList);
}

3.2.7 ReturnBook类代码

java 复制代码
package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:35
 */
public class ReturnBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("*************Return Book*************");
        System.out.println("请输入您所要归还图书的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentNum = bookList.getBookNumber();
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            if(name.equals(book.getName())) {
                if(book.getBorrowed()) {
                    book.setBorrowed(false);
                    System.out.println("图书归还成功!!!欢迎您的下次光临!!!");
                    return;
                }else {
                    System.out.println("查找到该书,但该书未被借出!!!");
                    return;
                }
            }
        }
        System.out.println("没有查找到该书籍!!!");
    }
}

3.2.8 ShowBook类代码

java 复制代码
package operation;

import book.Book;
import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 17:21
 */
public class ShowBook implements IOperation{
    @Override
    public void work(BookList bookList) {
        int currentNum = bookList.getBookNumber();
        System.out.println("***************展示书籍***************");
        for (int i = 0; i < currentNum; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
        System.out.println("***************展示书籍***************");
    }
}

3.3 user包

3.3.1 User类代码

java 复制代码
package user;

import book.BookList;
import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:30
 */
public abstract class User {
    protected String name;

    IOperation[] iOperations;

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

    public void youOperate(int choice,BookList bookList) {
        this.iOperations[choice].work(bookList);
    }
}

3.3.2 Administrator类代码

java 复制代码
package user;

import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:31
 */
public class Administrator extends User {
    public Administrator(String name) {
        super(name);
        iOperations = new IOperation[]{
                new ExitSystem(),
                new AddBook(),
                new FindBook(),
                new ShowBook(),
                new DeleteBook()
        };
    }

    @Override
    public int menu() {
        System.out.println("请输入您的操作:");
        System.out.println("    1.添加书籍 2.查找书籍");
        System.out.println("3.展示书籍 4.删除书籍 0.退出系统");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

3.3.3 NormalUser类代码

java 复制代码
package user;

import operation.*;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-13
 * Time: 13:30
 */
public class NormalUser extends User {
    public NormalUser(String name) {
        super(name);
        iOperations = new IOperation[]{
                new ExitSystem(),
                new FindBook(),
                new BorrowBook(),
                new ReturnBook(),
                new ShowBook()
        };
    }

    @Override
    public int menu() {
        System.out.println("请输入您的操作:");
        System.out.println("    1.查找书籍 2.借阅书籍");
        System.out.println("3.归还书籍 4.展示书籍 0.退出系统");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}

3.4 默认包

3.4.1 Main类代码

java 复制代码
import book.BookList;
import user.Administrator;
import user.NormalUser;
import user.User;

import java.util.Random;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:图书管理系统
 * User: dings
 * Date: 2024-05-13
 * Time: 13:18
 */
public class Main {
    public static User login() {
        System.out.println("请输入您的姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("Hello! "+name+"! "+"欢迎来到图书系统!");
        System.out.println("请选择您的身份:1->图书管理员  2->普通用户");
        int choice = scanner.nextInt();
        if(choice == 1) {
            return new Administrator(name);
        }else {
            return new NormalUser(name);
        }
    }
    public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while (true) {
            int choice = user.menu();
            user.youOperate(choice,bookList);
        }
    }
}
相关推荐
Am心若依旧409几秒前
[c++11(二)]Lambda表达式和Function包装器及bind函数
开发语言·c++
明月看潮生3 分钟前
青少年编程与数学 02-004 Go语言Web编程 20课题、单元测试
开发语言·青少年编程·单元测试·编程与数学·goweb
Yan.love9 分钟前
开发场景中Java 集合的最佳选择
java·数据结构·链表
椰椰椰耶12 分钟前
【文档搜索引擎】搜索模块的完整实现
java·搜索引擎
大G哥12 分钟前
java提高正则处理效率
java·开发语言
VBA633723 分钟前
VBA技术资料MF243:利用第三方软件复制PDF数据到EXCEL
开发语言
轩辰~25 分钟前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
小_太_阳34 分钟前
Scala_【1】概述
开发语言·后端·scala·intellij-idea
向宇it34 分钟前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
智慧老师43 分钟前
Spring基础分析13-Spring Security框架
java·后端·spring