一、初识工作

我们首先要清楚的是,图书管理系统,一定要有图书和使用者。所以我创了两个包,分别是book user。那么书这个包里面,我们创建了一个书类,同时也创建了一个书柜类,目的是存放我们所有书的信息。那么我们针对于系统,会有许多操作,我们也单独创建出了一个包operation,用来存放各种操作

image-20220605224454901

二、具体实现

首先,我们之前讲过,面向对象的思路是找对象 创建对象 使用对象,那么我们先来找对象。

毫无疑问的是,图书管理系统必须要创建一个关于书的类Book,以及图书管理系统的使用者。

后续如果还有新的对象,再去添加即可。

注意:我们并不是那么神通广大,一次性就能找到所有需要用的对象,这是个循序渐进的过程,想到什么再去做什么。

我们先去定义一个book的包,在包下创建出一个Book类,里面给一些最基础的属性,以及构造方法、getter and setter、以及toString方法

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

    //getter and setter
    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 double getPrice() {
        return price;
    }

    public void setPrice(double 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;
    }

    //构造方法
    //可以不带isBorrowed,因为boolean类型默认就是false,代表未被借出。一本书最刚开始的时候,默认就是未被借出的
    public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    //toString
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }
}

然后,我们需要创建一个理想化出来的书架类,来存放我们的这些书籍。

要注意到的是,书架可以用数组类型,每个位置存放一本书的各种信息

image-20220603214328297

我们在book包下创建BookList类,然后给出它的构造方法,在他的构造方法中初始化三本书籍。并且我们还创建了一个usedSize变量,用来存放数组当中存放了总共几本书。之后我们给出了usedSizegetter and setter方法,我们又自己增加了两个方法:获取pos下标位置的书以及给数组的pos位置 放一本书。

public class BookList {
    private Book[] books = new Book[10];//这个书架的大小
    private int usedSize;//数组中放了几本书

    public BookList() {
        books[0] = new Book("三国演义","罗贯中",90,"小说");
        books[1] = new Book("西游记","吴承恩",78,"小说");
        books[2] = new Book("红楼梦","曹雪芹",89,"小说");
        this.usedSize = 3;
    }

    /**
     * 获取当前数组当中的元素的个数
     * @return
     */
    public int getUsedSize() {
        return usedSize;
    }

    /**
     * 修改当前数组中元素个数
     * @param usedSize
     */
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    //可以在这里写借书 还书等操作->不推荐

    /**
     * 获取pos下标位置的书
     * @param pos
     * @return
     */
    public Book getPos(int pos) {
        return books[pos];
    }

    /**
     * 给数组的pos位置 放一本书
     * 
     */
    public void setBooks(int pos,Book book) {
        books[pos] = book;
    }
}

其实我们可以在这个类的最后面继续写相关操作,但是这样不太好,我们可以再创建一个operation包,把后续需要用到的相关操作写到这个包里面

然后我们就可以在这个包里面写代码了。要关注的是,我们接下来的几个操作,都需要用到BookList,即书柜这个类,我们就需要把他作为参数传进去

AddOperation

import book.BookList;

public class AddOperation {
    //添加书籍:需要添加到书架上
    public void work(BookList bookList) {
        System.out.println("添加书籍");
    }
}

BorrowOperation

import book.BookList;

public class BorrowOperation {
    //借阅书籍:需要从书架上拿下来供借阅
    public void work(BookList bookList) {
        System.out.println("借阅书籍");
    }
}

DelOperation

import book.BookList;

public class DelOperation {
    //删除书籍:需要从书架上删除
    public void work(BookList bookList) {
        System.out.println("删除书籍");
    }
}

DisplayOperation

import book.BookList;

public class DisplayOperation {
    //展示书籍:需要用到书柜,才能展示里面的书籍
    public void work(BookList bookList) {
        System.out.println("展示书籍");
    }
}

ExitOperation:要注意退出系统时候,如果有清空系统的需求的话,也是要把书柜这个类作为参数传进去的

import book.BookList;

public class ExitOperation {
    //退出系统
    //是不是需要销毁 这个数组当中的所有数据 或者使用到书柜里面的数据
    //答案是 有可能会需要
    public void work(BookList bookList) {
        System.out.println("退出系统");
    }
}

