【Java SE】Java实现 图书管理系统 附完整代码

一、菜单

该图书管理系统的菜单界面主要有以下两种:
管理员菜单:
查找图书
新增图书
删除图书
显示图书
退出系统
用户菜单:
查找图书
借阅图书
归还图书
退出系统

二、基本框架

1.book包

因为是图书管理系统,所以可以先从书(book)这个包开始。
book包中有book类和bookList类
在这里插入图片描述

1.1 book类

在book类中存放中图书的各种属性,书名、作者、价格、种类、借出状态。

    private String name;
    private String autor;
    private int price;
    private String type;
    private boolean isBorrowed;//是否被借出 默认是false

再生成对应的构造方法:(Alt + insert) + Construct+(Crtl+鼠标)
鼠标右键 + Generate + Construct+(Crtl+鼠标)
在这里插入图片描述
生成以下代码:

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

再生成对应属性的get和set方法:(Alt + insert)+ Getter and Setter +(Crtl+鼠标)
在这里插入图片描述
生成以下代码:

public String getName() {
    return name;
}
 
public void setName(String name) {
    this.name = name;
}
 
public String getAuthor() {
    return autor;
}
 
public void setAuthor(String autor) {
    this.autor = autor;
}
 
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;
}

最后再重写toString方法:(Alt + insert)+ toString() +(Crtl+鼠标)
在这里插入图片描述在这里插入图片描述
其中isBorrowed可以改写为下面三位运算符的形似:
在这里插入图片描述

1.2 bookList类

bookList类更像是一个书架,书是放在里面的。先创建Book数组,同时可以用useSized来记录图书数量。同时也可以放三本书在书架里面,方便后面运行功能。

    private Book[] books = new Book[10];
    private int useSized = 0;//计数器 来记录 当前实际放的书的书目!
 
    public BookList(){
    //构造方法 来初始化成员
        this.books[0] = new Book("三国演义","罗贯中",15,"小说");
        this.books[1] = new Book("西游记","吴承恩",14,"小说");
        this.books[2] = new Book("红楼梦","曹雪芹",15,"小说");
 
        this.useSized = 3;
    }

再生成useSized的get和set方法:(Alt + insert) + Getter and Setter +(Crtl+鼠标)

 public int getUseSized() {
        return useSized;
    }
 
    public void setUseSized(int useSized) {
        this.useSized = useSized;
    }

2.use包

写完部分book包的内容,我们可以开始写use包、用户方面的内容。从一开始的菜单可以得知,用户分为管理员和普通用户。而这两者之间有一些相同的属性:都有菜单,都有用户姓名。所以可以写一个User来被它们继承。
ues包中有User类、AdminUser类和NormalUser类
在这里插入图片描述

2.1 User类

为了保证name权限合理性,对应的访问修饰限定符为protected
同时生成对应的构造方法:(Alt + insert) + Construct+ 鼠标

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

AdminUser类继承(extends)User类,所以要在AdminUser中构造方法要重写User中的构造方法
对应构造方法的生成:(Alt + insert)+ Overr Methodes + 鼠标

2.2 AdminUser类

在这里插入图片描述
报错是因为子类继承父类后要帮助父类进行构造 要通过alt提示一下 在AdminUser中构造方法要重写User中的构造方法
自己里面不需要定义name了 因为已经继承父类了

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

2.3 NormalUser类

同AdminUser类,步骤同上

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

2.4 用户菜单

考虑到 AdminUser类和NormalUser类都有各自对应的菜单,所以可以把User类写成抽象类。同时里面写抽象方法menu。

public abstract class User {
    protected String name;
    public IOperation[] iOperations;
 
    public User(String name) {
        this.name = name;
    }
 
    public abstract int meun();
}

因为User类变为抽象类,其中有抽象方法menu,AdminUser类和NormalUser类要对meun进行重写,同时添加各自的界面
AdminUser类

public class AdminUser extends User{

    public AdminUser(String name) {
        super(name);
    }
 
    @Override
    public int meun() {
        System.out.println("**********");
        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("请输入需要的操作:");        
    }
}

NormalUser类

