12. 图书管理系统

本篇将会作为前几篇的一个回顾与复习,如果能够一自己的理解完成会对Java语法有更深层次的理解,话不多说,上代码!
在这里插入图片描述
这些是我创建的类和包供大家参考

book包

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;
    }

    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 ==true)?"已经借出":"未借出")+
                '}';
    }
}

BookList类

/**
 * 书架
 */
public class BookList {
    // 最多可以放10本书
    private Book[] books = new Book[10];
    private int usedSize;// 实时记录当前books这个数组放了几本书
    public BookList() {
        books[0] = new Book("三国演义","罗贯中",19,"小说");
        books[1] = new Book("西游记","吴承恩",29,"小说");
        books[2] = new Book("红楼梦","曹雪芹",9,"小说");
        usedSize = 3;
    }

    /**
     * 此时 pos 位置一定是合法的
     * @param pos
     * @return  当前下标的书
     */
    public Book getBook(int pos) {
        return books[pos];
    }

    /**
     *
     * @param pos  此时pos位置一定是合法的
     * @param book  book是你要放得书
     */
    public void  setBooks(int pos,Book book) {
        books[pos] = book;
    }

    /**
     * 实时获取当前书的个数
     * @return
     */
    public int getUsedSize() {
        return usedSize;
    }

    /**
     * 实时的修改当前书架上的书的个数
     */
    public void setUsedSize(int size) {
        usedSize = size;
    }
}

operation包

AddOperation类

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书");

        Scanner scanner = new Scanner(System.in);

        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,price,type);
        int currentSize = bookList.getUsedSize();
        bookList.setBooks(currentSize,book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增书籍成功!");
    }
}

BorrowOperation类

public class BorrowOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入借阅图书的名字:> ");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                book.setBorrowed(true);
                System.out.println("借阅成功!");
                return;
            }
        }
    }
}

DelOperation类

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你要删除图书的名字:> ");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                index = i;
                break;
            }
        }

        //1. i >= currentSize 没有这本书
        //2. break; 说明有这本书 把这本书的下标存储在了index
        for (int i = 0; i < currentSize - 1; i++) {
//            bookList[i] = bookList[i+1];
            Book book = bookList.getBook(i+1);
            bookList.setBooks(i,book);
        }
        bookList.setBooks(currentSize-1,null);
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功!");
    }
}

DisplayOperation类

public class DisplayOperation 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);

            System.out.println(bookList.getBook(i));
            }
        }
}

ExitOperation类

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

FindOperation类

public class FindOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查找图书");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书的名字:> ");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("找到了这本书!");
                System.out.println(book);
                return;
            }
        }
        //说明没找到
        System.out.println("没有你要找的这本书!");
    }
}

IOperation接口

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

ReturnOperation类

public class ReturnOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入归还图书的名字:> ");
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();

        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if (book.getName().equals(name)){
                book.setBorrowed(false);
                System.out.println("归还成功!");
                return;
            }
        }
    }
}

user包

AdminUser类

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
          new ExitOperation(),
          new FindOperation(),
          new AddOperation(),
          new DelOperation(),
          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;
    }

}

NormalUser类

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                //为什么能实现这些类?
                //因为他们都使用了 IOperations 接口
                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;
    }
}

User类

public abstract class User {
    protected String name;// 用户名
    protected IOperation[] iOperations;//此时并没有初始化和分配大小

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

    public abstract int menu();

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

Main类

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 引用的哪个对象, 需要由login的返回值决定
        User user = login();// 发生了向上转型

        while (true) {
            int choice = user.menu();// 发生了动态绑定
            user.doOperation(choice,bookList);

        }

    }
}

今天的分享就到此结束啦~~

2022年10月8日20:42:37

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱听歌的阿漓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值