FindOperation

import book.BookList;

public class FindOperation {
    //查找图书:需要用到书柜,才能去查找书的具体位置
    public void work(BookList bookList) {
        System.out.println("查找书籍");
    }
}

ReturnOperation

import book.BookList;

public class ReturnOperation {
    //归还书籍:需要用到书柜,把书放进去
    public void work(BookList bookList) {
        System.out.println("归还书籍");
    }
}

我们写完这么多方法之后,发现都是work方法啊,只不过实现的逻辑不同。

你反应过来了吗,这就是多态。我们就可以把work方法放到一个IOperation接口里面,让这些操作对应的类去继承这个接口,然后重写work方法

IOperation接口的代码:

import book.BookList;

public interface IOperation {
    //接口里面的方法默认是不能实现的
    void work(BookList bookList);
}

别忘了里面要传过去参数,不然不会重载成功

那么我们再去修改一下之前的那几个代码

AddOperation

import book.BookList;

public class AddOperation implements IOperation {
    //添加书籍:需要添加到书架上
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍");
    }
}

BorrowOperation

import book.BookList;

public class BorrowOperation implements IOperation {
    //借阅书籍:需要从书架上拿下来供借阅
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅书籍");
    }
}

DelOperation

import book.BookList;

public class DelOperation implements IOperation {
    //删除书籍:需要从书架上删除
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍");
    }
}

DisplayOperation

import book.BookList;

public class DisplayOperation implements IOperation {
    //展示书籍:需要用到书柜,才能展示里面的书籍
    @Override
    public void work(BookList bookList) {
        System.out.println("展示书籍");
    }
}

ExitOperation:要注意退出系统时候,如果有清空系统的需求的话,也是要把书柜这个类作为参数传进去的

import book.BookList;

public class ExitOperation implements IOperation {
    //退出系统
    //是不是需要销毁 这个数组当中的所有数据 或者使用到书柜里面的数据
    //答案是 有可能会需要
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");
    }
}

FindOperation

import book.BookList;

public class FindOperation implements IOperation {
    //查找图书:需要用到书柜,才能去查找书的具体位置
    @Override
    public void work(BookList bookList) {
        System.out.println("查找书籍");
    }
}

ReturnOperation

import book.BookList;

public class ReturnOperation implements IOperation {
    //归还书籍:需要用到书柜,把书放进去
    @Override
    public void work(BookList bookList) {
        System.out.println("归还书籍");
    }
}

那么目前,我们实现的接口,初步估测有两个作用

  1. 实现了接口之后,就可以实现多态,可以向上转型操作
  2. 使用了接口,规范化代码

那么接下来,我们再为系统的使用者创建一个类user(管理员或者普通用户都可以放在user里面)

public class User {
    protected String name;//不同包的子类也能访问
    //其实还可以罗列更多的属性
}

再创建NormalUser AdminUser,因为普通用户和管理员都是用户,所以让普通用户和管理员继承于User

image-20220604221809369

NormalUser :

public class NormalUser extends User {
    
}

AdminUser:

public class NormalUser extends User {
    
}

这时候我们为User生成构造方法,发现出错了

image-20220603231320892

这是因为在有继承的关系下,子类要先帮父类进行构造

比如:

public class NormalUser extends User {
 //子类要先帮助父类构造
 public NormalUser(String name) {
     super(name);
 }
 //菜单
 public void menu() {
     System.out.println(this.name + "你好,欢迎使用图书管理系统");
     System.out.println("1.查找图书");
     System.out.println("2.借阅图书");
     System.out.println("3.归还图书");
     System.out.println("0.退出系统");
 }
}
public class AdminUser extends User {
 //子类要先帮助父类进行构造
 public AdminUser(String name) {
     super(name);
 }

 //菜单
 public void menu() {
     System.out.println(this.name + "你好,欢迎使用图书管理系统");
     System.out.println("1.查找图书");
     System.out.println("2.新增图书");
     System.out.println("3.删除图书");
     System.out.println("4.显示图书");
     System.out.println("0.退出系统");
 }
}

那么同时,我们也把他们各自的菜单设计出来了

现在,基本的逻辑框架已经差不多了。我们在main方法编写相关语句来执行一下吧

我们设计一个登陆函数