public class NormalUser extends User{

    public NormalUser(String name) {
        super(name);
    }
 
    @Override
    public int meun() {
        System.out.println("**********");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("**********");
        System.out.println("请输入需要的操作:");               
    }

3.operation包

在这里插入图片描述
在写完book包和use包的部分内容后,我们可以开始来写不同用户所对应的功能
operation包中有AddIOperation类、BorrowedIOperation类、DelIOperation类、ExitIOperation类、FindIOperation类、IOperation接口、ReturnIOperation类、ShowIOperation类。

3.1 IOperation接口

在创建class文件时,选择Interface,创建接口
在这里插入图片描述
并在其中创建work方法

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

3.2 AddIOperation类

AddIOperation类去使用IOperation接口(implements)。
其中需要重写work方法:(Alt + insert)+ Overr Methodes + 鼠标
在这里插入图片描述
然后对其中的内容进行重写:

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

3.3 剩余类如下

步骤同上:接口 + 重写方法。
BorrowedIOperation类

public class BorrowedeIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书...");
    }
}

DelIOperation类

public class DelIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
}

ExitIOperation类

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

FindIOperation类

public class FindIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查阅图书");
}

ReturnIOperation类

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

ShowIOperation类

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

4.Main类

4.1 login方法

既然要运行该框架,那就需要一个方法来进行,这里创建出login方法来进行
开始界面的思路时:输入姓名,输入是什么类型的用户,并返回对应的菜单

