java----图书管理系统 (用户和管理员)

1. 描述

a> 角色

图书管理系统有两个角色对书进行操作, 分别是普通用户类和管理员类.

b> 操作

操作可划分为: 增添图书, 删除图书, 更新图书, 查找图书, 借阅图书, 归还图书, 退出系统

2. 实现

a> Book 类

package java0126;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:37
 */
public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    // 表示没有被借
    private boolean isBorrowed = false;

    public Book(String name, int price, String author, String type) {
        this.name = name;
        this.price = price;
        this.author = author;
        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 String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isBorrowed() {
        return isBorrowed;
    }

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

    public void bookImformation() {
        System.out.println(this.name + " " + this.price + " "
                + this.author + " " + this.type + " " + this.isBorrowed);
    }
}

b> BookList 类

package java0126;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:37
 */
public class BookList {
    private Book[] books = new Book[100] ;
    private int size;
    // 图书管理系统的关闭和开启条件
    private boolean isOpen = true;

    public BookList() {
        this.books[this.size++] = new Book("三国演义",100, "罗贯中","历史小说");
    }

    public void addBook(String name, int price, String author, String type) {
        this.books[size++] = new Book(name, price, author, type);
    }
    public Book getBook(int index) {
        if (!checkIndex(index)) {
            return null;
        }
        return this.books[index];
    }
    public void setSize(int size) {
        this.size = size;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        this.isOpen = open;
    }

    public int getSize() {
        return this.size;
    }
    public void borrowBook(int index) {
        if (!checkIndex(index)) {
            return;
        }
        if (!this.books[index].isBorrowed()) {
            this.books[index].setBorrowed(true);
        }
    }
    public void displayBook() {
        if (this.books == null) {
            System.out.println("没有书籍");
            return;
        }
        System.out.println("  书名  " + " 价格 " + " 作者 " + "  类型  " + " 是否被借");
        for (int i = 0; i < this.size; i++) {
            this.books[i].bookImformation();
        }
    }

    public void deleteBook(int index) {
        if (!checkIndex(index)) {
            return;
        }
        for (int i = index; i < this.size - 1; i++) {
            this.books[i] = this.books[i + 1];
        }
        this.size--;
    }

    public void updateBook(int index) {
        if (!checkIndex(index)) {
            return;
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的内容:");
        System.out.println("1. 书名");
        System.out.println("2. 作者");
        System.out.println("3. 类型");
        System.out.println("4. 价格");
        System.out.println("5. 书的状态");

        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                System.out.println("请输入修改后的书名: ");
                String name = scanner.next();
                this.books[index].setName(name);
                break;
            case 2:
                System.out.println("请输入修改后的作者: ");
                String author = scanner.next();
                this.books[index].setAuthor(author);
                break;
            case 3:
                System.out.println("请输入修改后的类型: ");
                String type = scanner.next();
                this.books[index].setType(type);
                break;
            case 4:
                System.out.println("请输入修改后的价格: ");
                int price = scanner.nextInt();
                this.books[index].setPrice(price);
                break;
            case 5:
                System.out.println("请输入修改后的书的状态(是否被借): ");
                boolean isBorrowed = scanner.hasNextBoolean();
                this.books[index].setBorrowed(isBorrowed);
                break;
        }
    }

    public boolean checkIndex(int index) {
        if (index < 0 && index >= this.size) {
            System.out.println("输入有误, 您输入的下标不存在!");
            return false;
        }
        return true;
    }
}

c> IOperation 操作接口

package java0126.Operation;

import java0126.BookList;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:46
 */
public interface IOperation {
    public void work(BookList bookList);
}

d> AddBook 类

package java0126.Operation;

import java0126.BookList;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:42
 */
public class AddBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书的名字:");
        String name = scanner.next();
        System.out.println("请输入书的价格:");
        int price = scanner.nextInt();
        System.out.println("请输入书的类型:");
        String type = scanner.next();
        System.out.println("请输入书的作者:");
        String author = scanner.next();
        bookList.addBook(name, price, author, type);
    }
}

e> DeleteBook 类

package java0126.Operation;

import java0126.BookList;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:43
 */
public class DeleteBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书的下标");
        int index = scanner.nextInt();
        bookList.deleteBook(index);
    }
}

f> UpdateBook 类

package java0126.Operation;


import java0126.BookList;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:43
 */