public class Main {

    public static void main(String[] args) {
        login();
    }
}

那么我们可以设计一个login登录函数,我们通过所选择的数字来创建出不同的用户,所以返回值应该为User

public static User login() {
        System.out.println("请输入姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入您的身份:");
        System.out.println("1:管理员   0:普通用户");
        int choice = scanner.nextInt();
        if(choice == 1) {
            return new AdminUser(name);
        } else {
            return new NormalUser(name);
        }
    }

image-20220603231758350

所以我们main方法也需要对User()传过来的返回值进行接收,因为传过来的是AdminUser 或者 NormalUser,所以我们直接用他们俩的父类User来接收即可

public static void main(String[] args) {
        User user = login();//user到底引用哪个对象
    }

但是我们主函数使用User生成的对象去调用menu发现调用失败

image-20220603232030922

这是因为发生了向上转型,user本身是没有menu方法的,不能直接去调用menu方法,但是我们可以在User里面写一个抽象方法,这样AdminUser以及NormalUser就可以重写User里面的方法了。main方法里user也可以使用子类的方法了

public abstract class User {
    protected String name;//不同包的子类也能访问
    //还可以罗列更多的属性

    //构造方法
    public User(String name) {
        this.name = name;
    }

    public abstract void menu();
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W4OZAYBS-1657773093401)(C:/Users/13555/AppData/Roaming/Typora/typora-user-images/image-20220603232519679.png)]

那么我们接下来,需要去再次完善menu,因为之前只是打印了我们的菜单,并未让用户选择

AdminUser

//菜单
    public int menu() {
        System.out.println(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 int menu() {
        System.out.println(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;
    }

因为我们最后返回的是我们的选择,所以要用int接收,抽象方法也需要修改

public abstract class User {
    protected String name;//不同包的子类也能访问
    //还可以罗列更多的属性

    //构造方法
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();
}

那么我们还需要将main方法优化一下,因为我们不可能只使用一次就退出

public static void main(String[] args) {
        User user = login();//user到底引用哪个对象
        while(true) {
            int choice = user.menu();

        }
    }

image-20220604225045351

那么user.menu传过来的数字可能是1,2…,那么我们怎么知道choice接收的是管理员的1还是用户的1呢?

image-20220604230456881

然后我们可以写一个方法,来帮我们具体的去调用NormalUser或者AdminUser

image-20220604230906594

那么我们再帮大家梳理一下结构

image-20220604231613121

那么这样我们的框架就搭出来了。我们可以测试一下

管理员的测试

image-20220605155059042

用户的测试

image-20220605155135684

这里面输入0还没退出是因为我们还没实现退出的功能

博客分隔符-月亮

那么接下来,我们要实现的就是各个功能的逻辑了

我们先来写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.getPos(i);
            System.out.println(book);
        }
    }
}

我们要注意的是,我们之前定义了一个usedSize表示有几本书,那么在这里面是不能直接使用usedSize的,因为我们之前将他设置成了私有属性,所以需要通过get方法拿到usedSize的大小

然后我们还有个getPos方法获取当前位置的书籍(之前没写,现在写也可以)

public Book getPos(int pos) {
        return books[pos];
    }

把当前位置的书赋给新创建的book变量,让他去直接打印(我们之前就重写过toString方法),但是我们现在再来修改一下toString方法,让布局更美观

原来的:

//toString
    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", isBorrowed=" + isBorrowed +
                '}';
    }

现在的:通过三目运算符来打印到底借没借出

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

image-20220605163429959

接下来,是ExitOperation方法

public class ExitOperation implements IOperation {
    //退出系统
    //是不是需要销毁 这个数组当中的所有数据 或者使用到书柜里面的数据
    //答案是 有可能会需要
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            bookList.setBooks(i,null);
        }
        System.exit(0);
    }
}

BookList类中写SetBooks方法

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

我们首先要知道,我们想要退出,可以写System.exit(0),这条语句跟C语言里面的return 0意思是一样的。那么在这之前,我为什么还要遍历一遍数组,将每个元素设置为空呢?

