Java实现图书馆系统

该博客介绍了一个简单的图书馆管理系统,包括Book类、BookList类、IOperation接口和User类。Book类定义了书籍的基本属性,BookList类用于存储和管理书籍。User类抽象出管理员和普通用户的角色,分别有不同的操作权限。NormalUser和AdminUser类实现了不同的菜单选项,允许用户进行查找、借阅、归还等操作。通过main方法展示了系统运行流程。
摘要由CSDN通过智能技术生成

首先让我们看一下这个代码的功能!

我们可以清楚的发现这个代码 有两个角色 管理员 普通用户 而且他们所能 实现的能力不同!

那我们实现分为几个part 书 方法 用户 整合(main)

 

public class Book {
    private String name;
    private String author;
    private int price;
    private String type;
    private boolean borrow;

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

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

    public Boolean getBorrow() {
        return borrow;
    }

    public void setBorrow(boolean borrow) {
        this.borrow = borrow;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +","+
                ((borrow == true)?" 已经借出":" 未被借出") +
                '}';
    }
}
public class BookList {

    private Book[] books = new Book[10];
    private int usedSize;

    public BookList(){
        books[0] = new Book("微积分","小明",49,"学习");
        books[1] = new Book("线性代数","小张",79,"学习");
        books[2] = new Book("离散数学","小芳",66,"学习");
        usedSize = 3;
    }


    //位置的书
    public Book getBook(int pos){
        return books[pos];
    }
    //你要放的书
    public void setBook(int pos,Book book){
        books[pos] = book;
    }

    //多少书
    public int getUsedSize() {
        return usedSize;
    }

    //修改书的个数
    public void setUsedSize(int size) {
        usedSize = size;
    }
}
public interface IOperation {
    void work(BookList bookList);
}
public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书!");
        System.out.println("请输入你想查找的书名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int current = bookList.getUsedSize();
        for (int i = 0; i < current; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("找到这本书了!");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有你要找的这本书");
    }
}
public abstract class User {
    protected String name;
    protected IOperation[] iOperations;

    public User(String name) {
        this.name = name;
    }
    abstract int menu();
    public void doOperation(int choice, BookList bookList){
        this.iOperations[choice].work(bookList);
    }
}
public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    public int menu(){
        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("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}
public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitOperation(),
                new FindOperation(),
                new AddOperation(),
                new DelectOperation(),
                new DisplayOperation()
        };
    }

    public int menu(){
        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("请输入你的操作:");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        return choice;
    }
}
public class Main {
        public static User login(){
            System.out.println("请输入姓名:");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            System.out.println("请输入你的身份:1-》管理员.0-》普通用户");
            int choice = scanner.nextInt();

            if(choice == 1){
                return new AdminUser(name);
            }else{
                return new NormalUser(name);
            }
        }

        public static void main(String[] args) {
            BookList bookList = new BookList();
            User user = login();
            //User user == new AdminUser(name);1
            //User user == new NormalUser(name);0
            while(true){
                 int choice = user.menu();
                 user.doOperation(choice,bookList);
            }
        }

}

总的来说 应该都能看懂 但有一个part 我觉得可以说说

public static void main(String[] args) {
    BookList bookList = new BookList();
    User user = login();

    while(true){
        //根据身份打印菜单选择方法
        int choice = user.menu();

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

上图片!直观一点

红红火火恍恍惚惚哈哈哈哈哈 520 我突然发现了图书馆的更有趣的方式

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值