public class UpdateBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("更新书籍信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要更新的书的下标:");
        int index = scanner.nextInt();
        bookList.updateBook(index);
    }
}

g> FindBook 类

package java0126.Operation;

import java0126.Book;
import java0126.BookList;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:43
 */
public class FindBook implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("查找书籍");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要找的输的下标");
        int index = scanner.nextInt();
        Book book = bookList.getBook(index);
        book.bookImformation();
    }
}

h> BorrowBook 类

package java0126.Operation;

import java0126.BookList;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 15:48
 */
public class BorrowBook implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("借书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要借的书的下标: ");
        int index = scanner.nextInt();
        // 该书存在 并且 书的状态是 false 未被借
        if (bookList.checkIndex(index) && !bookList.getBook(index).isBorrowed()) {
            // 借书, 将书的状态改为 true 表示已被借阅
            bookList.getBook(index).setBorrowed(true);
        }
    }
}

i> ReturnBook 类

package java0126.Operation;

import java0126.BookList;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 21:45
 */
public class ReturnBook implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("还书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要借的书的下标: ");
        int index = scanner.nextInt();
        if (bookList.checkIndex(index) && bookList.getBook(index).isBorrowed()) {
            bookList.getBook(index).setBorrowed(false);
        }
    }
}

j> DisplayBook 类

package java0126.Operation;

import java0126.BookList;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 14:12
 */
public class DisplayBook implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("显示书籍");
        bookList.displayBook();
    }
}

k> Exit 类

package java0126.Operation;

import java0126.BookList;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 14:19
 */
public class Exit implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("退出");
        bookList.setOpen(false);
    }
}

l> User 类

package java0126;

import java0126.Operation.IOperation;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:51
 */
public abstract class User {
    protected String name;
    protected IOperation[] userOperation;
    public abstract int menu();
    public void doOperation(int choice, BookList bookList) {
        this.userOperation[choice - 1].work(bookList);
    }
}

m> Admin 类

package java0126;

import java0126.Operation.*;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:51
 */
public class Admin extends User {
    public Admin(String name) {
        this.name = name;
        userOperation = new IOperation[]{
                new FindBook(),
                new AddBook(),
                new DeleteBook(),
                new UpdateBook(),
                new DisplayBook(),
                new Exit()
        };
    }

    @Override
    public int menu() {
        System.out.println("=====================");
        System.out.println("1.查找书籍");
        System.out.println("2.添加书籍");
        System.out.println("3.删除书籍");
        System.out.println("4.更新书籍信息");
        System.out.println("5.显示书籍信息");
        System.out.println("6.退出");
        System.out.println("=====================");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你的选择的操作: ");
        int choice = scanner.nextInt();
        return choice;
    }
}

n> NormalUser 类

package java0126;

import java0126.Operation.*;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:51
 */
public class NormalUser extends User{
    public NormalUser(String name) {
        this.name = name;
        userOperation = new IOperation[]{
                new FindBook(),
                new BorrowBook(),
                new ReturnBook(),
                new DisplayBook(),
                new Exit()
        };
    }

    @Override
    public int menu() {
        System.out.println("=====================");
        System.out.println("1.查找书籍");
        System.out.println("2.借阅书籍");
        System.out.println("3.归还书籍");
        System.out.println("4.显示书籍信息");
        System.out.println("5.退出");
        System.out.println("=====================");
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你的选择的操作: ");
        int choice = scanner.nextInt();
        return choice;
    }
}

o> Main 类

package java0126;

import java.util.Scanner;

/**
 * @author FMM
 * @version 7.0
 * @date 2021/1/26 12:49
 */
public class Main {
    public static void main(String[] args) {
//        1. 初始化书籍
        BookList bookList = new BookList();
//        2. 登录
        User user = login();
//        3. 执行相关操作
        while (bookList.isOpen() == true) {
            int choice = user.menu();
            user.doOperation(choice, bookList);
        }
    }


    public static User login() {
        System.out.println("0.管理员");
        System.out.println("1.用户");
        System.out.print("请输入您的选择:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        System.out.print("请输入用户名:");
        String name = scanner.next();
        if (choice == 1) {
            return new NormalUser(name);
        }
        return new Admin(name);
    }
}

3. 测试结果

a> 管理员

管理角色测试结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

b> 普通用户

用户角色测试结果
在这里插入图片描述

在这里插入图片描述

  • 12
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值