因为我们单纯只是退出系统的话,也能达到每个数据都被销毁的效果,但是在实际工作中,不是随随便便就可以退出系统的,假如我们不进行资源销毁,那么这些没有用的资源还是会占用空间,造成卡顿的,所以我们应该将每一块不需要用的内存置为空,将他的引用断开,垃圾回收器发现有没有引用的资源就会把这些没用的资源回收掉

image-20220605195501416

接下来是AddOperation

public class AddOperation implements IOperation {
    //添加书籍:需要添加到书架上
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍");
        System.out.println("请输入图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格:");
        double price = scanner.nextDouble();

        Book book = new Book(name,author,price,type);

        int currentSize = bookList.getUsedSize();
        bookList.setBooks(currentSize,book);
        bookList.setUsedSize(currentSize + 1);

    }
}

image-20220605202317918

然后是FindOperation

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 curtentSize = bookList.getUsedSize();
        for (int i = 0; i < curtentSize; i++) {
            Book book = bookList.getPos(i);//把每本书都查找出来
            if(book.getName().equals(name)) {//如果当前查找的书名和已经有的书名一样的话
                System.out.println("找到了这本书,信息如下");
                System.out.println(book);
                return;//找到了就可以退出了
            }
        }
        System.out.println("没有这本书");//走到这里了还没有,就是找不到了
    }
}

之后是DelOperation

public class DelOperation 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 index = 0;
        int currentSize = bookList.getUsedSize();
        int i = 0;
        for (i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(i);
            if(book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //1.遇到了break;
        //2.没有找到,结束了for循环
        if(i == currentSize) {
            System.out.println("没有你要删除的图书");
            return;
        }

        for (int j = 0; j < currentSize - 1; j++) {
            //bookList[j] = bookList[j+1]
            Book book = bookList.getPos(j+1);
            bookList.setBooks(j,book);
        }

        //把最后一个位置赋值为null
        bookList.setBooks(currentSize - 1,null);
        bookList.setUsedSize(currentSize - 1);
        System.out.println("已经删除");
    }
}

image-20220605222105487

image-20220605222804526

我们来测试一下:

image-20220605222628911

那么,管理员的功能我们都实现完毕了。接下来,我们来写用户的功能。用户有四个功能,我们已经实现两个了。

image-20220605223004566

BorrowOperation

public class BorrowOperation 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 i = 0;
        int currentSize = bookList.getUsedSize();
        for(i = 0;i < currentSize;i++) {
            Book book = bookList.getPos(i);
            if(book.getName().equals(name)) {
                //找到这本书了
                book.setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("没有您要借阅的书");
    }
}

