图书馆管理系统(java+IO流)

目录

一、项目功能:

1、读者

2、管理员

3、日志

4、结构

二、代码:

1、定义的类:

(1)book类

(2)BookType

(3)BorrowBook

(4)Reader

(5)ReaderType

(6)User

2、方法

(1)IO方法

(2)主方法和具体功能实现方法

三,总结


一、项目功能:

1、读者

(1)图书信息查询:图书信息查询功能。可以浏览所有图书信息和检索特定条件的图书的信息。

(2)图书借阅:包括图书借阅和图书归还功能。图书借阅功能,先输入读者的编号,然后输入要借阅的图书的ISBN,记录系统当前时间即借阅时间;图书归还功能,输入读者的编号,选择其名下已借阅的图书,判断当前日期即归还日期与借阅日期的差值是否超过了规定的期限,计算罚金,从而进行图书的归还操作。

2、管理员

(1)读者信息管理:包括读者信息添加和读者信息查询与修改功能。用户登录成功之后,可以浏览所有读者的信息,也可以检索特定读者的信息;同时,可以对读者信息进行维护,包括增加、删除及修改。

(2)图书信息管理:包括图书信息添加和图书信息查询与修改功能。用户登录成功之后,可以浏览所有图书信息和检索特定图书的信息;也可以对图书信息进行维护。包括添加图书、删除图书以及修改图书信息。

(3)基础信息维护:包括图书类别设置、读者类别设置及罚金设置。图书类别设置,可以对图书的类别进行增加、删除、修改和查询;读者类别设置可以对读者的类别进行增加、删除、修改和查询;罚金设置,可以指定超期一天的罚金标准。

(4)用户管理:包括修改密码、用户添加和删除。修改密码,是指当前用户修改自己的密码;用户添加和删除,是对新增和去除系统用户时对用户信息的维护。

3、日志

         日志功能:记录每一步操作和操作时系统时间(精确到毫秒),保存到文件中。

4、结构

二、代码:

1、定义的类:

(1)book类

import java.io.Serializable;

public class Book implements Serializable {

    private String bookName;//书名
    private String author;//作者
    private String publish;//出版社
    private String publishDate;//出版日期
    private String printTime;//印刷次数
    private String unitPrice;//单价
    private String typeId;//类型id
    private String typeName;//类型名
    private String ISBN;//ISBN

    public String getStockNum() {
        return stockNum;
    }

    public void setStockNum(String stockNum) {
        this.stockNum = stockNum;
    }

    private String stockNum;//库存

    public String getBookName() {
        return bookName;
    }

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

    public String getAuthor() {//学生
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublish() {
        return publish;
    }

    public void setPublish(String publish) {
        this.publish = publish;
    }

    public String getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(String publishDate) {
        this.publishDate = publishDate;
    }

    public String getPrintTime() {
        return printTime;
    }

    public void setPrintTime(String printTime) {
        this.printTime = printTime;
    }

    public String getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(String unitPrice) {
        this.unitPrice = unitPrice;
    }

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public Book(String bookName, String author, String publish, String publishDate, String printTime, String unitPrice, String typeId, String ISBN, String typeName, String stockNum) {

        this.setBookName(bookName);
        this.setAuthor(author);
        this.setISBN(ISBN);
        this.setPublish(publish);
        this.setPublishDate(publishDate);
        this.setPrintTime(printTime);
        this.setUnitPrice(unitPrice);
        this.setTypeId(typeId);
        this.setTypeName(typeName);
        this.setStockNum(stockNum);

    }

    public String toString() {    //重写tostring方法
        return "bookName=" + bookName + " " + "ISBN=" + ISBN + " " + "author=" + author + " " + "publish=" + publish + " " +
                "publishYear=" + publishDate + " " + "typeId=" + typeId + " " + "typeName=" + typeName + " " + "stockNum=" + stockNum;
    }

}

(2)BookType

import java.io.Serializable;

public class BookType implements Serializable {
    private String typeId;//书籍类型
    private String typeName;//类型名

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public BookType(String typeId, String typeName) {
        this.setTypeId(typeId);
        this.setTypeName(typeName);
    }

    public String toString() {
        return "typeId=" + typeId + " " + "typeName=" + typeName;
    }

}

(3)BorrowBook

import java.io.Serializable;

public class BorrowBook implements Serializable {

    private String bookName;//书名
    private String ISBN;//书籍isbn
    private String borrowDate;//借阅日期
    private String returnDate;//没用到
    private String fine;//没用到
    private String readerId;//读者id

    public String getISBN() {
        return ISBN;
    }

    public void setISBN(String ISBN) {
        this.ISBN = ISBN;
    }

    public String getBorrowDate() {
        return borrowDate;
    }

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

    public String getReturnDate() {
        return returnDate;
    }

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

    public String getFine() {
        return fine;
    }

    public void setFine(String fine) {
        this.fine = fine;
    }

    public String getBookName() {
        return bookName;
    }

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


    public String getReaderId() {
        return readerId;
    }

    public void setReaderId(String readerId) {
        this.readerId = readerId;
    }

    public BorrowBook(String readerId, String bookName, String ISBN, String borrowDate) {
        this.setBookName(bookName);
        this.setBorrowDate(borrowDate);
        this.setISBN(ISBN);
        this.setReaderId(readerId);
    }