public static User login(){
        System.out.println("请输入你的姓名:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("请输入你的身份:1.管理员  2.普通成员");
        int choice = sc.nextInt();
 
        if(choice == 1){
			/*AdminUser adminUser=new AdminUser(name);
			return adminUser;*/
			return new AdminUser(name);
		}else {
			/*NormalUser normalUser =new NormalUser(name)
			return normalUser;*/
			return new NormalUser(name);
    }

4.2 main函数

接下来通过login方法进行登录

public class Main {
	//可以利用返回值 的向上转型 达到发挥的一致性
    public static User login(){
        System.out.println("请输入你的姓名:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("请输入你的身份:1.管理员  2.普通成员");
        int choice = sc.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();
        while(true){
            int choice = user.meun();
        }
    }
}

此时通过调试可以发现,最后输出的需求并不能直接实现
在这里插入图片描述
要想,输入 1,返回查找图书,我们还需要两个步骤:

  1. 完善管理员菜单和普通用户菜单
  2. 在User类里添加 doIOperation方法。

对于完善各自对应的菜单,我们能想到:如何使输入数字能进行对应的功能。
对应的功能可以放在一个数组里,然后通过数组下标进行输出。所以在User类添加
User类

public IOperation[] iOperations;//只是定义并没有初始化大小

在AdminUser类和NormalUser中也要有对应的改动

  1. 搭建ioperation数组
  2. 使菜单能返回输入的值

AdminUser类

public class AdminUser extends User{
 
    public AdminUser(String name) {
        super(name);
        //1.搭建ioperation数组
        this.iOperations = new IOperation[]{
                new ExitIOperation(),
                new FindIOperation(),
                new AddIOperation(),
                new DelIOperation(),
                new ShowIOperation()
        };
    }
 
    @Override
    public int meun() {
        System.out.println("**********");
        System.out.println("1.查找图书");
        System.out.println("2.新增图书");
        System.out.println("3.删除图书");
        System.out.println("4.显示图书");
        System.out.println("0.退出系统");
        System.out.println("**********");
        //2.使菜单能返回输入的值
        System.out.println("请输入需要的操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

注意:只能在之前定义的时候直接初始化 像这样public IOPeration[]ioPerations ={}
不能这样直接初始化
在这里插入图片描述
NormalUser

public class NormalUser extends User{
 
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitIOperation(),
                new FindIOperation(),
                new BorrowedIOperation(),
                new ReturnIOperation()
        };
    }
 
    @Override
    public int meun() {
        System.out.println("**********");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("**********");
        System.out.println("请输入需要的操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

完成上面步骤,我们可以获取输入值,接下来就可以写doIOperation方法来让打通这条路
因为这是用户所使用的,所以把它写在User类中
User类最终的User如下图所示:

public abstract class User {
    protected String name;
    public IOperation[] iOperations;
 
    public User(String name) {
        this.name = name;
    }
 
    public abstract int meun();
 
    public void doIOperation(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
}

main方法为
在这里插入图片描述

public static void main(String[] args) {
        BookList bookList = new BookList();
        User user = login();
        while(true){
            int choice = user.meun();
            //通过user是哪个对象 choice是几 -> 能够确定:我能够操作哪个对象的哪个方法
			//通过这两个变量 可以确定了 但是怎么联系起来??
			/**
			*1.先让双方 存好 对应自己的操作
			*2:就是调用对应的操作!
			**/ 
            user.doIOperation(choice,bookList);
        }
    }

运行结果为:
在这里插入图片描述

三、具体运行

3.1 ExitIOperation类

退出系统
在后面加 System.exit(0)。在程序运行后输入0便能退出循环,结束运行。

public class ExitIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统...");
        /*int currentSize=bookList.getUsedsize();
		for(inti=0;i<currentsize;i++){
		bookList.setBooks(i,book: null);
		bookList.setUsedsize(0);
		可这样完善业务逻辑 也可以不写直接退出
		*/
        System.exit(0);
    }
}

3.2 ShowIOperation类

显示图书
书是在书架上的,这就是说我们只要循环遍历书架便可以了。
其中可能存在的问题是不知道书架上有几本书,不过在一开始就创建了UsedSized来确定书本数。
为了能输出书,我们需要实例化书架上的书,但书架被private修饰,这时在BookList类里添加就需要get和set方法
BookList类

   public void setBooks(Book[] books) {
        this.books = books;
    }
 
    public Book[] getBooks(){
        return books;
    }

这是IDEA生成的get和set方法,仔细看其中和我们的需求并不一样,我们需要每一本书,而不是这个书架本身,所以可以改为下面的内容

public Book getBook(int pos) {
		return books[pos];
    }
 
    public void setBook(int pos,Book book) {
        this.books[pos] = book;
    }

最后,应用for循环来遍历书架上的每一本书

public class ShowIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书...");
        int currentSize =bookList.getUseSized();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }
}

3.3 AddIOperation类

新增图书
新增图书可以分为四步:

  1. 判断书架是否放满
  2. 输入这本书的信息
  3. 判断书架里有没有这本书
  4. 插入这本书

其中主要是1.判断书架是否放满上面,我们需要在BookList类中添加getBooks方法,来获取数组的长度。
BookList类

public Book[] getBooks(){
        return books;
    }

AddIOperation类

public class AddIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
 
        //1.判满
        int currentSize = bookList.getUseSized();
        if(currentSize == bookList.getBooks().length){
            System.out.println("书放满了");
        }
 
        //2.插入图书信息
        System.out.println("新增图书...");
        System.out.println("请输入书名:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("请输入作者:");
        String autor = sc.nextLine();
        System.out.println("请输入价格:");
        Scanner sc1 = new Scanner(System.in);
        int price = sc1.nextInt();
        System.out.println("请输入类型:");
        String type = sc.nextLine();
 
        Book newbook = new Book(name,autor,price,type);
 
        //3.判断书架有没有这本书
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("书架里有这本书,不能插入");
                return;
            }
        }
 
        //4.插入这本书
        bookList.setBook(currentSize,newbook);
        bookList.setUseSized(currentSize + 1);
        System.out.println("新增图书成功");
    }
}

注意在这里插入图片描述
在这里插入图片描述
上面满是整型下面是字符串 这种情况会把回车读成你的类型
可以多读一次 把回车读进去 然后就会读入正确的类型:
在这里插入图片描述
在这里插入图片描述

3.4 BorrowedIOperation类

借阅图书
通过书名来借阅图书,通过遍历数组中的书来确定
1.是否有这本书
2.是否被借阅

public class BorrowedIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书...");
        System.out.println("请输入要借阅的图书:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
 
        int currentSize = bookList.getUseSized();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
 
 
            if(book.getName().equals(name)){
                if(book.isBorrowed() == true){
                    System.out.println("该图书已被借阅");
                    return;
                }
 
                book.setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("没有你要借阅的图书");
    }
}

3.5 ReturnIoperation类

归还图书
归还图书的整体逻辑和借阅图书的类似,通过代码也能看出这一点

public class ReturnIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书...");
        System.out.println("请输入你要归还的图书:");
        Scanner sc =  new Scanner(System.in);
        String name = sc.nextLine();
 
        int currentSize = bookList.getUseSized();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                if(book.isBorrowed() == false){
                    System.out.println("该图书未被借阅,不能归还");
                    return;
                }
 
                book.setBorrowed(false);
                System.out.println("归还成功");
            }
        }
    }
}

