编程技术是改变世界的力量。
本站
当前位置:网站首页 > 后端语言 > 正文

Java图书管理系统(io流) java图书管理系统教程

gowuye 2024-04-04 11:54 11 浏览 0 评论

目录

一.题目要求:

为图书管理人员编写一个图书管理系统,图书管理系统的设计主要是实现对图书的管理和相关操作,包括3个表:

图书信息表 ——存储图书的基本信息,包括书号、书名、作者、出版社、出版日期、存馆数量、定价等。

读者信息表 ——存储读者的基本信息,包括学号、姓名、学院、专业班级等。

图书借阅表 ——存储读者借书的相关信息,包括学号、姓名、书号、书名、借阅日期、应还日期、归还日期等。

用菜单选择方式完成下列功能:

1. 图书信息 添加功能:包括书号、书名、作者、出版社、存馆数量、定价等。

2. 图书信息 查询:分别按书名,按作者名,按出版社等进行查询。

3. 图书信息 排序:按书号、书名等按升序进行排序。

4. 图书信息 的修改、删除:按书号或书名进行图书的修改和删除。

5. 读者信息 添加功能:包括学号、姓名、学院、专业班级等。

6. 读者信息 查询:分别按学号、姓名、专业班级等进行查询。

7. 读者信息 排序:按学号、学院等按升序进行排序。

8. 读者信息 的修改、删除:按学号+姓名进行 读者信息 的修改和删除。

9.图书借阅:输入学号+书号,如果该书 图书信息表 中的存管数量大于0,则可以借出,借出相应数量后修改 图书信息表 中的存管数量,在 图书借阅表 添加该同学的借阅。

10.图书归还:输入学号+书号,修改 图书信息表 中的存管数量,在图书借阅表中记录该同学的归还时间。

11. 图书借阅 查询:分别按学号、书名、学院等进行查询。

二.说明

题目要求很明了,以前写过一个表的管理系统,这次就是要求写三个表,分别存储读书信息,读者信息,借阅信息,其中我发现读者信息和借阅信息中有很多相同的变量,所以我将关于这两个的几个操作写在了一起,这样更方便。

全篇主要分为五大块。

一是用来声明Book,Borrower,Reader的变量信息。

二是实现Book,Borrower,Reader的io流操作。

三是Book,Borrower,Reader的增删改查排序等操作。

四是选择身份,是选择图书管理员还是学生来借阅书籍。

五是main函数来实现全部操作。

其实一开始我是用数组做的,不过感觉太啰嗦,后来想起来ArrayList中有add,remove等便携操作

于是后面用List修改了一下.

三.property包

Book类

该类中声明图书信息变量。并定义构造函数。

package property;


import java.util.ArrayList;
import java.util.List;

public class Book {


    private int no;  //书号
    private String title;  //书名
    private String author;  //作者
    private String publisher;   //出版社
    private String publicationTime;   //出版时间
    private int number;   //存馆数量
    private int price;  //定价


    public Book(int no, String title, String author, String publisher, String publicationTime, int number, int price) {
        super();
        this.no = no;
        this.title = title;
        this.author = author;
        this.publisher = publisher;
        this.publicationTime = publicationTime;
        this.number = number;
        this.price = price;
    }

    public static List<Book> temps= new ArrayList<>();
    public static List<Book> books= new ArrayList<>();
    public Book() {super();}
    public int getNo() {
        return no;
    }
    public void setNo(int no) {
        this.no = no;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getPublisher() {
        return publisher;
    }
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }
    public String getPublicationTime() {
        return publicationTime;
    }
    public void setPublicationTime(String publicationTime) {
        this.publicationTime = publicationTime;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
}

Reader类

该类中声明读者信息变量以及借阅信息变量。并定义构造函数。

对于借阅日期,应还日期,归还日期我选择固定借阅日期,通过改变变量实际借阅天数以及预定借阅天数改变应还日期和实际归还日期。

package property;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


public class Reader extends Book{
    private int id;  //学号
    private String name;  //姓名
    private String school;  //学校
    private String major;   //专业
    private int cclass;  //班级
    private int no;   //书号
    private String title;  //书名
    private int trueDay;   //实际借阅天数
    private int appoint;   //约定借阅天数
    protected DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    protected static LocalDate localDate1 = LocalDate.now();
    protected String borrowDay = localDate1.format(dateTimeFormatter1);   //借阅日期

    protected DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    protected LocalDate localDate2= localDate1.plusDays(appoint);
    protected String appointDay = localDate2.format(dateTimeFormatter2);   //约定归还日期

    protected DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    protected LocalDate localDate3 =localDate1.plusDays(trueDay);
    protected String returnDay = localDate3.format(dateTimeFormatter3);   //实际归还日期


    public Reader(int id, String name, String school, String major, int cclass){
        super();
        this.id = id;
        this.name = name;
        this.school = school;
        this.major = major;
        this.cclass = cclass;
    }
    public Reader(int id, String name, int no, String title, String borrowDay, String appointDay,String returnDay) {
        super();
        this.id = id;
        this.name = name;
        this.no = no;
        this.title = title;
        this.borrowDay = borrowDay;
        this.appointDay = appointDay;
        this.returnDay = returnDay;
    }

