图书管理系统Java(IO流)

该博客详细介绍了如何实现一个图书管理系统,包括用户注册、登录、用户信息管理、图书信息管理、图书借阅和归还等功能。系统使用Java实现,具备增删改查、排序、查询等操作,并采用文件进行数据持久化。用户信息、图书信息、读者信息和借阅信息分别用User、Book、Reader和BorrowBook类表示,通过IO类进行文件操作。
摘要由CSDN通过智能技术生成

一、定义的类

 

 1、Book类

import java.io.Serializable;
import java.text.Collator;
import java.util.Locale;

public class Book implements Serializable,Comparable<Book> {
    private int number;//数量
    private double price;//价格
    private String bookId;//书号
    private String bookName;//书名
    private String writer;//作者
    private String publishingHouse;//出版社
    private String publicationDate;//出版日期
    public String getBookId() {
        return bookId;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getWriter() {
        return writer;
    }

    public void setWriter(String writer) {
        this.writer = writer;
    }

    public String getPublishingHouse() {
        return publishingHouse;
    }

    public void setPublishingHouse(String publishingHouse) {
        this.publishingHouse = publishingHouse;
    }

    public String getPublicationDate() {
        return publicationDate;
    }

    public void setPublicationDate(String publicationDate) {
        this.publicationDate = publicationDate;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book() {
    }

    public Book(String bookId, String bookName, String writer, String publishingHouse, String publicationDate, int number, double price) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.writer = writer;
        this.publishingHouse = publishingHouse;
        this.publicationDate = publicationDate;
        this.number = number;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book【" +
                "bookId:'" + bookId + '\'' +
                ", bookName:'" + bookName + '\'' +
                ", writer:'" + writer + '\'' +
                ", publishingHouse:'" + publishingHouse + '\'' +
                ", publicationDate:'" + publicationDate + '\'' +
                ", number:" + number +
                ", price:" + price +
                '】';
    }

    @Override
    public int compareTo(Book o) {
        Collator.getInstance(Locale.CHINA);
        int i = bookName.compareTo(o.bookName);
        if (i == 0){
            for (int i1 = 0; i1 < bookId.length(); i1++) {
                if (bookId.charAt(i1) != o.bookId.charAt(i1)){
                    return bookId.charAt(i1) - o.bookId.charAt(i1);
                }
            }
            return 0;
        }else {
            return i;
        }
    }
}

 2、BorrowBook类

import java.io.Serializable;

public class BorrowBook implements Serializable {
    private String studentId;//学号
    private String studentName;//学生姓名
    private String bookId;//书号
    private String bookName;//书名
    private String borrowDate;//借书时间
    private String shouldReturnDate;//应还时间
    private String returnDate;//实际还书时间

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getBookId() {
        return bookId;
    }

    public void setBookId(String bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getBorrowDate() {
        return borrowDate;
    }

    public void setBorrowDate(String borrowDate) {
        this.borrowDate = borrowDate;
    }

    public String getShouldReturnDate() {
        return shouldReturnDate;
    }

    public void setShouldReturnDate(String shouldReturnDate) {
        this.shouldReturnDate = shouldReturnDate;
    }

    public String getReturnDate() {
        return returnDate;
    }

    public void setReturnDate(String returnDate) {
        this.returnDate = returnDate;
    }

    public BorrowBook() {
    }

    public BorrowBook(String studentId, String studentName, String bookId, String bookName, String borrowDate, String shouldReturnDate, String returnDate) {
        this.studentId = studentId;
        this.studentName = studentName;
        this.bookId = bookId;
        this.bookName = bookName;
        this.borrowDate = borrowDate;
        this.shouldReturnDate = shouldReturnDate;
        this.returnDate = returnDate;
    }

    @Override
    public String toString() {
        return "BorrowBook【" +
                "studentId:'" + studentId + '\'' +
                ", studentName:'" + studentName + '\'' +
                ", bookId:'" + bookId + '\'' +
                ", bookName:'" + bookName + '\'' +
                ", borrowDate:'" + borrowDate + '\'' +
                ", shouldReturnDate:'" + shouldReturnDate + '\'' +
                ", returnDate:'" + returnDate + '\'' +
                '】';
    }
}

 3、Reader类

import java.io.Serializable;
import java.text.Collator;
import java.util.Locale;

public class Reader implements Serializable,Comparable<Reader> {
    private String studentId;//学号
    private String studentName;//姓名
    private String college;//学院
    private String specialty;//专业
    private String gradeAndClass;//班级

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }

    public String getSpecialty() {
        return specialty;
    }

    public void setSpecialty(String specialty) {
        this.specialty = specialty;
    }

    public String getGradeAndClass() {
        return gradeAndClass;
    }

    public void setGradeAndClass(String gradeAndClass) {
        this.gradeAndClass = gradeAndClass;
    }

    public Reader() {
    }

    public Reader(String studentId, String studentName, String college, String specialty, String gradeAndClass) {
        this.studentId = studentId;
        this.studentName = studentName;
        this.college = college;
        this.specialty = specialty;
        this.gradeAndClass = gradeAndClass;
    }

    @Override
    public String toString() {
        return "Reader【" +
                "studentId:'" + studentId + '\'' +
                ", studentName:'" + studentName + '\'' +
                ", college:'" + college + '\'' +
                ", specialty:'" + specialty + '\'' +
                ", gradeAndClass:'" + gradeAndClass + '\'' +
                '】';
    }

    @Override
    public int compareTo(Reader o) {
        Collator.getInstance(Locale.CHINA);
        //Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        int i = o.college.compareTo(college);
        if (i == 0){
            for (int i1 = 0; i1 < studentId.length(); i1++) {
                if (studentId.charAt(i1) != o.studentId.charAt(i1)){
                    return studentId.charAt(i1) - o.studentId.charAt(i1);
                }
            }
            return 0;
        }else {
            return i;
        }
    }
}

4、User类

import java.io.Serializable;

public class User implements Serializable {
    private String userId;//账号
    private String userName;//用户名
    private String passWord;//密码

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public User() {
    }

    public User(String userId, String userName, String passWord) {
        this.userId = userId;
        this.userName = userName;
        this.passWord = passWord;
    }

    @Override
    public String toString() {
        return "userName【" +
                "userId:'" + userId + '\'' +
                ", userName:'" + userName + '\'' +
                ", passWord:'" + passWord + '\'' +
                '】';
    }
}

5、IO类

将需要用到的io流操作都封装在这个类里面方便调用

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 将集合保存至本地txt文件
 * 序列化
 */
public class IO {
    public IO() {
    }

    /**
     * 序列化,保存到文件
     * @param list 要保存的集合
     * @param fos 字节输出流,访问文件
     * @throws IOException 抛出的异常
     */
    private static void store(List list, FileOutputStream fos) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(fos);//ObjectOutputStream将字符流转换为字节流
        oos.writeObject(list);
        oos.close();
    }

    /**
     * 反序列化,读取文件
     * @param fis 字节输入流
     * @return 文件为空关闭文件返回一个空集合,文件不为空返回一集合,包含内容不确定
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private static List load(FileInputStream fis, File file){
        List list = new ArrayList<>();
        if (file.length()==0){
            try {
                fis.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }else {
            ObjectInputStream ois = null;//ObjectInputStream将字节流转换为字符流
            try {
                ois = new ObjectInputStream(fis);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                list = (List) ois.readObject();
                ois.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        return list;
    }

    /**
     *  保存book集合
     * @param list  book集合
     * @throws IOException
     */
    public void shoreBook(List<Book> list) throws IOException {
        FileOutputStream fos = new FileOutputStream(new File("src/Data//books.txt"));
        store(list,fos);
    }