3.6 DelIoperation类

删除图书
删除图书的思路大致如下
1.遍历书架来找到这本书
2.找到即开始删除这本书

public class DelIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书...");
        System.out.print("请输入要删除的图书:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
 
        int currentSize = bookList.getUseSized();
 
        int pos = -1;//pos用来记录找到图书的下标
        //1.找到这本书
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
 
        if(i == currentSize){
            System.out.println("没有你要删除的图书");
            return;
        }
 
        //2.开始删除图书
        for (int j = pos; j < currentSize - 1; j++) {
            //j < currentSize - 1,如果只是currentSize,会越界
            //bookList[j] = bookList[j+1];
            Book book = bookList.getBook(j+1);//创建一个book对象,里面放book【j+1】
            bookList.setBook(j,book);
        }
        bookList.setUseSized(currentSize -1);
        System.out.println("删除成功");
    }
}

总代码

book包

Book类

package book;

public class Book {
    private String name;
    private String autor;
    private int price;
    private String type;
    private boolean isBorrowed;
 
    public Book(String name, String autor, int price, String type) {
        this.name = name;
        this.autor = autor;
        this.price = price;
        this.type = type;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getAutor() {
        return autor;
    }
 
    public void setAutor(String autor) {
        this.autor = autor;
    }
 
    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 + '\'' +
                ", autor='" + autor + '\'' +
                ", price=" + price +
                ", type='" + type + '\'' +
                ", " + (isBorrowed == true ? "已借出" :"未借出") +
                '}';
    }
}

BookList类

package book;
 
public class BookList {
    private Book[] books = new Book[10];
    private int useSized = 0;
 
    public BookList(){
        this.books[0] = new Book("三国演义","罗贯中",15,"小说");
        this.books[1] = new Book("西游记","吴承恩",14,"小说");
        this.books[2] = new Book("红楼梦","曹雪芹",15,"小说");
 
        this.useSized = 3;
    }
 
    public int getUseSized() {
        return useSized;
    }
 
    public void setUseSized(int useSized) {
        this.useSized = useSized;
    }
 
    public Book getBook(int pos) {
        return books[pos];
    }
 
    public void setBook(int pos,Book book) {
        this.books[pos] = book;
    }
 
    public Book[] getBooks(){
        return books;
    }
}

opera包

IOperation接口

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

AddIOperation类

package opera;
 
import book.Book;
import book.BookList;
 
import java.util.Scanner;
 
public class AddIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
 
        //1.判满
        int currentSize = bookList.getUseSized();
        if(currentSize == bookList.getBooks().length){
            System.out.println("书放满了");
        }
 
        //2.插入
        System.out.println("新增图书...");
        System.out.println("请输入书名:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        System.out.println("请输入作者:");
        String autor = sc.nextLine();
        System.out.println("请输入价格:");
        Scanner sc1 = new Scanner(System.in);
        int price = sc1.nextInt();
        System.out.println("请输入类型:");
        String type = sc.nextLine();
 
        Book newbook = new Book(name,autor,price,type);
 
        //3.判断书架有没有这本书
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("书架里有这本书,不能插入");
                return;
            }
        }
 
        //4.插入这本书
        bookList.setBook(currentSize,newbook);
        bookList.setUseSized(currentSize + 1);
        System.out.println("新增图书成功");
    }
}

BorroweIOperation类

package opera;
 
import book.Book;
import book.BookList;
 