    public static List<Reader> readers =new ArrayList<>();
    public static List<Reader> borrowers =new ArrayList<>();
    public Reader(){
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    public int getCclass() {
        return cclass;
    }
    public void setCclass(int cclass) {
        this.cclass = cclass;
    }
    public void setNo(int no) {this.no = no;}
    public void setTitle(String title) {this.title = title;}
    public int getNo() {return no;}
    public String getTitle() {return title;}
    public String getBorrowDay(){
        return borrowDay;
    }
    public void setBorrowDay(String borrowDay){
        this.borrowDay=borrowDay;
    }
    public String getAppointDay(){
        return appointDay;
    }
    public void setAppointDay(String appointDay){
        this.appointDay=appointDay;
    }
    public String getReturnDay(){
        return returnDay;
    }
    public void setReturnDay(String returnDay){
        this.returnDay=returnDay;
    }
}

四.IO包

ioBook类

该类中包含 读取文本文档中信息,写入一个Book对象,写入多个对象Books对象 三个方面。

其中写入一个Book对象时要注意加true,否则就会覆盖前面的内容。

package IO;

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


public class ioBook extends Book {


    public static List read() throws IOException {   // 从文本文件中读出数据

        List<Book> books= new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("E:\\bookData.txt"));  // 创建一个字符缓冲输入流对象
        String data;
        while ((data = br.readLine()) != null) {  // 每次度一行,如果有内容继续读取
            if (data.equals("")) {    //防止有空行出现
                continue;
            } else {
                String[] dataArr = data.split(" ");
                Book book = new Book();     // 创建Book对象
                book.setNo(Integer.parseInt(dataArr[0].trim()));
                book.setTitle(dataArr[1].trim());
                book.setAuthor(dataArr[2].trim());
                book.setPublisher(dataArr[3].trim());
                book.setPublicationTime(dataArr[4].trim());
                book.setNumber(Integer.parseInt(dataArr[5].trim()));
                book.setPrice(Integer.parseInt(dataArr[6].trim()));
                books.add(book);
            }
        }
        br.close();
        return books;  // 返回读取到的数据
    }

    public static void saveBook(Book book) throws IOException {    //单个图书信息写入
        FileOutputStream fos = new FileOutputStream("E:\\bookData.txt", true); //加true是为了不覆盖上一次数据,true就是追加
        StringBuffer data = new StringBuffer();
        data.append(book.getNo() + " ");
        data.append(book.getTitle() + " ");
        data.append(book.getAuthor() + " ");
        data.append(book.getPublisher() + " ");
        data.append(book.getPublicationTime() + " ");
        data.append(book.getNumber() + " ");
        data.append(book.getPrice() + "\n");
        String dataStr = data.toString();
        byte[] buf = dataStr.getBytes();
        int len = buf.length;
        fos.write(buf, 0, len);
        fos.flush();
        fos.close();
    }
    public static void saveBook(List books) throws IOException {    //全部图书信息写入
        FileOutputStream fos = new FileOutputStream("E:\\bookData.txt");
        StringBuffer data = new StringBuffer();
        for (int i = 0; i < books.size(); i++) {
            Object book = books.get(i);
            data.append(((Book) book).getNo()+" "+((Book) book).getTitle()+" "+((Book) book).getAuthor()+" "+((Book) book).getPublisher()+" "+((Book) book).getPublicationTime()+" "+((Book) book).getNumber()+" "+((Book) book).getPrice()+"\n");
        }
        String dataStr = data.toString();
        byte[] buf = dataStr.getBytes();
        int len = buf.length;
        fos.write(buf, 0, len);
        fos.flush();
        fos.close();
    }
}

ioBorrower类

同ioBook类

package IO;

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

public class ioBorrower extends Reader {

    public static List read() throws IOException {
       List<Reader> borrowers= new ArrayList<>();
        String data;
        BufferedReader br = new BufferedReader(new FileReader("E:\\borrowerData.txt"));
        while ((data = br.readLine()) != null) {
            if (data.equals("")) {
                continue;
            } else {
                String[] dataArr = data.split(" ");
                Reader borrower = new Reader();
                borrower.setId(Integer.parseInt(dataArr[0].trim()));
                borrower.setName(dataArr[1].trim());
                borrower.setNo(Integer.parseInt(dataArr[2].trim()));
                borrower.setTitle(dataArr[3].trim());
                borrower.setBorrowDay(dataArr[4].trim());
                borrower.setAppointDay(dataArr[5].trim());
                borrower.setReturnDay(dataArr[6].trim());
                borrowers.add(borrower);
            }
        }
        br.close();
        return borrowers;
    }

    public static void saveBorrower(Reader borrower) throws IOException {
        FileOutputStream fos = new FileOutputStream("E:\\borrowerData.txt", true);
        StringBuffer data = new StringBuffer();
        data.append(borrower.getId() + " ");
        data.append(borrower.getName() + " ");
        data.append(borrower.getNo() + " ");
        data.append(borrower.getTitle() + " ");
        data.append(borrower.getBorrowDay() + " ");
        data.append(borrower.getAppointDay() + " ");
        data.append(borrower.getReturnDay() + "\n");
        String dataStr = data.toString();
        byte[] buf = dataStr.getBytes();
        int len = buf.length;
        fos.write(buf, 0, len);
        fos.flush();
        fos.close();
    }

