图书管理系统

一、找对象

book包:Book类,BookList类

operatio包:Add,Borrow,Del,Display,Exit,Find,Return等操作

user包:AdminUser,NormalUser,User

main函数

二、搭建框架

book包:

Book类

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

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

    public Book(String name, String author, int price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }
}

BookList类

public class BookList {
    public Book[] bookList=new Book[10];
    private int usedSize;//当前书架有几本书
    public BookList(){
        bookList[0]=new Book("三国演义","罗贯中",18,"小说");
        bookList[1]=new Book("西游记","吴承恩",48,"小说");
        bookList[2]=new Book("水浒传","施耐庵",28,"小说");

        this.usedSize=3;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }
    //把书放到pos位置
    public void setBookList(int pos,Book book){
        bookList[pos]=book;
    }
    //获取pos位置的书
    public Book getBook(int pos){
        return bookList[pos];
    }

    //这里本质上可以写借阅书籍等操作
}

操作包:

Add

public class AddOperation{
    public void work{
        System.out.println("新增图书!");
        //...
    }
}

Borrow

public class BorrowOperation{
    public void work{
        System.out.println("借阅图书!");
        //...
    }
}

Del

public class DelOperation{
    public void work{
        System.out.println("删除图书!");
        //...
    }
}

Display

public class DisplayOperation{
    public void work{
        System.out.println("展示图书!");
        //...
    }
}

Exit

public class ExitOperation{
    public void work{
        System.out.println("退出系统!");
        //...
    }
}

Find

public class FindOperation{
    public void work{
        System.out.println("查找图书!");
        //...
    }
}

Return

public class ReturnOperation{
    public void work{
        System.out.println("归还图书!");
        //...
    }
}

根据上述代码,可以观察类下都有同一个work方法,所以可以定义一个接口IOperatin来将所有操作类串在一起

接口IOperatin

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

所有操作类在类名后面加implements IOperation,重写work方法

以Add为例

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

user包:

管理员和普通用户都是使用者,所以都继承User类。

User

public class User {
    protected String name;

    protected IOperation[] iOperations;

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

NormalUser

public class NormalUser extends User{
    public NormalUser(String name) {
        super(name);
        };
    }
}

AdminUser

public class AdminUser extends User{
    public AdminUser(String name) {
        super(name);
        };
    }
}

三、构造main函数,填充对象,建立连接

流程:

登入->选择身份->根据所选身份分配相应菜单->选择操作->操作

  1. 登入模块

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);
    }
}
  1. 主函数

public static void main(String[] args) {
    BookList bookList=new BookList();
    User user=login();
    while (true){
        int choice=user.menu();
        user.doOperation(choice,bookList);
    }
}
  1. 菜单模块写入User类

用户根据选择的操作要在菜单里筛选相匹配的操作类然后调用,所以建立一个操作类的数组,将操作选项放在数组里

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

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("==========管理员菜单==========");
        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("============================");
        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[]{
                new ExitOperation(),
                new FindOperation(),
                new BorrowOperation(),
                new ReturnOperation()
        };
    }

    public int menu(){
        System.out.println("==========普通用户菜单==========");
        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("==============================");
        System.out.println("请输入你的操作:");
        Scanner scanner=new Scanner(System.in);
        int choice= scanner.nextInt();
        return choice;
    }
}

4、操作模块

Add

让用户输入需要新增的图书的名字,作者,类型,价格来new一个新的book对象,将这个新对象放入BookList的构造方法的数组中

public class AddOperation implements IOperation{
    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.setBookList(currentSize,book);
        bookList.setUsedSize(currentSize+1);
        System.out.println("新增成功");
    }
}

Borrow

根据使用者传入的需要借阅的图书的名字来与数组中元素的名字匹配,匹配到了先判断该图书是否被借出,未借出修改该图书的借出状态,已借出输出“该书已被借出”,书名匹配失败输出“借阅失败,没有这本书”

public class BorrowOperation implements IOperation{
    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;
            }
        }
        System.out.println("借阅失败,没有这本书");
    }
}

Del

根据使用者传入的需要删除的图书的名字来与数组中元素的名字匹配,匹配到了删除该图书的信息(将这本书后面的元素向前覆盖),匹配不到输出“没有要删除的图书”

public class DelOperation implements IOperation{
    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;
            }
        }
        if (index==-1){
            System.out.println("没有要删除的图书");
            return;
        }
        //从这里开始删除
        for (int i = index; i < currentSize-1; i++) {
            Book book=bookList.getBook(i+1);
            bookList.setBookList(i,book);
        }
        bookList.setUsedSize(currentSize-1);
        System.out.println("删除成功");
    }
}

Display

遍历BookList数组

public class DisplayOperation implements IOperation{
    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);
        }
    }
}

Exit

System.exit()语句

public class ExitOperation implements IOperation{
    public void work(BookList bookList){
        //其实可以对书架手动清空
        System.out.println("退出系统!");
        System.exit(0);
    }
}

Find

根据使用者传入的需要查找的图书的名字来与数组中元素的名字匹配,匹配到了输出该图书的信息匹配不到输出“没有要查找的图书”

public class FindOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("查找图书!");
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入需要查找的图书名字:");
        String name=scanner.nextLine();
        int size=bookList.getUsedSize();
        for (int i = 0; i < size; i++) {
            Book book=bookList.getBook(i);
            if (book.getName().equals(name)){
                System.out.println("找到了这本书");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

Return

修改归还书的借出状态

public class ReturnOperation implements IOperation{
    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;
            }
        }
        System.out.println("归还失败,没有这本书");
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值