    public String toString() {
        return "readerId=" + readerId + " " + "bookName=" + bookName + " " + "ISBN=" + ISBN + " " + "borrowDate=" + borrowDate;
    }
}

(4)Reader

import java.io.Serializable;

public class Reader implements Serializable {
    private String maxBorrowNum;//最大借阅数量
    private String date;//注册日期
    private String type;//类型
    private String typename;//类型名
    private String readerId;//读者id
    private String name;//姓名
    private String sex;//性别
    private String age;//年龄
    private String dept;//部门
    private String phone;//电话号码
    private String maxBorrowDay;//最大借阅天数

    public String getMaxBorrowNum() {
        return maxBorrowNum;
    }

    public void setMaxBorrowNum(String maxBorrowNum) {
        this.maxBorrowNum = maxBorrowNum;
    }

    public String getTypename() {
        return typename;
    }

    public void setTypename(String typename) {
        this.typename = typename;
    }

    public String getMaxBorrowDay() {
        return maxBorrowDay;
    }

    public void setMaxBorrowDay(String maxBorrowDay) {
        this.maxBorrowDay = maxBorrowDay;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getReaderId() {
        return readerId;
    }

    public void setReaderId(String readerId) {
        this.readerId = readerId;
    }

    public Reader(String date, String typeid, String id, String age, String phone, String dept, String name, String sex, String maxBorrowDay, String maxBorrowNum, String typename) {
        this.setReaderId(id);
        this.setAge(age);
        this.setPhone(phone);
        this.setDept(dept);
        this.setName(name);
        this.setSex(sex);
        this.setType(typeid);
        this.setMaxBorrowDay(maxBorrowDay);
        this.setMaxBorrowNum(maxBorrowNum);
        this.setTypename(typename);
        this.setDate(date);
    }

    public String toString() {
        return "id=" + readerId + " " + "name=" + name + " " + "date=" + date + " " + "type=" + type + " " + "typename=" + typename +
                " " + "sex=" + sex + " " + "age=" + age + " " + "dept=" + dept + " " + "phone=" + phone+" "+"maxBorrowNum="+maxBorrowNum;
    }
}

(5)ReaderType

import java.io.Serializable;

public class ReaderType implements Serializable {
    private String typeId;//读者类型
    private String typeName;//类型名
    private String maxBorrowDay;//最大借阅天数
    private String maxBorrowNum;//最大借阅数量

    public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getMaxBorrowNum() {
        return maxBorrowNum;
    }

    public void setMaxBorrowNum(String maxBorrowNum) {
        this.maxBorrowNum = maxBorrowNum;
    }

    public String getMaxBorrowDay() {
        return maxBorrowDay;
    }

    public void setMaxBorrowDay(String maxBorrowDay) {
        this.maxBorrowDay = maxBorrowDay;
    }

    public ReaderType(String typeId, String typeName, String maxBorrowDay, String maxBorrowNum) {
        this.setMaxBorrowDay(maxBorrowDay);
        this.setTypeName(typeName);
        this.setMaxBorrowNum(maxBorrowNum);
        this.setTypeId(typeId);
    }

    public String toString() {
        return "typeId=" + typeId + " " + "typeName=" + typeName + " " + "maxBorrowDay=" + maxBorrowDay + " " + "maxBorrowNum=" + maxBorrowNum;
    }
}

(6)User

import java.io.Serializable;

public class User implements Serializable {

    private String id;//id
    private String name;//用户名
    private String password;//密码

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public User(String id, String na, String pa) {
        this.setId(id);
        this.setName(na);
        this.setPassword(pa);
    }