    public static void saveBorrower(List borrowers) throws IOException {
        FileOutputStream fos = new FileOutputStream("E:\\borrowerData.txt");
        StringBuffer data = new StringBuffer();
        for (int i = 0; i < borrowers.size(); i++) {
            Object borrower = borrowers.get(i);
            data.append(((Reader) borrower).getId()+" "+((Reader) borrower).getName()+" "+((Reader) borrower).getNo()+" "+((Reader) borrower).getTitle()+" "+((Reader) borrower).getBorrowDay()+" "+((Reader) borrower).getAppointDay()+" "+((Reader) borrower).getReturnDay()+"\n");
        }
        String dataStr = data.toString();
        byte[] buf = dataStr.getBytes();
        int len = buf.length;
        fos.write(buf, 0, len);
        fos.flush();
        fos.close();
    }
}

ioReader类

同ioBook类

package IO;

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


public class ioBook extends Book {


    public static List read() throws IOException {   // 从文本文件中读出数据

        List<Book> books= new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("E:\\bookData.txt"));  // 创建一个字符缓冲输入流对象
        String data;
        while ((data = br.readLine()) != null) {  // 每次度一行,如果有内容继续读取
            if (data.equals("")) {    //防止有空行出现
                continue;
            } else {
                String[] dataArr = data.split(" ");
                Book book = new Book();     // 创建Book对象
                book.setNo(Integer.parseInt(dataArr[0].trim()));
                book.setTitle(dataArr[1].trim());
                book.setAuthor(dataArr[2].trim());
                book.setPublisher(dataArr[3].trim());
                book.setPublicationTime(dataArr[4].trim());
                book.setNumber(Integer.parseInt(dataArr[5].trim()));
                book.setPrice(Integer.parseInt(dataArr[6].trim()));
                books.add(book);
            }
        }
        br.close();
        return books;  // 返回读取到的数据
    }

    public static void saveBook(Book book) throws IOException {    //单个图书信息写入
        FileOutputStream fos = new FileOutputStream("E:\\bookData.txt", true); //加true是为了不覆盖上一次数据,true就是追加
        StringBuffer data = new StringBuffer();
        data.append(book.getNo() + " ");
        data.append(book.getTitle() + " ");
        data.append(book.getAuthor() + " ");
        data.append(book.getPublisher() + " ");
        data.append(book.getPublicationTime() + " ");
        data.append(book.getNumber() + " ");
        data.append(book.getPrice() + "\n");
        String dataStr = data.toString();
        byte[] buf = dataStr.getBytes();
        int len = buf.length;
        fos.write(buf, 0, len);
        fos.flush();
        fos.close();
    }
    public static void saveBook(List books) throws IOException {    //全部图书信息写入
        FileOutputStream fos = new FileOutputStream("E:\\bookData.txt");
        StringBuffer data = new StringBuffer();
        for (int i = 0; i < books.size(); i++) {
            Object book = books.get(i);
            data.append(((Book) book).getNo()+" "+((Book) book).getTitle()+" "+((Book) book).getAuthor()+" "+((Book) book).getPublisher()+" "+((Book) book).getPublicationTime()+" "+((Book) book).getNumber()+" "+((Book) book).getPrice()+"\n");
        }
        String dataStr = data.toString();
        byte[] buf = dataStr.getBytes();
        int len = buf.length;
        fos.write(buf, 0, len);
        fos.flush();
        fos.close();
    }
}

五.operation包

该包主要进行Book,Borrower,Reader的增加,删减,修改,查找,排序以及借阅,归还步骤。其中Borrower和Reader的增加,删减,修改以及借阅,归还都可以写在一起。

addBook类

使用ArrayList中add方法;

package operation;

import java.io.IOException;
import IO.ioBook;
import property.Book;
import java.util.Scanner;


public class addBook extends Book {
    public void add() throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("正在进行增加图书信息");
        System.out.println("请输入书号");
        int no = scanner.nextInt();
        System.out.println("请输入书名");
        String title = scanner.next();
        System.out.println("请输入作者");
        String author = scanner.next();
        System.out.println("请输入出版社");
        String publisher = scanner.next();
        System.out.println("请输入出版日期");
        String publicationTime = scanner.next();
        System.out.println("请输入存馆数量");
        int number = scanner.nextInt();
        System.out.println("请输入定价");
        int price = scanner.nextInt();
        Book book = new Book(no, title, author, publisher, publicationTime, number, price);
        books.add(book);
        ioBook.saveBook(book);
        System.out.println("新增成功");
    }
}

deleteBook类

使用ArrayList中remove方法。

package operation;

import IO.ioBook;
import property.Book;
import java.io.IOException;
import java.util.Scanner;

public class deleteBook extends Book {

    public void delete() throws IOException {
        if (books.size() == 0) {
            System.out.println("图书管理系统还没有图书!!!");
        } else {
            System.out.println("正在进行删除图书");
            System.out.println("请输入要删除的图书书号");
            Scanner scanner = new Scanner(System.in);
            books = ioBook.read();  //读取文本文档中数据
            int no = scanner.nextInt();
            for (Book book:books) {
                if (no == book.getNo()) {
                    books.remove(book);
                    System.out.println("图书删除完毕");
                    break;
                }
            }
        }
        ioBook.saveBook(books);
    }
}

changeBook类

package operation;