ReturnOperation:

    //归还书籍:需要用到书柜,把书放进去
    @Override
    public void work(BookList bookList) {
        System.out.println("归还书籍");
        System.out.println("请输入您要归还的书籍名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int i = 0;
        int currentSize = bookList.getUsedSize();
        for(i = 0;i < currentSize;i++) {
            Book book = bookList.getPos(i);
            if(book.getName().equals(name)) {
                //找到这本书了
                book.setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("没有您要归还的书");
    }
}

image-20220605223915269

三、代码

book包

Book类

package book;

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

    //getter and setter
    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 double getPrice() {
        return price;
    }

    public void setPrice(double 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;
    }

    //构造方法
    //可以不带isBorrowed,因为boolean类型默认就是false,代表未被借出。一本书最刚开始的时候,默认就是未被借出的
    public Book(String name, String author, double price, String type) {
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;
    }

    //toString

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

BookList类

package book;

/**
 * 书架类
 */
public class BookList {
    private Book[] books = new Book[10];//这个书架的大小
    private int usedSize;//数组中放了几本书

    public BookList() {
        books[0] = new Book("三国演义","罗贯中",90,"小说");
        books[1] = new Book("西游记","吴承恩",78,"小说");
        books[2] = new Book("红楼梦","曹雪芹",89,"小说");
        this.usedSize = 3;
    }

    /**
     * 获取当前数组当中的元素的个数
     * @return
     */
    public int getUsedSize() {
        return usedSize;
    }

    /**
     * 修改当前数组中元素个数
     * @param usedSize
     */
    public void setUsedSize(int usedSize) {
        this.usedSize = usedSize;
    }

    //可以在这里写借书 还书等操作->不推荐

    /**
     * 获取pos下标位置的书
     * @param pos
     * @return
     */
    public Book getPos(int pos) {
        return books[pos];
    }

    /**
     * 给数组的pos位置 放一本书
     *
     */
    public void setBooks(int pos,Book book) {
        books[pos] = book;
    }
}

operation包

IOperation接口

package operation;

import book.BookList;

public interface IOperation {
    //接口里面的方法默认是不能实现的
    void work(BookList bookList);
}

AddOperation

package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation {
    //添加书籍:需要添加到书架上
    @Override
    public void work(BookList bookList) {
        System.out.println("添加书籍");
        System.out.println("请输入图书的名字:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入图书的作者:");
        String author = scanner.nextLine();
        System.out.println("请输入图书的类型:");
        String type = scanner.nextLine();
        System.out.println("请输入图书的价格:");
        double price = scanner.nextDouble();

        Book book = new Book(name,author,price,type);

        int currentSize = bookList.getUsedSize();
        bookList.setBooks(currentSize,book);
        bookList.setUsedSize(currentSize+1);

    }
}

BorrowOperation

package operation;

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 name = scanner.nextLine();
        int i = 0;
        int currentSize = bookList.getUsedSize();
        for(i = 0;i < currentSize;i++) {
            Book book = bookList.getPos(i);
            if(book.getName().equals(name)) {
                //找到这本书了
                book.setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("没有您要借阅的书");
    }
}

DelOperation

package operation;

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 name = scanner.nextLine();

        int index = 0;
        int currentSize = bookList.getUsedSize();
        int i = 0;
        for (i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(i);
            if(book.getName().equals(name)) {
                index = i;
                break;
            }
        }
        //1.遇到了break;
        //2.没有找到,结束了for循环
        if(i == currentSize) {
            System.out.println("没有你要删除的图书");
            return;
        }

        for (int j = i; j < currentSize - 1; j++) {
            //bookList[j] = bookList[j+1]
            Book book = bookList.getPos(j+1);
            bookList.setBooks(j,book);
        }

        //把最后一个位置赋值为null
        bookList.setBooks(currentSize - 1,null);
        bookList.setUsedSize(currentSize - 1);
        System.out.println("已经删除");
    }
}

DisplayOperation

package operation;

import book.Book;
import book.BookList;

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.getPos(i);
            System.out.println(book);
        }
    }
}

ExitOperation

package operation;

import book.BookList;

public class ExitOperation implements IOperation {
    //退出系统
    //是不是需要销毁 这个数组当中的所有数据 或者使用到书柜里面的数据
    //答案是 有可能会需要
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统");

        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            bookList.setBooks(i,null);
        }
        System.exit(0);
    }
}

FindOperation

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 curtentSize = bookList.getUsedSize();
        for (int i = 0; i < curtentSize; i++) {
            Book book = bookList.getPos(i);
            if(book.getName().equals(name)) {
                System.out.println("找到了这本书,信息如下");
                System.out.println(book);
                return;
            }
        }
        System.out.println("没有这本书");
    }
}

ReturnOperation

package operation;

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 name = scanner.nextLine();
        int i = 0;
        int currentSize = bookList.getUsedSize();
        for(i = 0;i < currentSize;i++) {
            Book book = bookList.getPos(i);
            if(book.getName().equals(name)) {
                //找到这本书了
                book.setBorrowed(false);
                System.out.println("归还成功");
                return;
            }
        }
        System.out.println("没有您要归还的书");
    }
}

user包

AdminUser

package user;

import operation.*;

import java.util.Scanner;

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(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

package user;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import operation.*;

import java.util.Scanner;

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(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

package user;

import book.BookList;
import operation.IOperation;

public abstract class User {
    protected String name;//不同包的子类也能访问
    //还可以罗列更多的属性

    //构造方法
    public User(String name) {
        this.name = name;
    }

    public abstract int menu();

    //创建一个接口数组,存放各种方法
    public IOperation[] iOperations;

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




Main

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

import java.util.Scanner;

public class Main {

    public static User login() {
        System.out.println("请输入姓名:");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        System.out.println("请输入您的身份:");
        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) {
        User user = login();//user到底引用哪个对象
        BookList bookList = new BookList();//初始化三本书
        while(true) {
            int choice = user.menu();
            user.doOperation(choice,bookList);
        }
    }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