Java实现图书管理系统

图书管理系统由3部分组成,分别是book,user,opera
每个package(包)又包含了若干类,接口等

🤡book

在这里插入图片描述

✏Book

代码展示

package book;

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

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
        //因为boolean类型默认值为false,也就是未被借出,所以可以不写
    }

    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 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 + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                //", isBorrowed=" + isBorrowed +
                (isBorrowed()? ", 已被借出" : ", 未被借出") +
                '}';
    }
}


✏BookList

代码展示

package book;

public class BookList {
    public static final int DEFAULT_SIZE = 10;
    private Book[] books = new Book[DEFAULT_SIZE];

    public BookList() {
        books[0] = new Book("三体","刘慈欣",89,"科幻");
        books[1] = new Book("当下的力量","哈特·托利",32,"心灵");
        books[2] = new Book("乔布斯传","沃尔特·艾萨克森",68,"传记");
        this.usedSize = 3;//存放了3本书
    }

    private int usedSize;//记录当前books数组有多少本书

    public Book getBook(int pos) {
        return this.books[pos];//数组中的第几本书
    }

    public void setBook(int pos,Book book) {
         this.books[pos] = book;
    }

    public int getUsedSize() {
        return usedSize;
    }

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

🤡opera

在这里插入图片描述

✏AddOperation

代码展示

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("添加图书📖");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入需要添加的图书名称📕:");
        String bookName = scanner.nextLine();

        System.out.println("请输入需要添加的图书作者📕:");
        String authorName = scanner.nextLine();

        System.out.println("请输入需要添加的图书类型📕:");
        String type = scanner.nextLine();

        System.out.println("请输入需要添加的图书价格📕:");
        int pirce = scanner.nextInt();

        Book book = new Book(bookName,authorName,pirce,type);

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book find_book = bookList.getBook(i);
            if(find_book.getName().equals(bookName)) {//判断要添加的书是否已经存在
                System.out.println("该书已存在,无法继续存放");
                return;
            }
        }

        bookList.setBook(currentSize,book);
        System.out.println("❀添加完成❀");
        bookList.setUsedSize(currentSize+1);//添加完成后usedSize值需要+1
    }
}


✏BorrowOperation

代码展示

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class BorrowOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书📖");
        System.out.println("请输入想要借阅的书名📕:");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookName) && !book.isBorrowed()) {//要借阅的书存在且未被借出
                System.out.println("借阅成功");
                book.setBorrowed(true);
                return;
            }
        }
        System.out.println("借阅失败");
    }
}


✏DelOperation

代码展示

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书📖");
        System.out.println("请输入需要删除的书名📕:");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();

        int index = -1;
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookName)) {//判断要删除的书是否存在
                index = i;
                break;
            }
        }

        if(-1 == index) {
            System.out.println("该书不存在");
        }else {
            for (int j = index; j < currentSize-1; j++) {
                Book book = bookList.getBook(j+1);//取到后一位置的书
                bookList.setBook(j,book);//将后一位置的书的信息覆盖前一位置
            }

            bookList.setUsedSize(currentSize - 1);
            bookList.setBook(currentSize - 1,null);
            System.out.println("❀删除成功❀");
        }
    }
}


✏ExitOperation

代码展示

package opera;

import book.BookList;

public class ExitOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("退出图书操作系统📖");
        System.exit(0);//退出
    }
}

✏FindOperation

代码展示

package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class FindOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书📖");
        System.out.println("请输入要查找的书名📕:");
        Scanner scanner = new Scanner(System.in);
        String bookName = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookName)) {
                System.out.println("❀找到了❀");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没找到,该书不存在");
    }
}


✒IOperation(接口)

代码展示

package opera;

import book.BookList;

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

✏ReturnOperation

代码展示

package opera;

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 bookName = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookName) && book.isBorrowed()) {//要归还的书存在且已被借出
                System.out.println("归还成功");
                book.setBorrowed(false);
                return;
            }
        }
    }
}

✏ShowOperation

代码展示

package opera;

import book.Book;
import book.BookList;

public class ShowOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("展示图书📖");
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {//遍历数组
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

🤡user

在这里插入图片描述

✏User

代码展示

package user;

import book.BookList;
import opera.IOperation;

public abstract class User {
    protected String name;

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

    public abstract int menu();

    protected IOperation[] iOperations;

    public abstract void doWork(int choice,BookList bookList);

}


✏AdminUser

代码展示

package user;

import book.BookList;
import opera.*;

import java.util.Scanner;

public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
        super.iOperations = new IOperation[] {//将AdminUser对应的操作存放到数组中
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelOperation(),
                new ShowOperation()
        };
    }

    @Override
    public void doWork(int choice,BookList bookList) {
        this.iOperations[choice].work(bookList);//调用数组对应的操作完成对应的工作
    }

    public int menu() {
        System.out.println("hello, " + name + " 📖欢迎来到图书管理系统📖");
        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;
    }
}


✏NormalUser

代码展示

package user;

import book.BookList;
import opera.*;

import java.util.Scanner;

public class NormalUser extends User {

    public NormalUser(String name) {
        super(name);
        super.iOperations = new IOperation[] {//将NormalUser对应的操作存放到数组中
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    @Override
    public void doWork(int choice, BookList bookList) {
        this.iOperations[choice].work(bookList);//调用数组对应的操作完成对应的工作
    }

    public int menu() {
        System.out.println("hello, " + name + " 📖欢迎来到图书管理系统📖");
        System.out.println("----------------------------------");
        System.out.println("1.查找图书📖");
        System.out.println("2.借阅图书📖");
        System.out.println("3.归还图书📖");
        System.out.println("0.退出图书操作系统📖");
        System.out.println("----------------------------------");
        System.out.println("请输入你的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}


🔎Main(类)

代码展示

import book.BookList;
import user.AdminUser;
import user.NormalUser;
import user.User;

import java.util.Scanner;

public class Main {
    public static User login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的ID:");
        String name = scanner.nextLine();
        System.out.println("请输入你的身份:1--管理员 0--用户");
        int choice = scanner.nextInt();
        if(1 == choice) {
            return new AdminUser(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.doWork(choice,bookList);
        }
    }
}


🔎难点分析

AddOperation
遍历数组,查看要添加的图书是否存在
利用bookList.setBook(currentSize,book)在数组第一个空缺位置添加图书
bookList.setUsedSize(currentSize+1),将usedSize值+1


如何根据返回的choice来调用正确的操作
利用IOperation[] iOperations数组将不同身份的操作分别保存
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


DelOperation
判断要删除的图书是否存在
不存在,无法删除
存在,后一位置图书信息覆盖前一位置图书信息,将最后的图书置为null
在这里插入图片描述

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值