import IO.ioBook;
import User.librarian;
import property.Book;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class changeBook extends Book {
    public void change() throws IOException {
        List<Book> books = new ArrayList<>();
        books = ioBook.read();  //读取文本文档中数据
        if (books.size() == 0) {
            System.out.println("图书管理系统还没有图书!!!");
        } else {
            Scanner scanner = new Scanner(System.in);
            System.out.println("正在进行修改图书信息");
            System.out.println("请选择你要修改的图书编号");
            int no = scanner.nextInt();
            for (Book book : books) {
                if (no == book.getNo()) {
                    System.out.println("请输入你要修改的内容 \n 1.书号  2.书名 3.作者 4.出版社 \n 5.出版日期 6.存馆数量 7.定价 \n 8.返回上一级");
                    int choose = scanner.nextInt();
                    switch (choose) {
                        case 1:
                            System.out.println("请输入修改后的书号");
                            int no1 = scanner.nextInt();
                            book.setNo(no1);
                            ioBook.saveBook(books);
                            System.out.println("修改成功");
                            break;
                        case 2:
                            System.out.println("请输入修改后的书名");
                            String title1 = scanner.next();
                            book.setTitle(title1);
                            ioBook.saveBook(books);
                            System.out.println("修改成功");
                            break;
                        case 3:
                            System.out.println("请输入修改后的作者");
                            String author1 = scanner.next();
                            book.setAuthor(author1);
                            ioBook.saveBook(books);
                            System.out.println("修改成功");
                            break;
                        case 4:
                            System.out.println("请输入修改后的出版社");
                            String publisher1 = scanner.next();
                            book.setPublisher(publisher1);
                            ioBook.saveBook(books);
                            System.out.println("修改成功");
                            break;
                        case 5:
                            System.out.println("请输入修改后的出版日期");
                            String publicationTime1 = scanner.next();
                            book.setPublicationTime(publicationTime1);
                            ioBook.saveBook(books);
                            System.out.println("修改成功");
                            break;
                        case 6:
                            System.out.println("请输入修改后的存馆数量");
                            int number1 = scanner.nextInt();
                            book.setNumber(number1);
                            ioBook.saveBook(books);
                            System.out.println("修改成功");
                            break;
                        case 7:
                            System.out.println("请输入修改后的定价");
                            int price1 = scanner.nextInt();
                            book.setPrice(price1);
                            ioBook.saveBook(books);
                            System.out.println("修改成功");
                            break;
                        case 8:
                            librarian l = new librarian();
                            l.menu();
                        default:
                            System.out.println("输入错误,没有这个选项!");
                            break;
                    }
                }
            }
        }
    }
}

findBook类

package operation;

import property.Book;
import java.io.IOException;
import java.util.Scanner;


public class  findBook extends Book {

    public void find() throws IOException {
        Scanner scanner = new Scanner(System.in);
        if (books.size() == 0) {
            System.out.println("图书管理系统还没有图书!!!");
        } else {
            System.out.println("正在进行查询书籍");
            System.out.println("请输入要查询的图书书号");
            int no = scanner.nextInt();
            for(Book book:books){
                if(no==book.getNo()){
                    System.out.println("书号:"+book.getNo()+" "+"书名:"+book.getTitle()+" "+"作者:"+book.getAuthor()+" "+"出版社:"+book.getPublisher()+" "+"出版时间:"+book.getPublicationTime()+" "+"存馆数量:"+book.getNumber()+" "+"定价:"+book.getPrice()+"\n");
                }
            }
        }
    }
}

sortBook类

使用Comparator中sort方法。

package operation;

import IO.ioBook;
import property.Book;
import java.io.IOException;
import java.util.Comparator;


public class sortBook extends Book {
    public void sort() throws IOException {
        books = ioBook.read();  //读取文本文档中数据
        if (books.size() == 0) {
            System.out.println("图书管理系统还没有图书!!!");
        } else {
            System.out.println("排序前");
            for(Book book:books){    //排序前的图书表
                System.out.println("书号:"+book.getNo()+" "+"书名:"+book.getTitle()+" "+"作者:"+book.getAuthor()+" "+"出版社:"+book.getPublisher()+" "+"出版时间:"+book.getPublicationTime()+" "+"存馆数量:"+book.getNumber()+" "+"定价:"+book.getPrice()+"\n");
            }
            System.out.println("正在进行排序图书信息");
            books.sort(new Comparator<Book>() {    //按照书号进行排序
                @Override
                public int compare(Book book1, Book book2) {
                    int i = book1.getNo() - book2.getNo();
                    if (i != 0) {
                        return i;
                    } else {
                        return 0;
                    }
                }
            });
            System.out.println("排序成功");
            System.out.println("排序后");
            for(Book book:books){   //排序后的图书表
                System.out.println("书号:"+book.getNo()+" "+"书名:"+book.getTitle()+" "+"作者:"+book.getAuthor()+" "+"出版社:"+book.getPublisher()+" "+"出版时间:"+book.getPublicationTime()+" "+"存馆数量:"+book.getNumber()+" "+"定价:"+book.getPrice()+"\n");
            }
             ioBook.saveBook(books);
        }
    }
}

changeBorrow类

同时改变借阅表和读者信息表中数据。将借阅信息和读者信息所有变量列出,当两者有共同变量时则同时改变,否则单独改变。

package operation;