import java.util.Scanner;
 
public class BorrowedIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("借阅图书...");
        System.out.println("请输入要借阅的图书:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
 
        int currentSize = bookList.getUseSized();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
 
            if(book.getName().equals(name)){
                if(book.isBorrowed() == true){
                    System.out.println("该图书已被借阅");
                    return;
                }
 
                book.setBorrowed(true);
                System.out.println("借阅成功");
                return;
            }
        }
        System.out.println("没有你要借阅的图书");
    }
}

DelIOperation类

package opera;
 
import book.Book;
import book.BookList;
 
import java.util.Scanner;
 
public class DelIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书...");
        System.out.print("请输入要删除的图书:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
 
        int currentSize = bookList.getUseSized();
 
        int pos = -1;//pos用来记录找到图书的下标
        //1.找到这本书
        int i = 0;
        for (; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                pos = i;
                break;
            }
        }
 
        if(i == currentSize){
            System.out.println("没有你要删除的图书");
            return;
        }
 
        //2.开始删除图书
        for (int j = pos; j < currentSize - 1; j++) {
            //j < currentSize - 1,如果只是currentSize,会越界
            //bookList[j] = bookList[j+1];
            Book book = bookList.getBook(j+1);//创建一个book对象,里面放book【j+1】
            bookList.setBook(j,book);
        }
        bookList.setUseSized(currentSize -1);
        System.out.println("删除成功");
    }
}

ExitIOperation类

package opera;
 
import book.Book;
import book.BookList;
 
public class ExitIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("退出系统...");
        System.exit(0);
    }
}

FindIOperation类

package opera;
 
import book.Book;
import book.BookList;
 
import java.util.Scanner;
 
public class FindIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("查阅图书...");
        System.out.println("输入要查找的图书:");
        Scanner sc = new Scanner(System.in);
        String name = sc.nextLine();
        int currentSize = bookList.getUseSized();
        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("没有找到这本书");
    }
}

ReturnIOperation类

package opera;
 
import book.Book;
import book.BookList;
 
import java.util.Scanner;
 
public class ReturnIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("归还图书...");
        System.out.println("请输入你要归还的图书:");
        Scanner sc =  new Scanner(System.in);
        String name = sc.nextLine();
 
        int currentSize = bookList.getUseSized();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                if(book.isBorrowed() == false){
                    System.out.println("该图书未被借阅,不能归还");
                    return;
                }
 
                book.setBorrowed(false);
                System.out.println("归还成功");
            }
        }
 
    }
 
}

ShowIOperation类

package opera;
 
import book.Book;
import book.BookList;
 
public class ShowIOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("显示图书...");
        int currentSize =bookList.getUseSized();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            System.out.println(book);
        }
    }

use包

User类

package use;
import book.BookList;
import opera.IOperation;
 
public abstract class User {
    protected String name;
    public IOperation[] iOperations;
 
    public User(String name) {
        this.name = name;
    }
 
    public abstract int meun();
 
    public void doIOperation(int choice, BookList bookList){
        iOperations[choice].work(bookList);
    }
}

AdminUser类

package use;
 
import opera.*;
 
import java.util.Scanner;
 
public class AdminUser extends User{
 
    public AdminUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitIOperation(),
                new FindIOperation(),
                new AddIOperation(),
                new DelIOperation(),
                new ShowIOperation()
        };
    }
 
    @Override
    public int meun() {
        System.out.println("**********");
        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 sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}

NormalUser类

package use;
 
import opera.*;
 
import java.util.Scanner;
 
public class NormalUser extends User{
 
    public NormalUser(String name) {
        super(name);
        this.iOperations = new IOperation[]{
                new ExitIOperation(),
                new FindIOperation(),
                new BorrowedIOperation(),
                new ReturnIOperation()
        };
    }
 
    @Override
    public int meun() {
        System.out.println("**********");
        System.out.println("1.查找图书");
        System.out.println("2.借阅图书");
        System.out.println("3.归还图书");
        System.out.println("0.退出系统");
        System.out.println("**********");
        System.out.println("请输入需要的操作:");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        return choice;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值