图书管理系统代码(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);
        }
    }
}
相关推荐
The Future is mine30 分钟前
Python计算经纬度两点之间距离
开发语言·python
Enti7c31 分钟前
HTML5和CSS3的一些特性
开发语言·css3
腥臭腐朽的日子熠熠生辉37 分钟前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
爱吃巧克力的程序媛38 分钟前
在 Qt 创建项目时,Qt Quick Application (Compat) 和 Qt Quick Application
开发语言·qt
ejinxian39 分钟前
Spring AI Alibaba 快速开发生成式 Java AI 应用
java·人工智能·spring
杉之44 分钟前
SpringBlade 数据库字段的自动填充
java·笔记·学习·spring·tomcat
圈圈编码1 小时前
Spring Task 定时任务
java·前端·spring
俏布斯1 小时前
算法日常记录
java·算法·leetcode
独好紫罗兰1 小时前
洛谷题单3-P5719 【深基4.例3】分类平均-python-流程图重构
开发语言·python·算法
27669582921 小时前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