import IO.ioBorrower;
import IO.ioReader;
import User.student;
import property.Reader;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class changeBorrow extends Reader {


    public void change() throws IOException {
        List<Reader> readers = new ArrayList<>();
        List<Reader> borrowers = new ArrayList<>();
        readers = ioReader.read();  //读取文本文档中数据
        borrowers = ioBorrower.read();  //读取文本文档中数据
        if (readers.size() == 0) {
            System.out.println("读者信息系统还没有读者!!!");
        } else {
            Scanner scanner = new Scanner(System.in);
            System.out.println("正在进行修改读者信息");
            System.out.println("请输入要修改的读者学号");
            int id = scanner.nextInt();
            for (Reader reader : readers) {
                for (Reader borrower : borrowers) {
                    if (id == reader.getId()) {
                        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.修改约定归还日期");
                        System.out.println("9.修改实际归还日期");
                        System.out.println("10.返回上一级");
                        int choose = scanner.nextInt();
                        switch (choose) {
                            case 1:
                                System.out.println("请输入学号");
                                int id1 = scanner.nextInt();
                                reader.setId(id1);
                                ioReader.saveReader(readers);
                                borrower.setId(id1);
                                ioBorrower.saveBorrower(borrowers);
                                System.out.println("修改完毕");
                                break;
                            case 2:
                                System.out.println("请输入姓名");
                                String name1 = scanner.nextLine();
                                reader.setName(name1);
                                ioReader.saveReader(readers);
                                borrower.setName(name1);
                                ioBorrower.saveBorrower(borrowers);
                                System.out.println("修改完毕");
                                break;
                            case 3:
                                System.out.println("请输入书号");
                                int no1 = scanner.nextInt();
                                borrower.setNo(no1);
                                ioBorrower.saveBorrower(borrowers);
                                System.out.println("修改完毕");
                                break;
                            case 4:
                                System.out.println("请输入书名");
                                String title1 = scanner.next();
                                borrower.setTitle(title1);
                                ioBorrower.saveBorrower(borrowers);
                                System.out.println("修改完毕");
                                break;
                            case 5:
                                System.out.println("请输入学院");
                                String school1 = scanner.next();
                                reader.setSchool(school1);
                                ioReader.saveReader(readers);
                                System.out.println("修改完毕");
                                break;
                            case 6:
                                System.out.println("请输入专业");
                                String major1 = scanner.next();
                                reader.setMajor(major1);
                                ioReader.saveReader(readers);
                                System.out.println("修改完毕");
                                break;
                            case 7:
                                System.out.println("请输入班级");
                                int cclass1 = scanner.nextInt();
                                reader.setCclass(cclass1);
                                ioReader.saveReader(readers);
                                System.out.println("修改完毕");
                                break;
                            case 8:
                                System.out.println("请输入约定借阅的天数");
                                int appoint1 = scanner.nextInt();
                                DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                                LocalDate localDate3 = localDate1.plusDays(appoint1);
                                String appointDay1 = localDate3.format(dateTimeFormatter3);
                                borrower.setAppointDay(appointDay1);
                                ioBorrower.saveBorrower(borrowers);
                                System.out.println("修改完毕");
                                break;
                            case 9:
                                System.out.println("请输入实际借阅的天数");
                                int trueDay1 = scanner.nextInt();
                                DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                                LocalDate localDate4 = localDate1.plusDays(trueDay1);
                                String returnDay1 = localDate4.format(dateTimeFormatter4);
                                borrower.setReturnDay(returnDay1);
                                ioBorrower.saveBorrower(borrowers);
                                System.out.println("修改完毕");
                                break;
                            case 10:
                                student s =new student();
                                s.menu();
                            default:
                                System.out.println("您输入的内容无效");
                                break;
                        }
                    }
                }
            }
        }
    }
}

deleteBorrow类

同时删去借阅表和读者信息表中数据

package operation;

import IO.ioBorrower;
import IO.ioReader;
import property.Reader;
import java.io.IOException;
import java.util.Scanner;

public class deleteBorrow extends Reader {

    public void delete() throws IOException {
        if (readers.size() == 0) {
            System.out.println("读者信息系统还没有读者!!!");
        } else {
            System.out.println("正在进行删除读者");
            System.out.println("请输入要删除的读者学号");
            Scanner scanner = new Scanner(System.in);
            readers = ioReader.read();  //读取文本文档中数据
            borrowers = ioBorrower.read();   //读取文本文档中数据
            int id = scanner.nextInt();
            for (Reader reader : readers) {
                if (id == reader.getId()) {
                    readers.remove(reader);
                    System.out.println("读者删除完毕");
                    break;
                }
            }
            for (Reader borrower : borrowers) {
                if (id == borrower.getId()) {
                    borrowers.remove(borrower);
                    System.out.println("借阅者删除完毕");
                    break;
                }
            }
        }
        ioBorrower.saveBorrower(borrowers);
        ioReader.saveReader(readers);
    }
}

findBorrow类

package operation;

import IO.ioBorrower;
import property.Reader;
import java.io.IOException;
import java.util.Scanner;