    public String toString() {
        return "Id=" + id + " " + "Name=" + name;
    }

}

2、方法

(1)IO方法

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

public class IO {
    /**
     * 将集合存储至本地txt文件中
     */
    private void store(List List, FileOutputStream fos) throws IOException {

        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(List);
        oos.close();
        fos.close();
        System.out.println("保存数据到本地成功");
    }
   //保存日志
    public void storeLog(String str) {
        try {
            FileWriter file = new FileWriter("txtFile/log.txt", true);

            file.write(str);// 往文件里写入字符串
            file.write("\r\n");
            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将序列化后的文件反序列化后使用对象
     */
    private Object load(FileInputStream fis) throws IOException {
        Object obj = new Object();
        try {

            ObjectInputStream ois = new ObjectInputStream(fis);
            try (fis; ois) {
                obj = ois.readObject();
                System.out.println("载入数据成功");

            } catch (IOException | ClassNotFoundException e) {
                System.out.println("载入数据失败");
            }
            return obj;
        } catch (FileNotFoundException fileNotFoundException) {
            System.out.println("未找到文件");
            return obj;
        } catch (EOFException eofException) {
            System.out.println("数据为空");
            return obj;
        }

    }
    //保存book集合
    public void storeBook(List<Book> List) throws IOException {
        FileOutputStream fos = new FileOutputStream("txtFile/book.txt");
        this.store(List, fos);
    }
    //读取book集合
    public List<Book> loadBook() throws IOException {
        FileInputStream fos = new FileInputStream("txtFile/book.txt");

        return (ArrayList<Book>) this.load(fos);


    }
    //保存集合
    public void storeReader(List<Reader> List) throws IOException {
        FileOutputStream fos = new FileOutputStream("txtFile/reader.txt");
        this.store(List, fos);
    }
    //读取集合
    public List<Reader> loadReader() throws IOException {
        FileInputStream fos = new FileInputStream("txtFile/reader.txt");
        return (ArrayList<Reader>) this.load(fos);
    }
    //保存集合
    public void storeUser(List<User> List) throws IOException {
        FileOutputStream fos = new FileOutputStream("txtFile/user.txt");
        this.store(List, fos);
    }
    //读取集合
    public List<User> loadUser() throws IOException {
        FileInputStream fos = new FileInputStream("txtFile/user.txt");

        return (ArrayList<User>) this.load(fos);

    }
    //保存集合
    public void storeReaderType(List<ReaderType> List) throws IOException {
        FileOutputStream fos = new FileOutputStream("txtFile/readerType.txt");
        this.store(List, fos);
    }
    //读取集合
    public List<ReaderType> loadReaderType() throws IOException {
        FileInputStream fos = new FileInputStream("txtFile/readerType.txt");

        return (ArrayList<ReaderType>) this.load(fos);


    }
    //保存集合
    public void storeBookType(List<BookType> List) throws IOException {
        FileOutputStream fos = new FileOutputStream("txtFile/bookType.txt");
        this.store(List, fos);
    }
    //读取集合
    public List<BookType> loadBookType() throws IOException {
        FileInputStream fos = new FileInputStream("txtFile/bookType.txt");

        return (ArrayList<BookType>) this.load(fos);

    }
    //保存罚金
    public void storeFine(Integer fine) {
        try {
            FileWriter file = new FileWriter("txtFile/fine.txt");
            String strFine = String.valueOf(fine);
            file.write(strFine);// 往文件里写入字符串
            file.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("罚金保存");
    }
    //读取罚金
    public String loadFine() {
        String fine = null;
        try {
            FileReader file = new FileReader("txtFile/fine.txt");
            BufferedReader i = new BufferedReader(file);
            fine = i.readLine();
            file.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();
            return fine;
        } catch (IOException e) {

            e.printStackTrace();
            return fine;
        }
        return fine;
    }
    //保存集合
    public void storeBorrowBook(List<BorrowBook> List) throws IOException {
        FileOutputStream fos = new FileOutputStream("txtFile/borrowBook.txt");
        this.store(List, fos);
    }
    //读取集合
    public List<BorrowBook> loadBorrowBook() throws IOException {
        FileInputStream fos = new FileInputStream("txtFile/borrowBook.txt");

        return (ArrayList<BorrowBook>) this.load(fos);

    }
    
}

(2)主方法和具体功能实现方法

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


public class Main {

    public static void main(String[] args) throws IOException, ParseException {

        List<Reader> Readers = new ArrayList<>();//读者
        List<Book> books = new ArrayList<>();//书籍
        List<User> users = new ArrayList<>();//管理员
        List<BookType> bookTypes = new ArrayList<>();//书籍类型
        List<ReaderType> readerTypes = new ArrayList<>();//读者类型
        List<BorrowBook> borrowBooks = new ArrayList<>();//借书
        IO io = new IO();

        Readers = io.loadReader();//读取数据
        books = io.loadBook();
        users = io.loadUser();
        bookTypes = io.loadBookType();
        readerTypes = io.loadReaderType();
        borrowBooks = io.loadBorrowBook();
        Scanner scanner = new Scanner(System.in);

        hello:
        while (true) {
            System.out.println("欢迎使用本系统");

            System.out.println("1、管理员");

            System.out.println("2、读者");

            System.out.println("3、退出");
            String choice = scanner.nextLine();
            switch (choice) {

                case "1":
                    login(users);//登录
                    start(Readers, readerTypes, books, bookTypes, users, borrowBooks);
                    break;
                case "2":
                    reader(Readers, borrowBooks, books);
                    break;
                case "3":
                    break hello;
                default:
                    System.out.println("没这个选项");
                    break;

            }
        }


    }

    //读者模块
    public static void reader(List<Reader> Readers, List<BorrowBook> borrowBooks, List<Book> books) throws ParseException, IOException {
        IO io = new IO();
        Scanner scanner = new Scanner(System.in);
        reader:
        while (true) {
            System.out.println("欢迎使用本系统");
            System.out.println("1、借还书");
            System.out.println("2、查书");
            System.out.println("3、退出");
            String choice = scanner.nextLine();
            switch (choice) {

                case "1":
                    manageBorrowBook(Readers, books, borrowBooks);
                    io.storeReader(Readers);
                    io.storeBorrowBook(borrowBooks);
                    break;
                case "2":
                    findBook(books);
                    break;
                case "3":
                    break reader;
                default:
                    System.out.println("没这个选项");
                    break;

            }
        }
    }

    //管理员模块
    public static void start(List<Reader> Readers, List<ReaderType> readerTypes, List<Book> books, List<BookType> bookTypes, List<User> users, List<BorrowBook> borrowBooks) throws IOException, ParseException {

        Scanner scanner = new Scanner(System.in);
        IO io = new IO();
        start:
        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、退出");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    manageReader(Readers, readerTypes);
                    io.storeReader(Readers);
                    break;
                case "2":
                    manageBook(books, bookTypes);
                    io.storeBook(books);
                    break;

                case "3":
                    manageBase(bookTypes, readerTypes);
                    io.storeBookType(bookTypes);
                    io.storeReaderType(readerTypes);

                    break;
                case "4":
                    manageUser(users);
                    io.storeUser(users);
                    break;
                case "5":
                    findBorrowBook(borrowBooks);
                    break;
                case "6":
                    break start;
                default:
                    System.out.println("没这个选项");
                    break;

            }
        }
    }

    //借阅信息查询
    public static void findBorrowBook(List<BorrowBook> borrowBooks) {
        storeLog("借阅信息管理");
        borrowBook:
        while (true) {
            System.out.println("借阅信息管理");
            System.out.println("1、输出全部");
            System.out.println("2、读者id查询");
            System.out.println("3、书籍isbn查询");
            System.out.println("4、退出");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    for (BorrowBook borrowBook : borrowBooks) {
                        System.out.println(borrowBook.toString());
                    }
                    break;
                case "2":
                    System.out.println("读者id:");
                    String readerId = scanner.nextLine();
                    for (BorrowBook borrowBook : borrowBooks) {
                        if (readerId.equals(borrowBook.getReaderId())) {
                            System.out.println(borrowBook.toString());
                        }
                    }
                    break;
                case "3":
                    System.out.println("ISBN:");
                    String isbn = scanner.nextLine();
                    for (BorrowBook borrowBook : borrowBooks) {
                        if (isbn.equals(borrowBook.getISBN())) {
                            System.out.println(borrowBook.toString());
                        }
                    }
                    break;

                case "4":
                    break borrowBook;
                default:
                    System.out.println("没这个选项");
                    break;
            }
        }
    }