    /**
     * 读取book集合
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public List<Book> loadBook() throws IOException, ClassNotFoundException {
        File file = new File("src/Data//books.txt");
        if (!file.exists()){
            file.createNewFile();
        }
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return (List<Book>)load(fis,file);
    }

    /**
     * 保存Reader集合
     * @param list  Reader集合
     * @throws IOException
     */
    public void storeReader(List<Reader> list) throws IOException {
        File file = new File("src/Data//readers.txt");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            if (!file.exists()){
                file.createNewFile();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        store(list,fos);
    }

    /**
     * 读取Reader集合
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public List<Reader> loadReader() throws IOException, ClassNotFoundException {
        File file = new File("src/Data//readers.txt");
        if (!file.exists()){
            file.createNewFile();
        }
        FileInputStream fis = new FileInputStream(file);
        return (ArrayList<Reader>)load(fis,file);
    }

    /**
     * 保存BorrowBook集合
     * @param list  BorrowBook集合
     * @throws IOException
     */
    public void storeBorrowBook(List<BorrowBook> list) throws IOException {
        FileOutputStream fos = new FileOutputStream("src/Data//borrowBooks.txt");
        store(list,fos);
    }

    /**
     * 读取BorrowBook集合
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public List<BorrowBook> loadBorrowBook() throws IOException, ClassNotFoundException {
        File file = new File("src/Data//borrowBooks.txt");
        if (!file.exists()){
            file.createNewFile();
        }
        FileInputStream fis = new FileInputStream(file);
        return (ArrayList<BorrowBook>)load(fis,file);
    }

    /**
     * 保存users集合
     * @param list users集合
     * @throws IOException
     */
    public void storeUser(List<User> list) throws IOException {
        FileOutputStream fos = new FileOutputStream("src/Data//users.txt");
        store(list,fos);
    }

    /**
     * 读取users集合
     * @return
     * @throws IOException
     */
    public List<User> loaduser() throws IOException {
        File file = new File("src/Data//users.txt");
        if (!file.exists()){
            file.createNewFile();
        }
        FileInputStream fis = new FileInputStream(file);
        return  (ArrayList<User>)load(fis,file);
    }
}

 6、Main类

实现了主要的功能,代码放在最后

二、首页功能

1、用户注册

①让用户输入用户名

②系统生成一个唯一的不重复的8位数账号

③让用户设置密码,连续两次输入一致设置成功

④将生成的用户信息录入系统

生成8位数账号代码:

private static String getRandomCardId(List<User> users) {
        Random r = new Random();
        while (true){
            //生成8位数字
            StringBuilder userId =new StringBuilder();
            for (int i = 0; i < 8; i++) {
                userId.append(r.nextInt(10));
            }
            //判断是否与其他用户重复,根据userId查询用户对象
            User user = getUserByUserId(userId.toString(),users);
            if(user == null){
                //userId没有重复,可以做新账户的账号
                return userId.toString();
            }
        }
    }

 主要代码:

private static void register(List<User> users, Scanner sc, IO io) throws IOException {
        User user = new User();
        System.out.println("请输入用户名:");
        String userName = sc.next();
        user.setUserName(userName);

        while(true) {
            System.out.println("请输入账户密码:");
            String passWorld = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassWorld = sc.next();
            if (okPassWorld.equals(passWorld)) {
                //密码认证通过,可以注入给用户对象
                user.setPassWord(okPassWorld);
                break;//密码录入成功,退出死循环
            }else{
                System.out.println("对不起,您输入的2次密码不一致,请重新确认~~~");
            }
        }
        String userId = getRandomCardId(users);
        user.setUserId(userId);
        users.add(user);
        io.storeUser(users);
        System.out.println("恭喜您," + userName + "先生/女士,您注册成功,您的卡号是:" + userId + "请妥善保管卡号");
    }

2、用户登录

代码:

//判断是否存在user
        if (users.size() == 0){
            System.out.println("请先注册后登录~~~");
            return;
        }
        while (true){
            //user存在,正式开始登陆
            System.out.println("请你输入登陆userId:");
            String cardId = sc.next();
            //判断userId是否存在(根据userId去账户集合中查询账户对象)
            User user =getUserByUserId(cardId,users);
            if (user != null){
                while (true){
                    //userId存在
                    //让用户输入密码,认证密码
                    System.out.println("请输入登陆密码:");
                    String passWord = sc.next();
                    //判断当前用户对象的密码是否与输入的密码一致
                    if (user.getPassWord().equals(passWord)){
                        //登陆成功了
                        System.out.println("恭喜您," + user.getUserName() + "先生|女士进入系统~~~");

                        //展示登录后的操作页
                        showUserCommand(sc,io);
                        return;//退出登陆方法
                    }else{
                        System.out.println("对不起,您输入的密码有误~~~");
                    }
                }
            }else {
                System.out.println("对不起,系统中不存在该用户账号~~~");
            }
        }

3、用户信息管理 

 包含3个功能,删除、查找、显示,每个功能都封装成函数

①用户信息删除

private static void deleteUser(Scanner sc, IO io, List<User> users) throws IOException {
        System.out.println("请输入要删除用户的账号:");
        String userId = sc.next();
        User user = getUserByUserId(userId,users);
        if (user != null){
            System.out.println("请输入此用户的密码:");
            String passWord = sc.next();
            if (user.getPassWord().equals(passWord)){
                int index = returnIndexByUserId(userId,users);
                users.remove(index);
                io.storeUser(users);
                System.out.println("删除成功~~~");
            }
        }else {
            System.out.println("系统中不存在此用户账号~~~");
        }
    }

②用户信息查找

private static void findUser(Scanner sc, List<User> users) {
        System.out.println("请输入用户名:");
        String userName = sc.next();
        for (User user:users) {
            if (user.getUserName().equals(userName)){
                System.out.println(user.getUserName() + "的账号为:" + user.getUserId());
                return;
            }
        }
        System.out.println("系统中不存在此用户名~~~");
    }

③用户信息列表显示

 private static void printUser(List<User> users) {
        if (users.size() == 0){
            System.out.println("系统中还没有用户信息~~~");
            return;
        }
        for (User user:users) {
            System.out.println(user.getUserName() + "的账号为:" + user.getUserId());
        }
    }

主要代码:

private static void manageUserInformation(Scanner sc, IO io, List<User> users) throws IOException {
        while (true){
            System.out.println("================1、用户信息删除===================");
            System.out.println("================2、用户信息查找===================");
            System.out.println("================3、用户信息列表显示================");
            System.out.println("================0、退出用户信息管理================");
            System.out.println("请选择操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    deleteUser(sc,io,users);//删除
                    break;
                case 2:
                    findUser(sc,users);//查找
                    break;
                case 3:
                    printUser(users);//输出全部用户信息,不包括密码
                    break;
                case 0:
                    return;//退出用户信息管理
            }
        }

    }

 三、登录成功后功能

将每一个功能封装成方法,这里只展示一下结构,具体的代码放在最后

1、读者信息管理

/**
     * 读者信息管理
     * @param sc 扫描器
     * @param readers 全部读者
     * @param io io方法
     * @throws IOException 异常
     */
    private static void readersInformationManage(Scanner sc,List<Reader> readers,IO io) throws IOException {
        while(true){
            System.out.println("================1、读者信息的添加================");
            System.out.println("================2、读者信息的查询================");
            System.out.println("================3、修改读者的信息================");
            System.out.println("================4、删除读者的信息================");
            System.out.println("================5、读者信息的排序================");
            System.out.println("================6、读者信息的显示================");
            System.out.println("================0、退出此界面=====================");
            System.out.println("请选择操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    addReader(sc,readers,io);//读者信息的添加
                    break;
                case 2:
                    findReader(sc,readers);//读者信息的查询
                    break;
                case 3:
                    modifyReader(sc,readers,io);//修改读者的信息
                    break;
                case 4:
                    deleteReader(sc,readers,io);//删除读者的信息
                    break;
                case 5:
                    sortReader(readers,io);//读者信息的排序
                    break;
                case 6:
                    printReader(readers);//读者信息的显示
                    break;
                case 0://退出此界面
                    return;
            }
        }
    }