public class findBorrow extends Reader {
    public void find() throws IOException {
        Scanner scanner = new Scanner(System.in);
        borrowers = ioBorrower.read();
        if (borrowers.size() == 0) {
            System.out.println("借阅者系统还没有借阅者!!!");
        } else {
            System.out.println("正在进行查询借阅者");
            System.out.println("请输入要查询的借阅者学号");
            int id = scanner.nextInt();
            for (Reader borrower : borrowers) {
                if (id == borrower.getId()) {
                    System.out.println("学号:" + borrower.getId() + " " + "姓名:" + borrower.getName() + " " + "书号:" + borrower.getNo() + " " + "书名:" + borrower.getTitle() + " " + "借阅日期:" + borrower.getBorrowDay() + " " + "约定归还日期:" + borrower.getAppointDay() + " " + "实际归还日期:" + borrower.getReturnDay() + " " + "归还日期:" + borrower.getReturnDay());
                }
            }
        }
    }
}

sortBorrower类

package operation;

import IO.ioBorrower;
import property.Reader;
import java.io.IOException;
import java.util.Comparator;

public class sortBorrow extends Reader {
    public void sort() throws IOException {
        borrowers = ioBorrower.read();  //读取文本文档中数据
        if (borrowers.size() == 0) {
            System.out.println("图书管理系统还没有图书!!!");
        } else {
            System.out.println("排序前");
            for(Reader borrower:borrowers){    //排序前的借阅表
                System.out.println("学号:" + borrower.getId() + " " + "姓名:" + borrower.getName() + " " + "书号:" + borrower.getNo() + " " + "书名:" + borrower.getTitle() + " " + "借阅日期:" + borrower.getBorrowDay() + " " + "约定归还日期:" + borrower.getAppointDay() + " " + "实际归还日期:" + borrower.getReturnDay() + " " + "归还日期:" + borrower.getReturnDay()+"\n");
            }
            System.out.println("正在进行排序图书信息");
            borrowers.sort(new Comparator<Reader>() {   //按照借阅者学号进行排序
                public int compare(Reader borrower1, Reader borrower2) {
                    int i = borrower1.getId() - borrower2.getId();
                    if (i != 0) {
                        return i;
                    } else {
                        return 0;
                    }
                }
            });
            System.out.println("排序成功");
            System.out.println("排序后");
            for(Reader borrower:borrowers){   //排序后的借阅表
                System.out.println("学号:" + borrower.getId() + " " + "姓名:" + borrower.getName() + " " + "书号:" + borrower.getNo() + " " + "书名:" + borrower.getTitle() + " " + "借阅日期:" + borrower.getBorrowDay() + " " + "约定归还日期:" + borrower.getAppointDay() + " " + "实际归还日期:" + borrower.getReturnDay() + " " + "归还日期:" + borrower.getReturnDay()+"\n");
            }
            ioBorrower.saveBorrower(borrowers);
        }
    }
}

sortReader类

package operation;

import IO.ioReader;
import property.Reader;
import java.io.IOException;
import java.util.Comparator;

public class sortRead extends Reader {

    public void sort() throws IOException {
        readers = ioReader.read();  //读取文本文档中数据
        if (readers.size() == 0) {
            System.out.println("图书管理系统还没有图书!!!");
        } else {
            System.out.println("排序前");
            for(Reader reader:readers){  //排序前的读者信息表
                System.out.println("学号:" + reader.getId() + " " + "姓名:" + reader.getName() + " " + "学院:" + reader.getSchool() + " " + "专业:" + reader.getMajor() + " " + "班级:" + reader.getCclass()+"\n");
            }
            System.out.println("正在进行排序图书信息");
            readers.sort(new Comparator<Reader>() {   //按照读者学号进行排序
                public int compare(Reader reader1, Reader reader2) {
                    int i = reader1.getId() - reader2.getId();
                    if (i != 0) {
                        return i;
                    } else {
                        return 0;
                    }
                }
            });
            System.out.println("排序成功");
            System.out.println("排序后");
            for(Reader reader:readers){  //排序后的读者信息表
                System.out.println("学号:" + reader.getId() + " " + "姓名:" + reader.getName() + " " + "学院:" + reader.getSchool() + " " + "专业:" + reader.getMajor() + " " + "班级:" + reader.getCclass()+"\n");
            }
            ioReader.saveReader(readers);
        }
    }
}

Borrow类

在借阅时bookData中Book的number存馆数量要-1,当数量到0时便不能再借阅。

当借阅时同时在借阅表和读者信息表中传入数据。

由于借阅日期已定,通过改变约定借阅天数而改变应还日期。