    public static void manageReader(List<Reader> list, List<ReaderType> readerTypes) {//读者管理
        storeLog("读者信息管理");
        Reader:
        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、退出");
            Scanner scanner = new Scanner(System.in);
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    addReader(list, readerTypes);
                    break;
                case "2":
                    findReader(list);
                    break;
                case "3":
                    modifyReader(list, readerTypes);
                    break;
                case "4":
                    deleteReader(list);
                    break;
                case "5":
                    break Reader;
                default:
                    System.out.println("没这个选项");
                    break;
            }
        }
    }


    public static void manageBook(List<Book> list, List<BookType> bookTypes) {
        storeLog("图书信息管理");
        Book:
        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、退出");
            Scanner scanner = new Scanner(System.in);

            switch (scanner.nextLine()) {
                case "1":
                    addBook(list, bookTypes);
                    break;
                case "2":
                    findBook(list);
                    break;
                case "3":
                    modifyBook(list, bookTypes);
                    break;
                case "4":
                    deleteBook(list);
                    break;
                case "5":
                    break Book;
                default:
                    System.out.println("没这个选项");
                    break;
            }
        }

    }

    public static void manageBorrowBook(List<Reader> readers, List<Book> books, List<BorrowBook> borrowBooks) throws ParseException {
        storeLog("借还图书");
        borrowBook:
        while (true) {
            System.out.println("图书借阅管理");
            System.out.println("1、图书借阅");
            System.out.println("2、图书归还");
            System.out.println("3、退出");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    borrowBook(readers, books, borrowBooks);
                    break;
                case 2:
                    returnBook(readers, books, borrowBooks);
                    break;
                case 3:
                    break borrowBook;
                default:
                    System.out.println("没这个选项");
                    break;
            }
        }
    }

    public static void manageBase(List<BookType> bookTypes, List<ReaderType> readerTypes) {
        storeLog("基础信息管理");
        Base:
        while (true) {
            System.out.println("基础信息维护");
            System.out.println("1、图书类别设置");
            System.out.println("2、读者类别设置");
            System.out.println("3、罚金设置");
            System.out.println("4、退出");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            switch (choice) {
                case 1:
                    manageBookType(bookTypes);

                    break;
                case 2:
                    manageReaderType(readerTypes);

                    break;
                case 3:
                    editFine();
                    break;
                case 4:
                    break Base;
                default:
                    System.out.println("没这个选项");
                    break;

            }
        }
    }

    public static void manageBookType(List<BookType> list) {
        storeLog("书籍类型管理");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1、增 2、查 3、删");
        switch (scanner.nextLine()) {
            case "1":
                addBookType(list);
                break;
            case "2":
                for (BookType bookType : list) {
                    System.out.println(bookType.toString());
                }
                break;
            case "3":
                System.out.println("typeId");
                String typeId = scanner.nextLine();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getTypeId().equals(typeId)) {
                        list.remove(i);
                        break;
                    }
                }
                break;
            default:
                System.out.println("没这个选项");
                break;
        }
    }

    public static void manageReaderType(List<ReaderType> list) {
        storeLog("读者类型管理");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1、增 2、查 3、删");
        switch (scanner.nextLine()) {
            case "1":
                addReaderType(list);
                break;
            case "2":
                for (ReaderType readerType : list) {
                    System.out.println(readerType.toString());
                }
                break;
            case "3":
                System.out.println("typeId");
                String typeId = scanner.nextLine();

                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getTypeId().equals(typeId)) {
                        list.remove(i);
                        break;
                    }
                }
                break;
            default:
                System.out.println("没这个选项");
                break;
        }
    }

    public static void manageUser(List<User> users) {
        storeLog("用户信息管理");
        System.out.println("用户管理");
        System.out.println("1、修改密码");
        System.out.println("2、用户添加");
        System.out.println("3、用户删除");
        System.out.println("4、用户查询");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                editPassword(users);
                break;
            case 2:
                addUser(users);
                break;
            case 3:
                deleteUser(users);
                break;
            case 4:
                findUser(users);
                break;
            default:
                System.out.println("没这个选项");
                break;
        }
    }

    public static void findUser(List<User> users) {
        storeLog("用户信息查询");
        for (User user : users) {
            System.out.println(user.toString());
        }
    }

    public static void addReader(List<Reader> Readers, List<ReaderType> readerTypes) {
        storeLog("增加读者信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("id");
        String id;
        id:
        while (true) {
            id = scanner.nextLine();
            if (id.length() == 8) {
                for (Reader reader : Readers) {
                    if (id.equals(reader.getReaderId())) {
                        System.out.println("id已存在");
                        continue id;
                    }
                }
                break;
            } else {
                System.out.println("长度不规范");
            }
        }
        System.out.println("姓名");
        String name;
        while (true) {
            name = scanner.nextLine();
            if (name.equals("")) {
                System.out.println("姓名不能为空");
            } else {
                break;
            }
        }
        for (ReaderType readerType : readerTypes) {
            System.out.println(readerType.toString());
        }
        Integer readerTypeIndex;
        readerType:
        while (true) {
            System.out.println("类别");
            String typeId = scanner.nextLine();
            for (ReaderType readerType : readerTypes) {
                if (typeId.equals(readerType.getTypeId())) {
                    readerTypeIndex = readerTypes.indexOf(readerType);
                    break readerType;
                }
            }
            System.out.println("类型不存在");
        }
        String typeId = readerTypes.get(readerTypeIndex).getTypeId();
        String typeName = readerTypes.get(readerTypeIndex).getTypeName();
        String maxBorrowDay = readerTypes.get(readerTypeIndex).getMaxBorrowDay();
        String maxBorrowNum = readerTypes.get(readerTypeIndex).getMaxBorrowNum();
        System.out.println("年龄");
        String age = scanner.nextLine();
        System.out.println("电话");
        String phone = scanner.nextLine();
        System.out.println("部门");
        String dept = scanner.nextLine();
        System.out.println("性别");
        String sex = scanner.nextLine();
        System.out.println("日期");
        String date = scanner.nextLine();
        Readers.add(new Reader(date, typeId, id, age, phone, dept, name, sex, maxBorrowDay, maxBorrowNum, typeName));
    }

    public static void findReader(List<Reader> list) {

        Reader:
        while (true) {
            storeLog("查询读者信息");
            Scanner scanner = new Scanner(System.in);
            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("选择:");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    System.out.println("全部读者信息:");
                    for (Reader Reader : list) {
                        System.out.println(Reader.toString());
                    }
                    break;
                case "2":
                    System.out.println("读者:");
                    String id = scanner.nextLine();
                    for (Reader Reader : list) {
                        if (id.equals(Reader.getReaderId())) {
                            System.out.println(Reader.toString());
                        }
                    }
                    break;
                case "3":
                    System.out.println("读者姓名:");
                    String name = scanner.nextLine();
                    for (Reader Reader : list) {
                        if (name.equals(Reader.getName())) {
                            System.out.println(Reader.toString());
                        }
                    }
                    break;


                case "4":
                    System.out.println("读者部门:");
                    String dept = scanner.nextLine();
                    for (Reader Reader : list) {
                        if (dept.equals(Reader.getDept())) {
                            System.out.println(Reader.toString());
                        }
                    }
                    break;


                case "5":
                    System.out.println("读者类型");
                    String type = scanner.nextLine();
                    for (Reader Reader : list) {
                        if (type.equals(Reader.getType())) {
                            System.out.println(Reader.toString());
                        }
                    }
                    break;
                case "6":
                    break Reader;
                default:
                    System.out.println("没这个选项");
                    continue;
            }
        }
    }

    public static void modifyReader(List<Reader> list, List<ReaderType> readerTypes) {
        Scanner scanner = new Scanner(System.in);
        storeLog("修改读者信息");
        System.out.println("输入读者id");
        String readerId = scanner.nextLine();
        for (Reader Reader : list) {
            if (readerId.equals(Reader.getReaderId())) {
                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("6、性别");
                System.out.println("7、注册日期");
                switch (scanner.nextLine()) {
                    case "1":
                        System.out.println("姓名");
                        String name;
                        while (true) {
                            name = scanner.nextLine();
                            if (name.equals("")) {
                                System.out.println("姓名不能为空");
                            } else {
                                Reader.setName(name);
                                break;
                            }
                        }
                        break;
                    case "2":
                        for (ReaderType readerType : readerTypes) {
                            System.out.println(readerType.toString());
                        }

                        Integer readerTypeIndex;
                        readerType:
                        while (true) {
                            System.out.println("类别:");
                            String typeId = scanner.nextLine();
                            for (ReaderType readerType : readerTypes) {
                                if (typeId.equals(readerType.getTypeId())) {
                                    readerTypeIndex = readerTypes.indexOf(readerType);
                                    break readerType;
                                }
                            }
                            System.out.println("类型不存在");
                        }
                        Reader.setType(readerTypes.get(readerTypeIndex).getTypeId());
                        Reader.setTypename(readerTypes.get(readerTypeIndex).getTypeName());
                        Reader.setMaxBorrowDay(readerTypes.get(readerTypeIndex).getMaxBorrowDay());
                        Reader.setMaxBorrowNum(readerTypes.get(readerTypeIndex).getMaxBorrowNum());
                        break;
                    case "3":
                        System.out.println("年龄");
                        String age = scanner.nextLine();
                        Reader.setAge(age);
                        break;
                    case "4":
                        System.out.println("电话");
                        String phone = scanner.nextLine();
                        Reader.setPhone(phone);
                        break;
                    case "5":
                        System.out.println("部门");
                        String dept = scanner.nextLine();
                        Reader.setDept(dept);
                        break;
                    case "6":
                        System.out.println("性别");
                        String sex = scanner.nextLine();
                        Reader.setSex(sex);
                        break;
                    case "7":
                        System.out.println("日期");
                        String date = scanner.nextLine();
                        Reader.setDate(date);
                        break;
                    default:
                        System.out.println("没这个选项");
                        break;
                }
            }
        }
    }

    public static void addBook(List<Book> list, List<BookType> bookTypes) {
        storeLog("增加图书信息");
        Scanner scanner = new Scanner(System.in);

        String ISBN;
        ISBN:
        while (true) {
            System.out.println("ISBN:");
            ISBN = scanner.nextLine();
            for (Book book : list) {
                if (ISBN.equals(book.getISBN())) {
                    System.out.println("isbn已存在");
                    continue ISBN;
                }
            }
            break;
        }

        System.out.println("书名");
        String bookName = scanner.nextLine();

        for (BookType bookType : bookTypes) {
            System.out.println(bookType.toString());

        }
        Integer bookTypeIndex;
        bookType:
        while (true) {
            System.out.println("类别");
            String typeId = scanner.nextLine();
            for (BookType bookType : bookTypes) {
                if (typeId.equals(bookType.getTypeId())) {
                    bookTypeIndex = bookTypes.indexOf(bookType);
                    break bookType;
                }
            }
            System.out.println("类型不存在");
        }
        String typeId = bookTypes.get(bookTypeIndex).getTypeId();
        String typeName = bookTypes.get(bookTypeIndex).getTypeName();
        System.out.println("作者");
        String author = scanner.nextLine();
        System.out.println("出版社");
        String publish = scanner.nextLine();
        System.out.println("出版日期");
        String publishDate = scanner.nextLine();
        System.out.println("印刷次数");
        String printTime = scanner.nextLine();
        System.out.println("单价");
        String unitPrice = scanner.nextLine();
        System.out.println("库存");
        String stockNum = scanner.nextLine();
        list.add(new Book(bookName, author, publish, publishDate, printTime, unitPrice, typeId, ISBN, typeName, stockNum));
    }

    public static void findBook(List<Book> list) {
        Scanner scanner = new Scanner(System.in);
        findBook:
        while (true) {
            storeLog("查询图书信息");
            System.out.println("1、输出全部书籍信息");
            System.out.println("2、书籍ISBN");
            System.out.println("3、书名");
            System.out.println("4、图书类别");
            System.out.println("5、作者");
            System.out.println("6、出版社");
            System.out.println("选择:");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    System.out.println("全部书籍信息:");
                    for (Book book : list) {
                        System.out.println(book.toString());
                    }
                    break;
                case "2":
                    System.out.println("ISBN:");
                    String isbn = scanner.nextLine();
                    for (Book book : list) {
                        if (isbn.equals(book.getISBN())) {
                            System.out.println(book.toString());
                        }
                    }
                    break;
                case "3":
                    System.out.println("书名:");
                    String name = scanner.nextLine();
                    for (Book book : list) {
                        if (name.equals(book.getBookName())) {
                            System.out.println(book.getBookName() + book.getAuthor());
                        }
                    }
                    break;


                case "4":
                    System.out.println("图书类别");
                    String bookType = scanner.nextLine();
                    for (Book book : list) {
                        if (bookType.equals(book.getTypeName())) {
                            System.out.println(book.getBookName() + book.getAuthor());
                        }
                    }
                    break;


                case "5":
                    System.out.println("作者");
                    String author = scanner.nextLine();
                    for (Book book : list) {
                        if (author.equals(book.getAuthor())) {
                            System.out.println(book.toString());
                        }
                    }
                    break;
                case "6":
                    System.out.println("出版社");
                    String publish = scanner.nextLine();
                    for (Book book : list) {
                        if (publish.equals(book.getPublish())) {
                            System.out.println(book.toString());
                        }
                    }
                    break;
                default:
                    System.out.println("没这个选项");
                    break findBook;
            }
        }
    }

    public static void modifyBook(List<Book> list, List<BookType> bookTypes) {
        storeLog("修改图书信息");
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入isbn");
        String isbn = scanner.nextLine();
        for (Book book : list) {
            if (isbn.equals(book.getISBN())) {
                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("6、印刷次数");
                System.out.println("7、单价");
                System.out.println("8、库存");
                String choice = scanner.nextLine();
                switch (choice) {
                    case "1":
                        System.out.println("书名");
                        String name = scanner.nextLine();
                        book.setBookName(name);
                        break;
                    case "2":
                        for (BookType bookType : bookTypes) {
                            System.out.println(bookType.toString());

                        }
                        Integer bookTypeIndex;
                        bookType:
                        while (true) {
                            System.out.println("类别");
                            String typeId = scanner.nextLine();
                            for (BookType bookType : bookTypes) {
                                if (typeId.equals(bookType.getTypeId())) {
                                    bookTypeIndex = bookTypes.indexOf(bookType);
                                    break bookType;
                                }
                            }
                            System.out.println("类型不存在");
                        }
                        book.setTypeId(bookTypes.get(bookTypeIndex).getTypeId());
                        book.setTypeName(bookTypes.get(bookTypeIndex).getTypeName());
                        break;
                    case "3":
                        System.out.println("作者");
                        String author = scanner.nextLine();
                        book.setAuthor(author);
                        break;
                    case "4":
                        System.out.println("出版社");
                        String publish = scanner.nextLine();
                        book.setPublish(publish);
                        break;
                    case "5":
                        System.out.println("出版日期");
                        String publishDate = scanner.nextLine();
                        book.setPublishDate(publishDate);
                        break;

                    case "6":
                        System.out.println("印刷次数");
                        String printTime = scanner.nextLine();
                        book.setPrintTime(printTime);
                        break;
                    case "7":
                        System.out.println("单价");
                        String unitPrice = scanner.nextLine();
                        book.setUnitPrice(unitPrice);
                        break;
                    case "8":
                        System.out.println("库存");
                        String stockNum = scanner.nextLine();
                        book.setStockNum(stockNum);
                        break;
                    default:
                        System.out.println("没这个选项");
                        break;
                }
            }
        }
    }

    public static void login(List<User> users) {//登录
        storeLog("登录");
        Scanner scanner = new Scanner(System.in);
        login:
        while (true) {
            System.out.println("输入用户名");
            String userName = scanner.next();
            for (User user : users) {
                if (user.getName().equals(userName)) {
                    password:
                    while (true) {
                        System.out.println("输入密码");
                        String password = scanner.next();
                        if (password.equals(user.getPassword())) {
                            break login;
                        } else {
                            System.out.println("密码错误");
                            while (true) {
                                System.out.println("1、重新输入");
                                System.out.println("2、更换账号");
                                String choice = scanner.next();
                                switch (choice) {
                                    case "1":
                                        continue password;
                                    case "2":
                                        continue login;
                                    default:
                                        System.out.println("没这个选项");
                                        continue;
                                }
                            }
                        }
                    }

                }

            }
            System.out.println("用户不存在");
        }
    }

    public static void addUser(List<User> users) {
        storeLog("增加用户信息");
        Scanner scanner = new Scanner(System.in);
        String id;
        Id:
        while (true) {
            System.out.println("Id:");
            id = scanner.nextLine();
            for (User user : users) {
                if (id.equals(user.getId())) {
                    System.out.println("id已存在");
                    continue Id;
                }
            }
            break;
        }
        System.out.println("用户名");
        String na = scanner.next();
        System.out.println("密码");
        String pa = scanner.next();
        users.add(new User(id, na, pa));
    }

    public static void editPassword(List<User> list) {
        storeLog("修改密码");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();
        for (User user : list) {
            if (user.getName().equals(userName)) {
                System.out.println("输入旧密码");
                String oldPassword = scanner.next();
                if (oldPassword.equals(user.getPassword())) {
                    System.out.println("输入新密码");
                    String password = scanner.next();
                    user.setPassword(password);
                    System.out.println("修改成功");
                }
            }
            break;
        }
    }

    public static void deleteReader(List<Reader> list) {
        storeLog("删除读者信息");
        System.out.println("读者:");
        Scanner scanner = new Scanner(System.in);
        String readerId = scanner.nextLine();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getReaderId().equals(readerId)) {
                list.remove(i);
                break;
            }
        }

    }

    public static void deleteBook(List<Book> list) {
        storeLog("删除书籍信息");
        System.out.println("书籍isbn");
        Scanner scanner = new Scanner(System.in);
        String isbn = scanner.nextLine();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getISBN().equals(isbn)) {
                list.remove(i);
                break;
            }
        }

    }

    public static void deleteUser(List<User> list) {
        storeLog("删除用户信息");
        System.out.println("用户名");
        Scanner scanner = new Scanner(System.in);
        String userName = scanner.nextLine();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(userName)) {
                list.remove(i);
                break;
            }
        }

    }

    public static void borrowBook(List<Reader> list, List<Book> books, List<BorrowBook> borrowBooks) {
        borrowBook:
        while (true) {
            storeLog("借书");
            Scanner scanner = new Scanner(System.in);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date = format.format(new Date());
            Integer readerIndex;
            Integer bookIndex;
            Reader:
            while (true) {
                System.out.println("读者id");
                String readerId = scanner.nextLine();

                for (Reader Reader : list) {
                    if (readerId.equals(Reader.getReaderId())) {
                        System.out.println(Reader.toString());
                        readerIndex = list.indexOf(Reader);
                        break Reader;
                    }
                }
                System.out.println("读者不存在");
            }
            book:
            while (true) {
                System.out.println("ISBN");
                String isbn = scanner.nextLine();
                for (Book book : books) {
                    if (book.getISBN().equals(isbn)) {
                        System.out.println(book.toString());
                        bookIndex = books.indexOf(book);
                        break book;
                    }
                }
                System.out.println("书籍不存在");
            }
            if (Integer.parseInt(list.get(readerIndex).getMaxBorrowNum()) < 1) {
                System.out.println("可借书籍数量已用完");
                break borrowBook;
            }
            if (Integer.parseInt(books.get(bookIndex).getStockNum()) < 1) {
                System.out.println("书籍库存不足");
                break borrowBook;
            }
            System.out.println("确定借书?y/*");
            if (scanner.nextLine().equals("y")) {
                String bookName = books.get(bookIndex).getBookName();
                String isbn = books.get(bookIndex).getISBN();
                String borrowDate = date;
                String readerId = list.get(readerIndex).getReaderId();
                borrowBooks.add(new BorrowBook(readerId, bookName, isbn, borrowDate));
                list.get(readerIndex).setMaxBorrowNum(String.valueOf(Integer.parseInt(list.get(readerIndex).getMaxBorrowNum()) - 1));
                books.get(bookIndex).setStockNum(String.valueOf(Integer.parseInt(books.get(bookIndex).getStockNum()) - 1));

                break borrowBook;
            }
        }
    }

    public static void returnBook(List<Reader> list, List<Book> books, List<BorrowBook> borrowBooks) throws ParseException {
        returnBook:
        while (true) {
            storeLog("还书");
            Scanner scanner = new Scanner(System.in);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String date = format.format(new Date());
            IO io = new IO();
            Integer fine = Integer.parseInt(io.loadFine());
            Integer readerIndex;
            readerIndex:
            while (true) {
                System.out.println("读者id");
                String readerId = scanner.nextLine();
                for (BorrowBook borrowBook : borrowBooks) {
                    if (readerId.equals(borrowBook.getReaderId())) {
                        System.out.println(borrowBook.toString());
                    }
                }
                for (Reader reader : list) {
                    if (reader.getReaderId().equals(readerId)) {
                        readerIndex = list.indexOf(reader);
                        break readerIndex;
                    }
                }
                System.out.println("信息不存在");
            }
            Integer bookIndex;
            bookIndex:
            while (true) {
                System.out.println("书籍isbn");
                String isbn = scanner.nextLine();
                for (Book book : books) {
                    if (isbn.equals(book.getISBN())) {
                        bookIndex = books.indexOf(book);
                        break bookIndex;
                    }
                }
                System.out.println("没记录");
            }
            Integer borrowBookIndex;
            borrowBookIndex:
            while (true) {
                for (BorrowBook borrowBook : borrowBooks) {
                    if (books.get(bookIndex).getISBN().equals(borrowBook.getISBN()) && list.get(readerIndex).getReaderId().equals(borrowBook.getReaderId())) {
                        System.out.println(borrowBook.toString());
                        borrowBookIndex = borrowBooks.indexOf(borrowBook);
                        break borrowBookIndex;
                    }
                }
                System.out.println("没记录");
                break returnBook;
            }

            Integer fineDay = dayBetween(borrowBooks.get(borrowBookIndex).getBorrowDate(), date);
            Integer maxBorrowDay = Integer.parseInt(list.get(readerIndex).getMaxBorrowDay());
            while (true) {
                if (maxBorrowDay >= fineDay) {
                    System.out.println("没有超期");
                    break;
                } else {
                    String money = String.valueOf((fineDay - maxBorrowDay) * fine);
                    System.out.println("需缴纳" + money + "元");
                    String giveFine = scanner.nextLine();
                    if (giveFine.equals(money)) {
                        break;
                    }
                }
            }
            System.out.println("确定还书?y/*");
            String choice = scanner.nextLine();

            if (choice.equals("y")) {
                System.out.println(borrowBookIndex);
                borrowBooks.remove((int) borrowBookIndex);
                for (BorrowBook borrowBook : borrowBooks) {
                    System.out.println(borrowBook.toString());
                }
                list.get(readerIndex).setMaxBorrowNum(String.valueOf(Integer.parseInt(list.get(readerIndex).getMaxBorrowNum()) + 1));
                books.get(bookIndex).setStockNum(String.valueOf(Integer.parseInt(books.get(bookIndex).getStockNum()) + 1));

                System.out.println("已归还");
                break returnBook;
            }
        }
    }

    //获取日期相差天数
    public static Integer dayBetween(String startDate, String endDate) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar cal = Calendar.getInstance();

        cal.setTime(sdf.parse(startDate));

        long time1 = cal.getTimeInMillis();

        cal.setTime(sdf.parse(endDate));

        long time2 = cal.getTimeInMillis();

        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));

    }


    public static void addBookType(List<BookType> bookTypes) {
        storeLog("增加书籍类型");
        Scanner scanner = new Scanner(System.in);
        String id;
        Id:
        while (true) {
            System.out.println("Id:");
            id = scanner.nextLine();
            for (BookType bookType : bookTypes) {
                if (id.equals(bookType.getTypeId())) {
                    System.out.println("id已存在");
                    continue Id;
                }
            }
            break;
        }
        System.out.println("类型名");
        String na = scanner.nextLine();
        bookTypes.add(new BookType(id, na));
    }

    public static void addReaderType(List<ReaderType> readerTypes) {
        storeLog("增加读者类型");
        Scanner scanner = new Scanner(System.in);
        String id;
        Id:
        while (true) {
            System.out.println("Id:");
            id = scanner.nextLine();
            for (ReaderType readerType : readerTypes) {
                if (id.equals(readerType.getTypeId())) {
                    System.out.println("id已存在");
                    continue Id;
                }
            }
            break;
        }
        System.out.println("类型名");
        String na = scanner.nextLine();
        System.out.println("最长借书时间");
        String day = scanner.nextLine();
        System.out.println("借书数");
        String maxBorrowNum = scanner.nextLine();
        readerTypes.add(new ReaderType(id, na, day, maxBorrowNum));
    }

    public static void editFine() {
        IO io = new IO();
        while (true) {
            try {
                Integer fine = Integer.parseInt(io.loadFine());
                storeLog("修改罚金");
                System.out.println("罚金" + fine + "/天");

                Scanner scanner = new Scanner(System.in);

                fine = Integer.parseInt(scanner.nextLine());
                io.storeFine(fine);
                break;
            } catch (NumberFormatException e) {
                continue;
            }
        }
    }

    //获取系统时间加上字符串,保存到日志文件
    public static void storeLog(String s) {
        IO io = new IO();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        String times = format.format(new Date());
        io.storeLog(times + " " + s);
    }

}

三,结束

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值