 2、图书信息管理

/**
     * 图书信息管理
     * @param sc 扫描器
     * @param books 全部图书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void booksInformationManage(Scanner sc, List<Book> books, IO io) throws IOException {
        while(true){
            System.out.println("================1、图书信息的添加================");
            System.out.println("================2、图书信息的查询================");
            System.out.println("================3、图书信息的修改================");
            System.out.println("================4、图书信息的删除================");
            System.out.println("================5、图书信息的排序================");
            System.out.println("================6、图书信息的显示================");
            System.out.println("================0、退出功能=====================");
            int select = sc.nextInt();
            switch(select){
                case 1:
                    addBook(sc,books,io);
                    break;
                case 2:
                    findBook(sc,books);
                    break;
                case 3:
                    modifyBook(sc,books,io);
                    break;
                case 4:
                    deleteBook(sc,books,io);
                    break;
                case 5:
                    sortBook(books,io);
                    break;
                case 6:
                    printBook(books);
                    break;
                case 0:
                    return;
            }
        }
    }

 3、图书借阅

①用户输入学号,书号

②学号数号均存在,且书的数量不为0,可以进行借阅操作

③使用时间日期相关类,记录接书时间

④修改图书表中和图书借阅表中的信息

4、图书归还

①输入学号书号,在图书借阅表中查询是否存在相关对借阅信息

②如存在,进行还书操作

③使用时间日期相关类,记录还书时间

④修改图书表中和图书借阅表中的信息

5、图书借阅查询

/**
     * 图书借阅信息查询
     * @param sc 扫描器
     * @param borrowBooks 全部借阅信息
     * @param readers 全部读者
     * @param books 全部图书
     */
    private static void findBorrowBook(Scanner sc, List<BorrowBook> borrowBooks, List<Reader> readers,List<Book> books) {
        while(true){
            System.out.println("================1、按学号进行查询================");
            System.out.println("================2、按书号进行查询================");
            System.out.println("================3、按学院进行查询================");
            System.out.println("================4、显示全部借阅信息===============");
            System.out.println("================0、退出功能======================");
            System.out.println("请选择操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    findBorrowByStudentId(sc,borrowBooks,readers);
                    break;
                case 2:
                    findBorrowBookByBookId(sc,borrowBooks,books);
                    break;
                case 3:
                    findBorrowBookByCollege(sc,borrowBooks,readers);
                case 4:
                    printBorrowBook(borrowBooks);
                    break;
                case 0:
                    return;
            }
        }
    }

四、实现功能的主要方法的代码

 

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        IO io = new IO();
        List<User> users;//用户
        users = io.loaduser();//从文件中导出用户
        while(true) {
            System.out.println("=======================图书管理系统======================");
            System.out.println("1、登录");
            System.out.println("2、注册");
            System.out.println("3、管理users信息");
            System.out.println("请选择操作:");
            int command = sc.nextInt();

            switch(command){
                case 1:
                    login(users,sc,io);//登录
                    break;
                case 2:
                    register(users,sc,io);//注册
                    break;
                case 3:
                    manageUserInformation(sc,io,users);//用户信息管理
            }
        }


    }

    private static void manageUserInformation(Scanner sc, IO io, List<User> users) throws IOException {
        while (true){
            System.out.println("================1、用户信息删除===================");
            System.out.println("================2、用户信息查找===================");
            System.out.println("================3、用户信息列表显示================");
            System.out.println("================0、退出用户信息管理================");
            System.out.println("请选择操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    deleteUser(sc,io,users);//删除
                    break;
                case 2:
                    findUser(sc,users);//查找
                    break;
                case 3:
                    printUser(users);//输出全部用户信息,不包括密码
                    break;
                case 0:
                    return;//退出用户信息管理
            }
        }

    }

    private static void printUser(List<User> users) {
        if (users.size() == 0){
            System.out.println("系统中还没有用户信息~~~");
            return;
        }
        for (User user:users) {
            System.out.println(user.getUserName() + "的账号为:" + user.getUserId());
        }
    }

    private static void findUser(Scanner sc, List<User> users) {
        System.out.println("请输入用户名:");
        String userName = sc.next();
        for (User user:users) {
            if (user.getUserName().equals(userName)){
                System.out.println(user.getUserName() + "的账号为:" + user.getUserId());
                return;
            }
        }
        System.out.println("系统中不存在此用户名~~~");
    }

    /**
     * 用户信息的删除
     * @param sc 扫描器
     * @param io io方法
     * @param users 全部用户
     */
    private static void deleteUser(Scanner sc, IO io, List<User> users) throws IOException {
        System.out.println("请输入要删除用户的账号:");
        String userId = sc.next();
        User user = getUserByUserId(userId,users);
        if (user != null){
            System.out.println("请输入此用户的密码:");
            String passWord = sc.next();
            if (user.getPassWord().equals(passWord)){
                int index = returnIndexByUserId(userId,users);
                users.remove(index);
                io.storeUser(users);
                System.out.println("删除成功~~~");
            }
        }else {
            System.out.println("系统中不存在此用户账号~~~");
        }
    }

    /**
     * 查找用户下标
     * @param userId 用户账号
     * @param users 全部用户
     * @return 下标|-1
     */
    private static int returnIndexByUserId(String userId, List<User> users) {
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getUserId().equals(userId)){
                return i;
            }
        }
        return -1;
    }

    /**
     * 注册user
     * @param users 全部用户
     * @param sc 扫描器
     * @param io io方法
     * @throws IOException
     */
    private static void register(List<User> users, Scanner sc, IO io) throws IOException {
        User user = new User();
        System.out.println("请输入用户名:");
        String userName = sc.next();
        user.setUserName(userName);

        while(true) {
            System.out.println("请输入账户密码:");
            String passWorld = sc.next();
            System.out.println("请您输入确认密码:");
            String okPassWorld = sc.next();
            if (okPassWorld.equals(passWorld)) {
                //密码认证通过,可以注入给用户对象
                user.setPassWord(okPassWorld);
                break;//密码录入成功,退出死循环
            }else{
                System.out.println("对不起,您输入的2次密码不一致,请重新确认~~~");
            }
        }
        String userId = getRandomCardId(users);
        user.setUserId(userId);
        users.add(user);
        io.storeUser(users);
        System.out.println("恭喜您," + userName + "先生/女士,您注册成功,您的卡号是:" + userId + "请妥善保管卡号");
    }

    /**
     * 生成8位随机账号
     * @param users 全部用户
     * @return user|null
     */
    private static String getRandomCardId(List<User> users) {
        Random r = new Random();
        while (true){
            //生成8位数字
            StringBuilder userId =new StringBuilder();
            for (int i = 0; i < 8; i++) {
                userId.append(r.nextInt(10));
            }
            //判断是否与其他用户重复,根据userId查询用户对象
            User user = getUserByUserId(userId.toString(),users);
            if(user == null){
                //userId没有重复,可以做新账户的账号
                return userId.toString();
            }
        }
    }

    /**
     * 根据账号获取用户
     * @param userId
     * @param users
     * @return
     */
    private static User getUserByUserId(String userId, List<User> users) {
        for (User user:users) {
            if (user.getUserId().equals(userId)){
                return user;
            }
        }
        return null;
    }

    /**
     * 登录
     * @param users 全部的用户
     * @param sc 扫描器
     * @param io io方法
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private static void login(List<User> users, Scanner sc, IO io) throws IOException, ClassNotFoundException {
        //判断是否存在user
        if (users.size() == 0){
            System.out.println("请先注册后登录~~~");
            return;
        }
        while (true){
            //user存在,正式开始登陆
            System.out.println("请你输入登陆userId:");
            String cardId = sc.next();
            //判断userId是否存在(根据userId去账户集合中查询账户对象)
            User user =getUserByUserId(cardId,users);
            if (user != null){
                while (true){
                    //userId存在
                    //让用户输入密码,认证密码
                    System.out.println("请输入登陆密码:");
                    String passWord = sc.next();
                    //判断当前用户对象的密码是否与输入的密码一致
                    if (user.getPassWord().equals(passWord)){
                        //登陆成功了
                        System.out.println("恭喜您," + user.getUserName() + "先生|女士进入系统~~~");

                        //展示登录后的操作页
                        showUserCommand(sc,io);
                        return;//退出登陆方法
                    }else{
                        System.out.println("对不起,您输入的密码有误~~~");
                    }
                }
            }else {
                System.out.println("对不起,系统中不存在该用户账号~~~");
            }
        }

    }

    /**
     * 登录后的操作页
     * @param sc 扫描器
     * @param io io方法
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private static void showUserCommand(Scanner sc, IO io) throws IOException, ClassNotFoundException {
        List<Book> books;
        List<Reader> readers;
        List<BorrowBook> borrowBooks;
        //从文件中读取已有信息
        books = io.loadBook();
        readers = io.loadReader();
        borrowBooks = io.loadBorrowBook();


        while(true){
            System.out.println("==============欢迎使用图书管理系统==============");
            System.out.println("================1、读者信息管理================");
            System.out.println("================2、图书信息管理================");
            System.out.println("================3、图书借阅====================");
            System.out.println("================4、图书归还====================");
            System.out.println("================5、图书借阅查询=================");
            System.out.println("================0、退出系统====================");
            System.out.println("请选择操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    readersInformationManage(sc,readers,io);//读者信息管理
                    break;
                case 2:
                    booksInformationManage(sc,books,io);//图书信息管理
                    break;
                case 3:
                    borrowBook(sc,borrowBooks,io,books,readers);//图书借阅
                    break;
                case 4:
                    returnBook(sc,borrowBooks,io,books,readers);//图书归还
                    break;
                case 5:
                    findBorrowBook(sc,borrowBooks,readers,books);//图书借阅信息查询
                    break;
                case 0:
                    return;//退出系统
            }
        }
    }

    /**
     * 图书借阅信息查询
     * @param sc 扫描器
     * @param borrowBooks 全部借阅信息
     * @param readers 全部读者
     * @param books 全部图书
     */
    private static void findBorrowBook(Scanner sc, List<BorrowBook> borrowBooks, List<Reader> readers,List<Book> books) {
        while(true){
            System.out.println("================1、按学号进行查询================");
            System.out.println("================2、按书号进行查询================");
            System.out.println("================3、按学院进行查询================");
            System.out.println("================4、显示全部借阅信息===============");
            System.out.println("================0、退出功能======================");
            System.out.println("请选择操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    findBorrowByStudentId(sc,borrowBooks,readers);
                    break;
                case 2:
                    findBorrowBookByBookId(sc,borrowBooks,books);
                    break;
                case 3:
                    findBorrowBookByCollege(sc,borrowBooks,readers);
                case 4:
                    printBorrowBook(borrowBooks);
                    break;
                case 0:
                    return;
            }
        }
    }

    /**
     * 显示全部借阅信息
     * @param borrowBooks 全部借阅信息
     */
    private static void printBorrowBook(List<BorrowBook> borrowBooks) {
        if (borrowBooks.size() == 0){
            System.out.println("系统中还没有借阅信息~~~");
        }
        borrowBooks.forEach(System.out::println);
    }

    /**
     * 按学院查询借阅信息
     * @param sc
     * @param borrowBooks
     * @param readers
     */
    private static void findBorrowBookByCollege(Scanner sc, List<BorrowBook> borrowBooks, List<Reader> readers) {
        //获取目标学院的读者信息
        List<Reader> readersList = findReaderByCollege(sc,readers);
        //判断系统中是否有录入该学院的读者
        if (readersList.size() != 0){
            //有
            String flag = "n";
            List<BorrowBook> borrowBookList;
            //遍历获取到的读者信息
            for (Reader reader:readersList) {
                borrowBookList = getBorrowBooksByStudentId(reader.getStudentId(),borrowBooks);//根据读者信息在图书借阅表中获取相关借阅信息
                if (borrowBookList.size() != 0){//判断该读者是否在此借阅过图书
                    flag = "y";
                    borrowBookList.forEach(System.out::println);//有则输出
                }
            }
            if (flag.equals("n")){
                System.out.println("没有书被该学院的人借阅~~~");
            }
        }else {
            System.out.println("抱歉,系统没有录入该学院的人~~~");
        }
    }

    /**
     * 根据学院查找读者
     * @param sc 扫描器
     * @param readers 全部读者
     * @return 查找到的读者信息
     */
    private static List<Reader> findReaderByCollege(Scanner sc, List<Reader> readers) {
        List<Reader> readerList = new ArrayList<>();
        System.out.println("请输入学院:");
        String college = sc.next();
        for (Reader reader :readers) {
            if (reader.getCollege().equals(college)){
                readerList.add(reader);
            }
        }
        return readerList;
    }


    /**
     * 按书号查询图书借阅信息
     * @param sc 扫描器
     * @param borrowBooks 全部借阅信息
     * @param books 全部图书
     */
    private static void findBorrowBookByBookId(Scanner sc, List<BorrowBook> borrowBooks,List<Book> books) {
        List<BorrowBook> borrowBookList;//用于记录查询到的借阅信息
        Book book = getBookByBookId(sc, books);
        //判断该书号对应图书是否存在
        if (book != null){
            //图书存在,从借阅信息表获取该图书相关借阅信息
            borrowBookList = getBorrowBookByBookId(book.getBookId(),borrowBooks);
            //判断图书借阅表中是否有相关借阅信息
            if (borrowBookList.size() != 0){
                //存在借阅信息,输出
                borrowBookList.forEach(System.out::println);
                System.out.println(borrowBookList);
            }else {
                System.out.println("此书没有被借阅~~~");
            }
        }
    }

    /**
     * 获取该书号对应的借阅信息
     * @param bookId 书号
     * @param borrowBooks 全部借阅信息
     * @return 获取到的借阅信息
     */
    private static List<BorrowBook> getBorrowBookByBookId(String bookId, List<BorrowBook> borrowBooks) {
        List<BorrowBook> borrowBookList = new ArrayList<>();
        for (BorrowBook b:borrowBooks) {
            if (b.getBookId().equals(bookId)){
                borrowBookList.add(b);
            }
        }
        return borrowBookList;
    }

    /**
     * 按学号查询图书借阅信息
     * @param sc 扫描器
     * @param borrowBooks 全部图书借阅信息
     * @param readers 全部读者
     */
    private static void findBorrowByStudentId(Scanner sc, List<BorrowBook> borrowBooks, List<Reader> readers) {
        List<BorrowBook> borrowBookList;// 用来存储该学生的借阅信息
        System.out.println("请输入学号:");
        String studentId = sc.next();
        Reader reader = getReaderByStudentId(studentId,readers);
        //判断读者是否存在
        if (reader != null){
            //存在,从图书借阅表中获取该读者相关借阅记录
            borrowBookList = getBorrowBooksByStudentId(studentId,borrowBooks);
            //判断图书借阅表中是否存在与此读者相关的记录
            if (borrowBookList.size() != 0){
                //存在,输出展示
                borrowBookList.forEach(System.out::println);
            }else {
                //不存在
                System.out.println("此用户没有借阅图书:");
            }
        }else {
            //读者不存在
            System.out.println("此用户不存在!!!");
        }
    }

    /**
     * 图书归还
     * @param sc 扫描器
     * @param borrowBooks 全部结束信息
     * @param io io方法
     * @param books 全部图书
     * @param readers 全部读者
     */
    private static void returnBook(Scanner sc, List<BorrowBook> borrowBooks, IO io, List<Book> books, List<Reader> readers) throws IOException {
        System.out.println("请输入学号:");
        String studentId = sc.next();
        Reader reader = getReaderByStudentId(studentId,readers);
        if (reader != null){
            List<BorrowBook> borrowBook = getBorrowBooksByStudentId(studentId,borrowBooks);
            if (borrowBook.size() != 0){
                while (true){
                    Book book = getBookByBookId(sc, books);
                    if (book != null){
                        for (BorrowBook b:borrowBook) {
                            if (b.getBookId().equals(book.getBookId())){
                                int index = returnIndexByBookIdAndStudentId(studentId,book.getBookId(),borrowBooks);
                                Date date = new Date();
                                long returnTime = date.getTime();
                                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE");
                                String returnDate = simpleDateFormat.format(returnTime);
                                b.setReturnDate(returnDate);
                                borrowBooks.set(index,b);
                                book.setNumber(book.getNumber() + 1);
                                index = returnIndexByBookId(book.getBookId(),books);
                                books.set(index,book);
                                io.shoreBook(books);
                                io.storeBorrowBook(borrowBooks);
                                System.out.println("图书已成功归还~~~");
                            }
                        }
                        System.out.println("用户没有借阅此书~~~");
                    }else {
                        System.out.println("此书号不存在~~~");
                    }
                    System.out.println("是否继续还书?y/n");
                    while (true){
                        String select = sc.next();
                        if (select.equals("y")||select.equals("Y")){
                            break;
                        }else if (select.equals("n")||select.equals("N")){
                            return;
                        }else {
                            System.out.println("抱歉,您的输入系统不能识别,请重新输入~~~");
                        }
                    }
                }
            }else {
                System.out.println("此用户没有借阅图书~~~");
            }
        }else {
            System.out.println("此用户不存在,无法进行还书操作~~~");
        }
    }

    private static int returnIndexByBookIdAndStudentId(String studentId, String bookId, List<BorrowBook> borrowBooks) {
        for (int i = 0; i < borrowBooks.size(); i++) {
            BorrowBook borrowBook = borrowBooks.get(i);
            if (borrowBook.getBookId().equals(bookId) && borrowBook.getStudentId().equals(studentId)){
                return i;
            }
        }
        return -1;
    }

    /**
     * 获取该学号对应学生的借阅信息
     * @param studentId 学号
     * @param borrowBooks 全部借阅信息
     * @return 获取到的借阅信息
     */
    private static List<BorrowBook> getBorrowBooksByStudentId(String studentId, List<BorrowBook> borrowBooks) {
        List<BorrowBook> borrowBookList = new ArrayList<>();//用来存储查询到的该读者的借阅信息
        for (BorrowBook borrowBook:borrowBooks) {
            if (borrowBook.getStudentId().equals(studentId)){
                borrowBookList.add(borrowBook);
            }
        }
        return borrowBookList;
    }


    /**
     * 图书借阅
     * @param sc 扫描器
     * @param borrowBooks 全部图书借阅信息
     * @param io io方法
     * @param books 全部图书
     * @param readers 全部读者
     * @throws IOException 异常
     */
    private static void borrowBook(Scanner sc, List<BorrowBook> borrowBooks, IO io,List<Book> books,List<Reader> readers) throws IOException {
        System.out.println("请输入学号:");
        String studentId = sc.next();
        Reader reader = getReaderByStudentId(studentId,readers);
        //判断此学号对应读者是否存在
        if (reader != null){
            //存在
            while (true){
                Book book = getBookByBookId(sc,books);
                //判断读者要借阅的书是否存在
                if (book != null){
                    //存在,判断是否还有库存
                    if (book.getNumber() > 1){
                        //还有库存
                        BorrowBook borrowBook = new BorrowBook();
                        borrowBook.setStudentId(studentId);//在借阅信息表中录入学号
                        borrowBook.setStudentName(reader.getStudentName());//在借阅信息中录入读者姓名
                        borrowBook.setBookId(book.getBookId());//在借阅信息表中录入书号
                        borrowBook.setBookName(book.getBookName());//在借阅信息表中录入书名
                        Date date = new Date();
                        long borrowTime = date.getTime();//获取当前时间毫秒值
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE");//格式化
                        String borrowDate = simpleDateFormat.format(borrowTime);
                        borrowBook.setBorrowDate(borrowDate);//将借书时间录入借阅信息表
                        long shouldReturnTime = borrowTime + 3*24*60*60*1000;
                        String shouldReturnDate = simpleDateFormat.format(shouldReturnTime);
                        borrowBook.setShouldReturnDate(shouldReturnDate);//设置三天后为应还时间
                        borrowBooks.add(borrowBook);
                        book.setNumber(book.getNumber() - 1);//对应书的数量-1
                        int index = returnIndexByBookId(book.getBookId(),books);
                        books.set(index,book);
                        io.shoreBook(books);//将图书信息存入文件
                        io.storeBorrowBook(borrowBooks);//将借阅信息存入文件
                        System.out.println("您的借书时间为:" + borrowDate);
                        System.out.println("请您在" + shouldReturnDate + "之前归还");
                        System.out.println("请问是否继续进行借阅操作?y/n");
                        while (true){
                            String select = sc.next();
                            if (select.equals("y")||select.equals("Y")){
                                System.out.println("请继续借阅操作~~~");
                            }else if (select.equals("n")||select.equals("N")){
                                return;
                            }else {
                                System.out.println("对不起,系统识别不出您的输入,请重新输入~~~");
                            }
                        }
                    }
                }else {
                    System.out.println("此书不存在!!!");
                    System.out.println("请检测书号是否有误后重新输入~~~");
                }
            }
        } else {
            System.out.println("此用户不存在,无法进行借阅~~~");
        }
    }

    /**
     * 图书信息管理
     * @param sc 扫描器
     * @param books 全部图书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void booksInformationManage(Scanner sc, List<Book> books, IO io) throws IOException {
        while(true){
            System.out.println("================1、图书信息的添加================");
            System.out.println("================2、图书信息的查询================");
            System.out.println("================3、图书信息的修改================");
            System.out.println("================4、图书信息的删除================");
            System.out.println("================5、图书信息的排序================");
            System.out.println("================6、图书信息的显示================");
            System.out.println("================0、退出功能=====================");
            int select = sc.nextInt();
            switch(select){
                case 1:
                    addBook(sc,books,io);
                    break;
                case 2:
                    findBook(sc,books);
                    break;
                case 3:
                    modifyBook(sc,books,io);
                    break;
                case 4:
                    deleteBook(sc,books,io);
                    break;
                case 5:
                    sortBook(books,io);
                    break;
                case 6:
                    printBook(books);
                    break;
                case 0:
                    return;
            }
        }
    }

    /**
     * 图书信息的显示
     * @param books 全部图书
     */
    private static void printBook(List<Book> books) {
        if (books.size() == 0){
            System.out.println("系统中还没有录入图书信息~~~");
            return;
        }
        books.forEach(System.out::println);
    }

    /**
     * 图书信息的排序
     * @param books 全部图书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void sortBook(List<Book> books, IO io) throws IOException {
        Collections.sort(books);
        io.shoreBook(books);
        books.forEach(System.out::println);
    }

    /**
     * 图书信息的删除
     * @param sc 扫描器
     * @param books 全部图书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void deleteBook(Scanner sc, List<Book> books, IO io) throws IOException {
        while (true){
            System.out.println("================1、按书号进行删除================");
            System.out.println("================2、按书名进行删除================");
            System.out.println("================0、退出删除功能==================");
            System.out.println("请选操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    deleteBookByBookId(sc,books,io);
                    break;
                case 2:
                    deleteBookByBookName(sc,books,io);
                    break;
                case 0:
                    return;
            }
        }
    }

    /**
     * 按书名进行删除
     * @param sc 扫描器
     * @param books 全部图书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void deleteBookByBookName(Scanner sc, List<Book> books, IO io) throws IOException {
        Book book = getBookByBookName(sc,books);
        if (book != null){
            int index = returnIndexByBookName(book.getBookName(), books);
            books.remove(index);
            io.shoreBook(books);
            System.out.println("删除成功~~~");
        }else {
            System.out.println("此书不存在,不能进行删除操作!!!");
        }
    }

    /**
     * 按书号进行删除
     * @param sc 扫描器
     * @param books 全部的书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void deleteBookByBookId(Scanner sc, List<Book> books, IO io) throws IOException {
        Book book = getBookByBookId(sc,books);
        if (book != null){
            int index = returnIndexByBookId(book.getBookId(),books);
            books.remove(index);
            io.shoreBook(books);
            System.out.println("删除成功~~~");
        }else {
            System.out.println("此书不存在,不能进行删除操作!!!");
        }
    }

    /**
     * 图书信息的修改
     * @param sc 扫描器
     * @param books 全部图书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void modifyBook(Scanner sc, List<Book> books, IO io) throws IOException {
        while (true){
            System.out.println("===============1、按书号进行修改===============");
            System.out.println("===============2、按书名进行修改===============");
            System.out.println("===============0、退出修改功能=================");
            System.out.println("请选择修改方式:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    modifyBookByBookId(sc,books,io);
                    break;
                case 2:
                    modifyBookBookByBookName(sc,books,io);
                    break;
                case 0:
                    return;
            }
        }
    }

    /**
     * 按书号进行修改
     * @param sc 扫描器
     * @param books 全部图书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void modifyBookBookByBookName(Scanner sc, List<Book> books, IO io) throws IOException {
        Book book = getBookByBookName(sc,books);
        if (book != null){
            int index = returnIndexByBookName(book.getBookName(),books);
            while(true){
                System.out.println("===============1、修改书名===============");
                System.out.println("===============2、修改书号===============");
                System.out.println("===============3、修改作者===============");
                System.out.println("===============4、修改出版社==============");
                System.out.println("===============5、修改出版社日期===========");
                System.out.println("===============6、修改库存数量============");
                System.out.println("===============7、修改定价===============");
                System.out.println("===============0、结束对此书的修改==========");
                System.out.println("请选择要修改的内容:");
                int select = sc.nextInt();
                switch (select){
                    case 1:
                        System.out.println("请输入书名:");
                        String newBookName = sc.next();
                        book.setBookName(newBookName);
                        break;
                    case 2:
                        System.out.println("请输入书号:");
                        String newBookId = sc.next();
                        book.setBookId(newBookId);
                        break;
                    case 3:
                        System.out.println("请输入作者:");
                        String newWriter = sc.next();
                        book.setWriter(newWriter);
                        break;
                    case 4:
                        System.out.println("请输入出版社:");
                        String newPublishingHouse = sc.next();
                        book.setPublishingHouse(newPublishingHouse);
                        break;
                    case 5:
                        System.out.println("请输入出版日期:");
                        String newPublicationDate = sc.next();
                        book.setPublicationDate(newPublicationDate);
                        break;
                    case 6:
                        System.out.println("请输入库存数量:");
                        int newNumber = sc.nextInt();
                        book.setNumber(newNumber);
                        break;
                    case 7:
                        System.out.println("请输入定价:");
                        double newPrice = sc.nextDouble();
                        book.setPrice(newPrice);
                        break;
                    case 0:
                        books.set(index,book);
                        io.shoreBook(books);
                        return;
                }
            }
        }
        else {
            System.out.println("此书不存在,不能进行修改操作!!!");
        }
    }

    /**
     * 根据书名查找对应下标
     * @param bookName 书名
     * @param books 全部的书
     * @return 对应下标或-1
     */
    private static int returnIndexByBookName(String bookName, List<Book> books) {
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            if (book.getBookName().equals(bookName)){
                return i;
            }
        }
        return -1;
    }

    /**
     * 根据书名查找图书
     * @param sc 扫描器
     * @param books 全部的书
     * @return 对应图书|null
     */
    private static Book getBookByBookName(Scanner sc, List<Book> books) {
        System.out.println("请输入书名:");
        String bookName = sc.next();
        for (Book book: books) {
            if (book.getBookName().equals(bookName)){
                return book;
            }
        }
        return null;
    }

    /**
     * 按书号对图书信息进行修改
     * @param sc 扫描器
     * @param books 全部图书
     * @param io io方法
     */
    private static void modifyBookByBookId(Scanner sc, List<Book> books, IO io) throws IOException {
        Book book = getBookByBookId(sc,books);
        if (book != null){
            int index = returnIndexByBookId(book.getBookId(),books);
            while(true){
                System.out.println("===============1、修改书名===============");
                System.out.println("===============2、修改书号===============");
                System.out.println("===============3、修改作者===============");
                System.out.println("===============4、修改出版社==============");
                System.out.println("===============5、修改出版社日期===========");
                System.out.println("===============6、修改库存数量============");
                System.out.println("===============7、修改定价===============");
                System.out.println("===============0、结束对此书的修改==========");
                System.out.println("请选择要修改的内容:");
                int select = sc.nextInt();
                switch (select){
                    case 1:
                        System.out.println("请输入书名:");
                        String newBookName = sc.next();
                        book.setBookName(newBookName);
                        System.out.println("书名修改成功~~~");
                        break;
                    case 2:
                        System.out.println("请输入书号:");
                        String newBookId = sc.next();
                        book.setBookId(newBookId);
                        System.out.println("书号修改成功~~~");
                        break;
                    case 3:
                        System.out.println("请输入作者:");
                        String newWriter = sc.next();
                        book.setWriter(newWriter);
                        System.out.println("作者修改成功~~~");
                        break;
                    case 4:
                        System.out.println("请输入出版社:");
                        String newPublishingHouse = sc.next();
                        book.setPublishingHouse(newPublishingHouse);
                        System.out.println("出版社修改成功~~~");
                        break;
                    case 5:
                        System.out.println("请输入出版日期:");
                        String newPublicationDate = sc.next();
                        book.setPublicationDate(newPublicationDate);
                        System.out.println("出版日期修改成功~~~");
                        break;
                    case 6:
                        System.out.println("请输入库存数量:");
                        int newNumber = sc.nextInt();
                        book.setNumber(newNumber);
                        System.out.println("库存数量修改成功~~~");
                        break;
                    case 7:
                        System.out.println("请输入定价:");
                        double newPrice = sc.nextDouble();
                        book.setPrice(newPrice);
                        System.out.println("定价修改成功~~~");
                        break;
                    case 0:
                        books.set(index,book);
                        io.shoreBook(books);
                        return;
                }
            }
        }else {
            System.out.println("此书不存在,不能进行修改操作!!!");
        }
    }

    /**
     * 根据书号查询下标
     * @param bookId 书号
     * @param books 全部的书
     * @return 找到返回下标|找不到返回-1
     */
    private static int returnIndexByBookId(String bookId, List<Book> books) {
        for (int i = 0; i < books.size(); i++) {
            Book book = books.get(i);
            if (book.getBookId().equals(bookId)){
                return i;
            }
        }
        return -1;
    }

    /**
     * 查询图书
     * @param sc 扫描器
     * @param books 全部的书
     */
    private static void findBook(Scanner sc, List<Book> books) {
        while (true){
            System.out.println("===============1、根据书号查询===============");
            System.out.println("===============2、根据作者查询===============");
            System.out.println("===============3、根据出版社查询==============");
            System.out.println("===============0、退出查询===================");
            System.out.println("请选择查询方式:");
            int select = sc.nextInt();
            switch (select) {
                case 1:
                    Book book = getBookByBookId(sc, books);
                    if (book != null) {
                        System.out.println(book);
                    } else {
                        System.out.println("查询不到此书号对应的书!!!");
                    }
                    break;
                case 2:
                    List<Book> books1 = getBookByWriter(sc, books);
                    if (books1.size() == 0) {
                        System.out.println("查询不到此作者的作品!!!");
                    }else {
                        books1.forEach(System.out::println);
                    }
                    break;
                case 3:
                    List<Book> books2 = getBookByPublishingHouse(sc,books);
                    if (books2.size() != 0){
                        books2.forEach(System.out::println);
                    }else {
                        System.out.println("查询不到此出版社出版的图书!!!");
                    }
                    break;
                case 0:
                    return;
            }
        }
    }

    /**
     * 根据出版社来查询图书
     * @param sc 扫描器
     * @param books 全部的书
     * @return 出版社为publishingHouse的图书集合
     */
    private static List<Book> getBookByPublishingHouse(Scanner sc, List<Book> books) {
        System.out.println("请输入要查询的出版社:");
        String publishingHouse = sc.next();
        List<Book> books1 = new ArrayList<>();
        for (Book book:books) {
            if (book.getPublishingHouse().equals(publishingHouse)){
                books1.add(book);
            }
        }
        return books1;
    }

    /**
     * 根据作者查询图书
     * @param sc 扫描器
     * @param books 全部的书
     * @return 作者为writer的图书集合
     */
    private static List<Book> getBookByWriter(Scanner sc, List<Book> books) {
        System.out.println("请输入要查询的作者:");
        String writer = sc.next();
        List<Book> books1 = new ArrayList<>();
        for (Book book:books) {
            if (book.getWriter().equals(writer)){
                books1.add(book);
            }
        }
        return books1;
    }

    /**
     * 根据书号来查询图书
     * @param sc 扫描器
     * @param books 全部的书
     * @return book|null
     */
    private static Book getBookByBookId(Scanner sc, List<Book> books) {
        System.out.println("请输入要查询的书号:");
        String bookId = sc.next();
        for (Book book:books) {
            if (book.getBookId().equals(bookId)){
                return book;
            }
        }
        return null;
    }

    /**
     * 添加图书
     * @param sc 扫描器
     * * @param books 全部的书
     * @param io io方法
     * @throws IOException 异常
     */
    private static void addBook(Scanner sc, List<Book> books, IO io) throws IOException {
        while (true){
            Book b = new Book();
            char ch = 'y';
            System.out.println("请输入图书书号:");
            String bookId = sc.next();
            //判断书号是否重复
            for (Book book:books) {
                if (book.getBookId().equals(bookId)){
                    System.out.println("此书号已存在!!!");
                    ch = 'n';//重复
                    break;
                }
            }
            if (ch == 'y'){
                //书号没有重复
                b.setBookId(bookId);
                System.out.println("请输入书名:");
                String bookName = sc.next();
                b.setBookName(bookName);
                System.out.println("请输入数量:");
                int number = sc.nextInt();
                b.setNumber(number);
                System.out.println("请输入作者:");
                String writer = sc.next();
                b.setWriter(writer);
                System.out.println("请输入出版社:");
                String publishingHouse = sc.next();
                b.setPublishingHouse(publishingHouse);
                System.out.println("请输入出版时间:");
                String publicationDate = sc.next();
                b.setPublicationDate(publicationDate);
                System.out.println("请输入定价:");
                double price = sc.nextDouble();
                b.setPrice(price);
                books.add(b);
                System.out.println("添加成功~~~");
            }else {
                System.out.println("此书号已存在~~~");
            }

            while(true){
                System.out.println("是否继续添加?1---是;2---否");
                int select = sc.nextInt();
                if (select == 1){
                    break;
                }else if (select == 2){
                    io.shoreBook(books);//将新增的读者信息存入文件中
                    return;
                }else {
                    System.out.println("您的选择不能识别,请重新输入:");
                }
            }
        }
    }

    /**
     * 读者信息管理
     * @param sc 扫描器
     * @param readers 全部读者
     * @param io io方法
     * @throws IOException 异常
     */
    private static void readersInformationManage(Scanner sc,List<Reader> readers,IO io) throws IOException {
        while(true){
            System.out.println("================1、读者信息的添加================");
            System.out.println("================2、读者信息的查询================");
            System.out.println("================3、修改读者的信息================");
            System.out.println("================4、删除读者的信息================");
            System.out.println("================5、读者信息的排序================");
            System.out.println("================6、读者信息的显示================");
            System.out.println("================0、退出此界面=====================");
            System.out.println("请选择操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    addReader(sc,readers,io);//读者信息的添加
                    break;
                case 2:
                    findReader(sc,readers);//读者信息的查询
                    break;
                case 3:
                    modifyReader(sc,readers,io);//修改读者的信息
                    break;
                case 4:
                    deleteReader(sc,readers,io);//删除读者的信息
                    break;
                case 5:
                    sortReader(readers,io);//读者信息的排序
                    break;
                case 6:
                    printReader(readers);//读者信息的显示
                    break;
                case 0://退出此界面
                    return;
            }
        }
    }

    /**
     * 读者信息的显示
     * @param readers 全部读者
     */
    private static void printReader(List<Reader> readers) {
        if (readers.size() == 0){
            System.out.println("系统中还每有录入读者信息~~~");
        }
        readers.forEach(System.out::println);
    }

    /**
     * 按学号+姓名进行读者的删除
     * @param sc    扫描器
     * @param readers   全部的读者
     * @param io    io方法
     * @throws IOException  异常
     */
    private static void deleteReader(Scanner sc, List<Reader> readers, IO io) throws IOException {
        while (true) {
            System.out.println("请输入学号:");
            String studentId = sc.next();
            Reader r = getReaderByStudentId(studentId, readers);
            //判断此学号对对应读者是否存在
            if (r != null) {
                //存在
                System.out.println("请输入姓名:");
                String studentName = sc.next();
                //验证姓名是否一致
                if (r.getStudentName().equals(studentName)) {
                    //一致,进行删除操作
                    int index = returnIndexByStudentId(studentId, readers);//获取此读者的下标
                    readers.remove(index);//进行删除操作
                    io.storeReader(readers);//将删除后的读者信息存入文档
                    System.out.println("删除完成~~~");
                    return;
                }else {
                    //不一致,选择是否进行删除操作
                    System.out.println("对不起,您输入的名字不正确!!!");
                    System.out.println("是否继续验证操作?y/n");
                    while(true){
                        String select = sc.next();
                        if (select.equals("y")||select.equals("Y")){
                            break;
                        }else if (select.equals("n") || select.equals("N")){
                            return;
                        }else {
                            System.out.println("您的输入不能识别,请重新输入!!!");
                        }
                    }
                }
            }else {
                //不存在,选择是否进行删除操作
                System.out.println("查无此人!!!");
                System.out.println("是否继续删除操作?y/n");
                while(true) {
                    String select = sc.next();
                    if (select.equals("y") || select.equals("Y")) {
                        break;
                    } else if (select.equals("n") || select.equals("N")) {
                        return;
                    } else {
                        System.out.println("您的输入不能识别,请重新输入~~~");
                    }
                }
            }
        }
    }

    /**
     * 根据学院和学号进行排序操作
     * @param readers   全部的读者
     * @param io    io方法
     * @throws IOException  异常
     */
    private static void sortReader(List<Reader> readers, IO io) throws IOException {
        Collections.sort(readers);
        io.storeReader(readers);
        readers.forEach(System.out::println);
    }

    /**
     * 添加读者信息
     * @param sc    扫描器
     * @param readers   全部的读者
     * @param io    io方法
     * @throws IOException  异常
     */
    private static void addReader(Scanner sc, List<Reader> readers,IO io) throws IOException {
        while(true){
            Reader r = new Reader();
            System.out.println("请输入学号:");
            String studentId = sc.next();
            //判断学号是否重复
            Reader reader = getReaderByStudentId(studentId,readers);
            if (reader != null){
                System.out.println("此学号已存在~~~");
            }else {
                r.setStudentId(studentId);
                System.out.println("请输入姓名:");
                String studentName = sc.next();
                r.setStudentName(studentName);
                System.out.println("请输入学院:");
                String college = sc.next();
                r.setCollege(college);
                System.out.println("请输入专业:");
                String specialty = sc.next();
                r.setSpecialty(specialty);
                System.out.println("请输入班级:");
                System.out.println("格式:20xx级xx班");
                String gradeAndClass = sc.next();
                r.setGradeAndClass(gradeAndClass);
                readers.add(r);
            }
            System.out.println("请问是否继续添加?y/n");
            while (true){
                String select = sc.next();
                if (select.equals("n") || select.equals("N")){
                    io.storeReader(readers);//将新添加的信息存入文件中
                    return;
                }else if (select.equals("y") || select.equals("Y")){
                    System.out.println("请继续添加操作~~~");
                    break;
                }else {
                    System.out.println("抱歉,系统不能识别你的输入,请重新输入~~~");
                }
            }
        }
    }

    /**
     * 按学号+姓名进行读者的修改
     * @param sc 扫描器
     * @param readers   全部读者
     * @param io    io方法
     * @throws IOException  异常
     */
    private static void modifyReader(Scanner sc, List<Reader> readers,IO io) throws IOException {
        while(true){
            System.out.println("请输入学号:");
            String studentId = sc.next();
            Reader r = getReaderByStudentId(studentId,readers);
            if (r != null){
                System.out.println("请输入姓名:");
                String studentName = sc.next();
                if (r.getStudentName().equals(studentName)){
                    System.out.println("可以进行修改操作了~~~");
                    int index = returnIndexByStudentId(studentId,readers);
                    while (true){
                        System.out.println("================1、修改学号================");
                        System.out.println("================2、修改姓名================");
                        System.out.println("================3、修改学院================");
                        System.out.println("================4、修改专业================");
                        System.out.println("================5、修改班级================");
                        System.out.println("================0、结束修改操作=============");
                        int select = sc.nextInt();
                        switch (select){
                            case 1:
                                System.out.println("请输入学号:");
                                String newStudentId = sc.next();
                                r.setStudentId(newStudentId);
                                break;
                            case 2:
                                System.out.println("请输入姓名:");
                                String studentName1 = sc.next();
                                r.setStudentName(studentName1);
                                break;
                            case 3:
                                System.out.println("请输入学院:");
                                String college = sc.next();
                                r.setCollege(college);
                                break;
                            case 4:
                                System.out.println("请输入专业:");
                                String specialty = sc.next();
                                r.setGradeAndClass(specialty);
                                break;
                            case 5:
                                System.out.println("请输入班级:");
                                System.out.println("格式:20xx级xx班");
                                String gradeAndClass = sc.next();
                                r.setGradeAndClass(gradeAndClass);
                                break;
                            case 0:
                                readers.set(index,r);
                                io.storeReader(readers);//将修改后的信息存入文件中
                                return;
                        }
                    }
                }else {
                    System.out.println("对不起,您输入的名字不正确!!!");
                    System.out.println("是否继续验证操作?y/n");
                    while(true){
                        String select = sc.next();
                        if (select.equals("y")||select.equals("Y")){
                            break;
                        }else if (select.equals("n") || select.equals("N")){
                            return;
                        }else {
                            System.out.println("您的输入不能识别,请重新输入!!!");
                        }
                    }
                }
            }else {
                System.out.println("对不起,此用户不存在!!!");
                System.out.println("是否继续修改操作?y/n");
                while(true){
                    String select = sc.next();
                    if (select.equals("y")||select.equals("Y")){
                        break;
                    }else if (select.equals("n") || select.equals("N")){
                        return;
                    }else {
                        System.out.println("您的输入不能识别,请重新输入~~~");
                    }
                }
            }
        }
    }

    /**
     * 根据学号查询Reader在读者信息表中的位置
     * @param studentId 需要用于查询位置的学号
     * @param readers   读者信息表
     * @return 目标读者序号|-1
     */
    private static int returnIndexByStudentId(String studentId, List<Reader> readers) {
        for (int i = 0; i < readers.size(); i++) {
            Reader r = readers.get(i);
            if (r.getStudentId().equals(studentId)){
                return i;
            }
        }
        return -1;
    }

    /**
     * 读者信息的查询
     * @param sc 扫描器
     * @param readers 全部读者
     */
    private static void findReader(Scanner sc, List<Reader> readers) {
        while (true){
            System.out.println("请选择你的查找方式:");
            System.out.println("===============1、根据学号查询===============");
            System.out.println("===============2、根据姓名查询===============");
            System.out.println("===============3、根据学院查询===============");
            System.out.println("===============4、根据专业查询===============");
            System.out.println("===============5、根据班级查询===============");
            System.out.println("===============0、退出查询功能===============");
            System.out.println("请选操作:");
            int select = sc.nextInt();
            switch (select){
                case 1:
                    System.out.println("请输入学号:");
                    String studentId = sc.next();
                    Reader r = getReaderByStudentId(studentId,readers);
                    if (r != null){
                        System.out.println(r);
                    }else {
                        System.out.println("查无此人!!!");
                    }
                    break;
                case 2:
                    System.out.println("请输入姓名:");
                    String studentName = sc.next();
                    List<Reader> readers1 = getReaderByStudentName(studentName,readers);
                    if (readers1.size() != 0){
                        for (Reader reader: readers1) {
                            System.out.println(reader);
                        }
                    }else {
                        System.out.println("查无此人!!!");
                    }
                    break;
                case 3:
                    System.out.println("请输入学院:");
                    String college = sc.next();
                    List<Reader> readers2 =getReaderByCollege(college,readers);
                    if (readers2.size() != 0){
                        for (Reader reader: readers2) {
                            System.out.println(reader);
                        }
                    }else {
                        System.out.println("查找不到此学院相关信息!!!");
                    }
                    break;
                case 4:
                    System.out.println("请输入专业:");
                    String specialty = sc.next();
                    List<Reader> readers3 = getReaderBySpecially(specialty,readers);
                    if (readers3.size() != 0){
                        for (Reader reader: readers3) {
                            System.out.println(reader);
                        }
                    }else {
                        System.out.println("查找不到辞职专业相关信息!!!");
                    }
                    break;
                case 5:
                    System.out.println("请输入班级:");
                    System.out.println("格式:20xx级xx班");
                    String gradeAndClass = sc.next();
                    List<Reader> readers4 = getReaderByGradeAndClass(gradeAndClass,readers);
                    if (readers4.size() != 0){
                        for (Reader reader: readers4) {
                            System.out.println(reader);
                        }
                    }else {
                        System.out.println("查询不到此班级相关信息!!!");
                    }
                    break;
                case 0:
                    return;
            }
        }
    }

    /**
     * 根据班级查询Reader
     * @param gradeAndClass 班级
     * @param readers 全部读者
     * @return 查询到的读者
     */
    private static List<Reader> getReaderByGradeAndClass(String gradeAndClass, List<Reader> readers) {
        List<Reader> readers1 = new ArrayList<>();
        for (Reader reader:readers) {
            if (reader.getGradeAndClass().equals(gradeAndClass)){
                readers1.add(reader);
            }
        }
        return readers1;
    }

    /**
     * 根据学院查询Reader
     * @param specialty 学院
     * @param readers   所有的Reader
     * @return  一个所属专业为specialty的集合
     */
    private static List<Reader> getReaderBySpecially(String specialty, List<Reader> readers) {
        List<Reader> readers1 = new ArrayList<>();
        for (Reader reader:readers) {
            if (reader.getSpecialty().equals(specialty)){
                readers1.add(reader);
            }
        }
        return readers1;
    }

    /**
     * 根据学院查询Reader
     * @param college   学院
     * @param readers   所有的Reader
     * @return  一个所属学院为college的集合
     */
    private static List<Reader> getReaderByCollege(String college, List<Reader> readers) {
        List<Reader> readers1 = new ArrayList<>();
        for (Reader reader: readers) {
            if (reader.getCollege().equals(college)){
                readers1.add(reader);
            }
        }
        return readers1;
    }

    /**
     * 根局姓名查询Reader
     * @param studentName   要查询的姓名
     * @param readers   全部Reader
     * @return  一个姓名为studentName的集合
     */
    private static List<Reader> getReaderByStudentName(String studentName, List<Reader> readers) {
        List<Reader> readers1 = new ArrayList<>();
        for (Reader reader:readers) {
            if (reader.getStudentName().equals(studentName)){
                readers1.add(reader);
            }
        }
        return readers1;
    }

    /**
     * 根据学号查询出一个Reader对象
     * @param studentId 学号
     * @param readers   全部Reader
     * @return  Reader|null
     */
    private static Reader getReaderByStudentId(String studentId, List<Reader> readers) {
        for (Reader reader:readers) {
            if (reader.getStudentId().equals(studentId)){
                return reader;
            }
        }
        return null;
    }
}

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值