import property.Reader;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Borrow extends Reader {

    public void borrow() throws IOException {
        List<Book> books = new ArrayList<>();
        books = ioBook.read();    //读取文本文档中数据
        if (books.size() == 0) {
            System.out.println("图书管理系统还没有图书!!!");

        } else {
            System.out.println("正在进行借阅");
            System.out.println("图书列表");
            Scanner scanner = new Scanner(System.in);
            for (Book book : books) {    //展示所有可借阅的图书
                System.out.println("书号:" + book.getNo() + " " + "书名:" + book.getTitle() + " " + "作者:" + book.getAuthor() + " " + "出版社:" + book.getPublisher() + " " + "出版日期:" + book.getPublicationTime() + " " + "存馆数量:" + book.getNumber() + " " + "定价:" + book.getPrice() + "\n");
            }
            System.out.println("请选择你要借阅的图书书号");
            int no = scanner.nextInt();
            for (Book book1 : books) {
                if (no == book1.getNo()) {
                    if (book1.getNumber() > 0) {      //图书数量>0
                        int num = book1.getNumber();
                        num--;
                        book1.setNumber(num);
                        ioBook.saveBook(books);
                    } else {
                        System.out.println("该书的数量目前为0,请过几天再来吧");
                        break;
                    }
                    System.out.println("请进行登记信息");
                    System.out.println("请输入学号");
                    int id = scanner.nextInt();
                    System.out.println("请输入姓名");
                    String name = scanner.next();
                    System.out.println("请输入学院");
                    String school = scanner.next();
                    System.out.println("请输入专业");
                    String major = scanner.next();
                    System.out.println("请输入班级");
                    int cclass = scanner.nextInt();
                    System.out.println("请输入约定借阅的天数");
                    int a1 = scanner.nextInt();       //约定借阅的天数
                    String returnDay1 = "未还书";
                    DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    LocalDate localDate3 = localDate1.plusDays(a1);
                    String appointDay1 = localDate3.format(dateTimeFormatter3);    //约定归还的日期
                    Reader borrower2 = new Reader(id, name, no, book1.getTitle(), borrowDay, appointDay1, returnDay1);
                    Reader reader2 = new Reader(id, name, school, major, cclass);
                    readers.add(reader2);
                    ioReader.saveReader(reader2);
                    borrowers.add(borrower2);
                    ioBorrower.saveBorrower(borrower2);
                    System.out.println("新增成功");
                }
            }
        }
    }
}

Return类

归还时Book的number存馆数量+1,同时改变借阅表中归还日期的信息。

由于借阅日期已定,通过改变实际借阅天数而改变实际归还日期。

package operation;

import IO.ioBook;
import IO.ioBorrower;
import property.Book;
import property.Reader;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Return extends Reader {
    public void returnBook() throws IOException {
        List<Book> books = new ArrayList<>();
        List<Reader> borrowers = new ArrayList<>();
        books = ioBook.read();
        borrowers = ioBorrower.read();
        if (borrowers.size() == 0) {
            System.out.println("系统还没有借阅信息!!!");
        } else {
            System.out.println("正在进行还书");
            Scanner scanner = new Scanner(System.in);
            System.out.println("图书借阅列单");
            for (Reader borrower : borrowers) {    //所有借阅中和借阅过的图书
                System.out.println("学号:" + borrower.getId() + " " + "姓名:" + borrower.getName() + " " + "书号:" + borrower.getNo() + " " + "书名:" + borrower.getTitle() + " " + "借阅时间:" + borrower.getBorrowDay() + " " + "预计归还时间:" + borrower.getAppointDay() + " " + "实际归还时间:" + borrower.getReturnDay() + "\n");
            }
            System.out.println("请选择你要归还的图书书号");
            int no = scanner.nextInt();
            System.out.println("请输入借阅者学号");
            int id = scanner.nextInt();
            for (Reader borrower : borrowers) {
                for (Book book : books) {
                    if (no == book.getNo() && id == borrower.getId() && no == borrower.getNo() && borrower.getReturnDay().equals("未还书")) {
                        int n = book.getNumber();
                        n++;
                        Book book1 = new Book(book.getNo(), book.getTitle(), book.getAuthor(), book.getPublisher(), book.getPublicationTime(), n, book.getPrice());
                        books.remove(book);   //将改变前的数据删去
                        books.add(book1);     //加入改变后的数据
                        ioBook.saveBook(books);
                        break;
                    }
                }
            }
            for (Reader borrower : borrowers) {
                if (no == borrower.getNo() && borrower.getReturnDay().equals("未还书")) {
                    System.out.println("请输入实际借阅的天数");
                    int trueDay1 = scanner.nextInt();    //实际借阅的天数
                    DateTimeFormatter dateTimeFormatter4 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                    LocalDate localDate4 = localDate1.plusDays(trueDay1);
                    String returnDay1 = localDate4.format(dateTimeFormatter4);  //实际归还的日期
                    borrower.setReturnDay(returnDay1);
                    ioBorrower.saveBorrower(borrowers);
                    System.out.println("归还成功");
                    break;
                }
            }
        }
    }
}

六.User包

该包是选择身份,进行图书管理员或者读者进行借阅归还。

librarian类

package User;

import operation.*;
import java.io.IOException;
import java.util.Scanner;

public class librarian {
    public int menu() throws IOException {
        Scanner scanner = new Scanner(System.in);
        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.删除书籍");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    addBook Add = new addBook();
                    Add.add();
                    break;
                case 2:
                    findBook Find = new findBook();
                    Find.find();
                    break;
                case 3:
                    sortBook Sort = new sortBook();
                    Sort.sort();
                    break;
                case 4:
                    changeBook Change = new changeBook();
                    Change.change();
                    break;
                case 5:
                    deleteBook Delete = new deleteBook();
                    Delete.delete();
                default:
                    System.out.println("您输入的内容无效");
                    break;
            }
        }
    }
}

student类

package User;

import operation.*;
import java.io.IOException;
import java.util.Scanner;

public class student {
    public int menu() throws IOException {
        Scanner scanner = new Scanner(System.in);
        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("6.修改读者及借阅者信息");
            System.out.println("7.删除读者及借阅者信息");
            System.out.println("8.归还图书");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    Borrow b = new Borrow();
                    b.borrow();
                    break;
                case 2:
                    findRead Find = new findRead();
                    Find.find();
                    break;
                case 3:
                    findBorrow f = new findBorrow();
                    f.find();
                    break;

                case 4:
                    sortRead Sort = new sortRead();
                    Sort.sort();
                    break;
                case 5:
                    sortBorrow s = new sortBorrow();
                    s.sort();
                    break;
                case 6:
                    changeBorrow c = new changeBorrow();
                    c.change();
                    break;
                case 7:
                    deleteBorrow d = new deleteBorrow();
                    d.delete();
                    break;
                case 8:
                    Return r = new Return();
                    r.returnBook();
                default:
                    System.out.println("您输入的内容无效");
                    break;
            }
        }
    }
}

七.Main类

在main中运行操作

import User.*;
import java.io.IOException;
import java.util.*;


public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        librarian l = new librarian();
        student s = new student();
        System.out.println("请确定您的身份: 1 > 管理员   0 > 学生");
        int choice = scanner.nextInt();
        if (choice == 1) {
            l.menu();
        } else {
            s.menu();
        }
    }
}

相关推荐

爱上开源之golang入门至实战第四章-切片(Slice)

前言Go数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"),与数组相比切片的长度是不固定的,可以追加元素,在追加时可...

Go语言入门必知教程-切片

切片是一种灵活的和可扩展的数据结构,用于实现和管理数据集。切片由多个元素组成,所有元素都是相同类型的。切片是动态数组的一部分,可以根据需要进行增长和收缩。与数组一样,切片也可以索引。切片具有容量和长度...

Go语言基础-切片

切片是什么?切片是Go语言的一种数据结构。和数组相似,不过切片可以在它的结尾增加更多的元素。这样可变长度在实际编程中更为有用。声明切片切片的声明和数组也很相似,只是声明切片时不需要指定大小。例:va...

5分钟掌握GO中切片的基本使用方法

最近Golang越来越火,不少小伙伴都纷纷开始学习Golang,但对于原先为C++或者JAVA的同学,用习惯了数据、list、vector等,会对Go的切片slice不习惯,下面整理出go中slice...

揭秘 Go 切片(Slice)的秘密

当向切片添加新参数时,底层数组会发生什么变化?它会扩展以容纳更多元素吗?在这篇文章中,我们将深入探讨切片的内部工作原理,以及如何利用这些知识来进行更好的内存管理和性能优化。具体而言,我们将探索Go...

【Go语言slice详解】深入掌握Go语言中的slice类型及常用操作!

Go语言中的slice(切片)是一种非常方便的数据结构,可以动态地增加或减少其元素数量,且可以访问底层数组的任意一个子序列。本文将对Go语言中的slice进行详细的讲解。Slice的定义在Go语言中,...

掌握GO中的Slice,这就够了

最近Golang越来越火,不少小伙伴都纷纷开始学习Golang,但对于原先为C++或者JAVA的同学,用习惯了数据、list、vector等,会对Go的切片slice不习惯,下面整理出go中slice...

golang2021面向对象(26)Go语言类型内嵌和结构体内嵌

结构体可以包含一个或多个匿名(或内嵌)字段,即这些字段没有显式的名字,只有字段的类型是必须的,此时类型也就是字段的名字。匿名字段本身可以是一个结构体类型,即结构体可以包含内嵌结构体。?可以粗略地将这个...

2022-11-13:以下go语言代码中,如何获取结构体列表以及结构体内

2022-11-13:以下go语言代码中,如何获取结构体列表以及结构体内的指针方法列表?以下代码应该返回{"S1":["M1","M2"],"S...

Go语言文件和目录操作

文件和目录操作概述一、文件和目录操作概述在计算机中,文件和目录是存储数据的重要方式。在Go语言中,我们可以使用os和io/ioutil包提供的函数和结构体来进行文件和目录操作。本文将详细介绍Go语言中...

跟我一起学习go语言(五)golang中结构体的初始化方法

1、自定义一个结构体typeVertexstruct{X,Yfloat64}2、初始化方法-指针:rect1:=new(Vertex)rect2:=&Vertex...

Go复合数据类型:结构体

一种通用的、对实体对象进行聚合抽象的能力,在Go中,提供这种聚合抽象能力的类型是结构体类型,也就是struct。自定义一个新类型在Go中,我们自定义一个新类型一般有两种方法。第一种是类型定义...

Go语言基础:方法

导读在阅读本文章前,假定你具备如下能力:?已掌握结构体1.方法1.1方法的概念在理解程序中方法的概念时,我们先看看现实中的一些情况,这样相对比较好理解一些。在农村的朋友可能会知道,在医疗落后的情况...

为什么 Go 语言 struct 要使用 tags

在Go语言中,struct是一种常见的数据类型,它可以用来表示复杂的数据结构。在struct中,我们可以定义多个字段,每个字段可以有不同的类型和名称。除了这些基本信息之外,Go还提供了s...

一文带你掌握掌握 Golang结构体与方法

1.Golang结构体的概念及定义结构体是Golang中一种复合类型,它是由一组具有相同或不同类型的数据字段组成的数据结构。结构体是一种用户自定义类型,它可以被用来封装多个字段,从而实现数据的...

取消回复欢迎 发表评论: