Java项目之 图书管理系统————IO流存储

Java项目之 图书管理系统————IO流存储,第1张

Java项目之 图书管理系统————IO流存储

目录
  • 项目说明
  • 项目内容
  • 参考设计
    • 整体代码模块
    • 实体类
    • 读者信息管理
    • 图书信息管理
    • 图书借阅管理
    • 读者类别管理
    • 图书类别管理
    • 用户管理
  • 总结

项目说明

 长期以来,人们使用传统的人工方式管理图书馆的日常业务,其 *** 作流程比较烦琐。在借书时,读者首先将要借的书和借阅证交给工作人员,然后工作人员将每本书的信息卡片和读者的借阅证放在一个小格栏里,最后在借阅证和每本书贴的借阅条上填写借阅信息。在还书时,读者首先将要还的书交给工作人员,工作人员根据图书信息找到相应的书卡和借阅证,并填好相应的还书信息。太过于繁琐了!所以,我们需求设计一个图书管理系统来方便学生的借书和图书馆管理书籍。
 本系统功能分为读者信息管理模块、图书信息管理模块、图书借阅管理模块、基础信息维护模块和用户管理模块。
 读者信息管理:能够对读者的基本信息进行管理,包括新增读者,如学校新来一名教师,想要借书,就必须先添加读者信息;读者信息的修改,如学生转到别的专业,此时要修改学生的基本信息;删除读者的信息,比如某个学生中途退学了,可以将其信息删除。查询读者的信息,比如有同学拾到了一张借阅卡,卡上有学生的编号,通过此号来查询学生的联系电话,从而可以找到学生。
 图书信息管理:能够对图书的基本信息进行管理,包括新增图书,学校每年会购进新书,此时需要将新书的信息录入系统中;图书信息的修改,如学生借书后将图书丢失,此时需要修改图书的总数量,使总数减1;删除图书,学校在购进新书的同时,每年会对过期的图书进行清理,不再提供借阅,此时就需要将这些图书的信息从系统中删除。查询图书的信息,比如要查看有哪些是Java相关的书籍或者指定ISBN号的图书等。
 图书借阅信息管理:能够对图书的借阅信息进行记录,包括读者信息、图书信息、借阅时间等信息。
 图书归还信息管理:能够对图书的借阅信息进行记录,包括读者信息、图书信息、归还时间、是否超期、罚金等信息。
 系统用户信息管理:能够对系统用户的信息进行管理,包括增加新的系统 *** 作用户,对当前系统用户的密码进行修改,以及删除某一用户。

项目内容

项目功能结构图:

项目功能如下:
(1)读者信息管理:包括读者信息添加和读者信息查询与修改功能。用户登录成功之后,可以浏览所有读者的信息,也可以检索特定读者的信息;同时,可以对读者信息进行维护,包括增加、删除及修改。具体信息包括读者类型(读者的类型决定着他可以借到书的最大数量和最大归还天数)、读者姓名、出生日期、性别、电话、所在院系、注册日期等。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(2)图书信息管理:包括图书信息添加和图书信息查询与修改功能。用户登录成功之后,可以浏览所有图书信息和检索特定图书的信息;也可以对图书信息进行维护。包括添加图书、删除图书以及修改图书信息。具体信息包括:图书ISBN、图书名称、作者、出版社、出版日期、印刷次数、单价、图书类别等。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(3)图书借阅管理:包括图书借阅和图书归还功能。图书借阅功能,先输入读者的编号,然后输入要借阅的图书的信息,记录系统当前时间即借阅时间;图书归还功能,输入读者的编号,选择其名下已借阅的图书,判断当前日期即归还日期与借阅日期的差值是否超过了规定的期限,计算罚金,从而进行图书的归还 *** 作。具体信息包括:借阅日期、归还日期、罚金。由于要计算罚金,故需要知道该读者的读者类型,根据类型判断其可借图书天数、可借图书数量等。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(4)基础信息维护:包括图书类别设置、读者类别设置及罚金设置。图书类别设置,可以对图书的类别进行增加、删除、修改和查询;读者类别设置可以对读者的类别进行增加、删除、修改和查询;罚金设置,可以指定超期一天的罚金标准。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)
(5)用户管理:包括修改密码、用户添加和删除。修改密码,是指当前用户修改自己的密码;用户添加和删除,是对新增和去除系统用户时对用户信息的维护。(相关存储数据可通过I/O流直接存在文件,可也直接在文件里读的数据)

参考设计 整体代码模块

实体类
package librarysystem.domain;
import java.io.Serializable;
public class Book implements Serializable {
    
    //图书ISBN
    private String ISBN;
    //图书名称
    private String bookname;
    //作者
    private String author;
    //出版社
    private String publish;
    //出版日期
    private String publishdate;
    //印刷次数
    private int printtime;
    //单价
    private double price;
    //类别
    private BookType typename;

    public Book() {
    }

    public Book(String ISBN, String bookname, String author, String publish, String publishdate, int printtime, double price, BookType typename) {
        this.ISBN = ISBN;
        this.bookname = bookname;
        this.author = author;
        this.publish = publish;
        this.publishdate = publishdate;
        this.printtime = printtime;
        this.price = price;
        this.typename = typename;
    }

    public String getISBN() {
        return ISBN;
    }

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

    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 int getPrinttime() {
        return printtime;
    }

    public void setPrinttime(int printtime) {
        this.printtime = printtime;
    }

    public double getPrice() {
        return price;
    }

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

    public BookType getTypename() {
        return typename;
    }

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

    @Override
    public String toString() {
        return "Book{" +
                "ISBN='" + ISBN + ''' +
                ", bookname='" + bookname + ''' +
                ", author='" + author + ''' +
                ", publish='" + publish + ''' +
                ", publishdate='" + publishdate + ''' +
                ", printtime=" + printtime +
                ", price=" + price +
                ", typename=" + typename +
                '}';
    }
}
读者信息管理
package librarysystem.dao;



import librarysystem.domain.Reader;
import librarysystem.domain.ReaderType;
import librarysystem.tool.BookException;
import librarysystem.tool.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ReaderDao {
    public static void main(String[] args) throws IOException, ClassNotFoundException, BookException {

    }

    //打印信息
    public static void printInfor(List list){
        tool.storeLog("打印读者信息");
        System.out.println("编号t姓名t类型t年龄t性别tt电话ttt院系tttt注册时间");
        for (Reader r:list){
            System.out.println(r.getReaderid()+"   "+r.getName()+" "+r.getType().getTypename()+"  "+r.getAge()+"  "+r.getSex()+"  "+r.getPhone()+"  "+r.getDepart()+"  "+r.getRegdat());
        }

    }

    //初始化
    public static void initialization() throws IOException, ClassNotFoundException {
        tool.storeLog("读者信息初始化");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Reader reader=new Reader("1","秦潇","男",21,"12346874614","大数据与智能工程学院","2021-09-01",new ReaderType("1","学生",5,5));
        List list=new ArrayList<>();
        list.add(reader);
        tool.returnFile(list,file);
    }

    //读者信息添加
    public static int insertReader(String id, List list, String name, String age, String sex, String phone, String dept, String reg) throws BookException, IOException, ClassNotFoundException {
        tool.storeLog("读者信息添加");
        Reader reader=new Reader(id,name,sex,Integer.parseInt(age),phone,dept,reg,list.get(0));
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");

        Object o = tool.readFile(file);
        List readers=(List) o;

        boolean flag=false;
        for (Reader r:readers){
            String readerid = r.getReaderid();
            if (readerid.equals(id)){
                flag=true;
            }
        }

        if (flag){
            tool.logging("发现了异常,该编号读者已存在");
            throw new BookException("该编号读者已存在");
        }else {
            readers.add(reader);
            tool.returnFile(readers,file);
            return Integer.parseInt(id);
        }

    }

    //查询读者信息
    public static List selectReader() throws IOException, ClassNotFoundException {
        tool.storeLog("查询读者信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Object o=null;
        try {
            o=tool.readFile(file);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        List list=(List)o;

        return list;
    }
    //查询读者信息通过ID
    public static List selectReaderById(String id) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("查询读者信息Id");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Object o=tool.readFile(file);
        List list=(List)o;
        List readerList=new ArrayList<>();
        boolean flag=false;
        for (Reader r:list){
            String readerid = r.getReaderid();
            if (readerid.equals(id)){
                readerList.add(r);
                flag=true;
            }
        }

        if (flag){
            return readerList;
        }else {
            tool.logging("发现了异常,该编号的读者不存在");
            throw new BookException("该编号的读者不存在");
        }
    }
    //查询读者信息通过名字
    public static List selectReaderByName(String name) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("查询读者信息姓名");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Object o=tool.readFile(file);
        List list=(List)o;
        List readerList=new ArrayList<>();
        boolean flag=false;
        for (Reader r:list){
            String readerName = r.getName();
            if (readerName.equals(name)){
                readerList.add(r);
                flag=true;
            }
        }

        if (flag){
            return readerList;
        }else {
            tool.logging("发现了异常,该姓名的读者不存在");
            throw new BookException("该姓名的读者不存在");
        }
    }
    //查询读者信息通过类型
    public static List selectReaderByType(String type) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("查询读者信息类型");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Object o=tool.readFile(file);
        List list=(List)o;
        List readerList=new ArrayList<>();
        boolean flag=false;
        for (Reader r:list){
            String readerType=r.getType().getTypename();
            if (readerType.equals(type)){
                readerList.add(r);
                flag=true;
            }
        }

        if (flag){
            return readerList;
        }else {
            tool.logging("发现了异常,该类型的读者不存在");
            throw new BookException("该类型的读者不存在");
        }}
    //查询读者信息通过学院
    public static List selectReaderByDept(String dept) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("查询读者信息学院");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Object o=tool.readFile(file);
        List list=(List)o;
        List readerList=new ArrayList<>();
        boolean flag=false;
        for (Reader r:list){
            String readerDept=r.getDepart();
            if (readerDept.equals(dept)){
                readerList.add(r);
                flag=true;
            }
        }

        if (flag){
            return readerList;
        }else {
            tool.logging("发现了异常,该学院的读者不存在");
            throw new BookException("该学院的读者不存在");
        }
    }

    //修改读者信息
    public static int updateReader(String id, String typename, String name, Integer age,String sex, String phone, String dept, String reg) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("修改读者信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Object o = tool.readFile(file);
        List list=(List)o;
        List list1 = ReaderTypeDao.selectReaderType(typename);

        boolean flag=false;
        for (Reader reader:list){
            String readerid = reader.getReaderid();
            if (readerid.equals(id)){
                flag=true;
                reader.setName(name);
                reader.setAge(age);
                reader.setSex(sex);
                reader.setPhone(phone);
                reader.setDepart(dept);
                reader.setRegdat(reg);
                reader.setType(list1.get(0));
                break;
            }
        }

        if (flag){
            tool.returnFile(list,file);
            return Integer.parseInt(id);
        }else {
            tool.logging("发现了异常,该编号的读者信息不存在");
            throw new BookException("该编号的读者信息不存在");
        }
    }

    //删除读者信息
    public static int deleteReader(String id) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("删除读者信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\reader.txt");
        Object o=tool.readFile(file);
        List list=(List)o;

        boolean flag=false;
        for (Reader reader:list){
            String readerid = reader.getReaderid();
            if (readerid.equals(id)){
                flag=true;
            }
        }

        if (flag){
            int deleteId=Integer.parseInt(id)-1;
            list.remove(deleteId);
            int id_new=0;
            for (int i = deleteId; i < list.size(); i++) {
                Reader r = list.get(deleteId);
                id_new=Integer.parseInt(r.getReaderid())-1;
                r.setReaderid(String.valueOf(id_new));
            }
            tool.returnFile(list,file);
            return id_new;
        }else {
            tool.logging("发现了异常,该编号的读者信息不存在");
            throw new BookException("该编号的读者信息不存在");
        }
    }


}

图书信息管理
package librarysystem.dao;



import librarysystem.domain.*;

import librarysystem.tool.*;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class BookDao {

    //打印输出
    public static void printInfor(List list){
        System.out.println("编号t图书名称tt作者tt出版社ttt出版日期tt库存数量t单价tt图书类型");
        System.out.println();
        for (Book book:list){
            System.out.println(book.getISBN()+"   "+book.getBookname()+"    "+book.getAuthor()+"    "+book.getPublish()+"    "+book.getPublishdate()+"    "+book.getPrinttime()+"   "+ book.getPrice()+"      "+book.getTypename().getTypename());
        }
    }

    //初始化
    public static void initialization() throws IOException, ClassNotFoundException {
        tool.storeLog("图书初始化");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        //ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        Book book=new Book("1","泰坦尼克号","海伦","北京文化出版社","2012-08-06",20,35.0,new BookType("1","爱情"));
        List list=new ArrayList<>();
        list.add(book);
        tool.returnFile(list,file);
    }


    //添加图书信息
    public static int insertBook(String ISBN, String bookname, String author, String publish, String publishdate, String  printtime, String price, BookType bt) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("添加图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;

        Book book=new Book(ISBN,bookname,author,publish,publishdate,Integer.parseInt(printtime),Double.valueOf(price),bt);
        boolean flag=false;
        for (Book b:list){
            String isbn = b.getISBN();
            if (isbn.equals(ISBN)){
                flag=true;
                break;
            }
        }

        if (flag){
            tool.logging("发现异常,添加的该编号图书已存在");
            throw new BookException("该编号图书已存在");
        }else {
            list.add(book);
            tool.returnFile(list,file);
            return Integer.parseInt(ISBN);
        }
    }

    //查询所有图书信息
    public static List selectBook() throws IOException, ClassNotFoundException {
        tool.storeLog("查询所有图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o=null;
        try {
            o=tool.readFile(file);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        List list=(List)o;

        return list;
    }

    //根据其他信息查询图书信息
    public static List selectBookByISBN(String ISBN) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("根据编号查询图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;
        boolean flag=false;
        List list1=new ArrayList<>();
        for(Book book:list){
            String isbn = book.getISBN();
            if (isbn.equals(ISBN)){
                flag=true;
                list1.add(book);
            }
        }

        if (flag){
            return list1;
        }else {
            tool.logging("发现了异常,查询图书的该编号图书不存在");
            throw new BookException("该编号图书不存在");
        }
    }

    public static List selectBookByName(String bookname) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("根据名称查询图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;
        boolean flag=false;
        List list1=new ArrayList<>();
        for(Book book:list){
            String name = book.getBookname();
            if (name.equals(bookname)){
                flag=true;
                list1.add(book);
            }
        }

        if (flag){
            return list1;
        }else {
            tool.logging("发现了异常,查询图书的该名称图书不存在");
            throw new BookException("该名称图书不存在");
        }
    }

    public static List selectBookByType(String type) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("根据类型查询图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;
        boolean flag=false;
        List list1=new ArrayList<>();
        for(Book book:list){
            String typename = book.getTypename().getTypename();
            if (typename.equals(type)){
                flag=true;
                list1.add(book);
            }
        }

        if (flag){
            return list1;
        }else {
            tool.logging("发现了异常,查询图书的该类型图书不存在");
            throw new BookException("该类型图书不存在");
        }
    }

    public static List selectBookByAuthor(String author) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("根据作者查询图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;
        boolean flag=false;
        List list1=new ArrayList<>();
        for(Book book:list){
            String author1 = book.getAuthor();
            if (author1.equals(author)){
                flag=true;
                list1.add(book);

            }
        }

        if (flag){
            return list1;
        }else {
            tool.logging("发现了异常,查询图书的该作者写的图书不存在");
            throw new BookException("该作者写的图书不存在");
        }
    }

    public static List selectBookByPublish(String Publish) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("根据出版社查询图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;
        boolean flag=false;
        List list1=new ArrayList<>();
        for(Book book:list){
            String publish = book.getPublish();
            if (publish.equals(Publish)){
                flag=true;
                list1.add(book);
            }
        }

        if (flag){
            return list1;
        }else {
            tool.logging("发现了异常,查询图书的该出版社出版的图书不存在");
            throw new BookException("该出版社出版的图书不存在");
        }
    }

    //修改图书信息
    public static int update(String ISBN, String bookname, String author, String publish, String publishdate, int printtime, double price, BookType bt) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("修改图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;

        boolean flag=false;
        for (Book book:list){
            String isbn = book.getISBN();
            if (isbn.equals(ISBN)){
                flag=true;
                book.setISBN(ISBN);
                book.setBookname(bookname);
                book.setAuthor(author);
                book.setPublish(publish);
                book.setPublishdate(publishdate);
                book.setPrinttime(printtime);
                book.setPrice(price);
                book.setTypename(bt);
                break;
            }
        }

        if (flag){
            tool.returnFile(list,file);
            return Integer.parseInt(ISBN);
        }else {
            tool.logging("发现了异常,修改的该编号图书不存在");
            throw new BookException("该编号图书不存在");
        }

    }

    //删除图书信息
    public static int delete(String ISBN) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("删除图书信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
        Object o = tool.readFile(file);
        List list=(List)o;

        boolean flag=false;
        for (Book book:list){
            String isbn = book.getISBN();
            if (isbn.equals(ISBN)){
                flag=true;
            }
        }

        if (flag){
            int id=Integer.parseInt(ISBN)-1;
            list.remove(id);
            for (int i = id; i  
图书借阅管理 
package librarysystem.dao;



import librarysystem.domain.Book;
import librarysystem.domain.BorrowBook;
import librarysystem.domain.Reader;
import librarysystem.domain.ReaderType;
import librarysystem.tool.BookException;
import librarysystem.tool.tool;

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


public class BookBorrowDao {

    //初始化
    public static void initialization() throws IOException, ParseException, BookException, ClassNotFoundException {
        tool.storeLog("图书借阅初始化");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\bookborrow.txt");
        BorrowBook bb=new BorrowBook();
        bb.setReaderid("1");
        bb.setISBN("1");

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
        String date="2021-9-24";
        Date d=sdf.parse(date);
        bb.setBorrowdate(d);
        List list=new ArrayList<>();
        list.add(bb);
        tool.returnFile(list,file);

        //改变图书库存
        File f=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\bookborrow.txt");
        Object o =tool.readFile(f);
        List books=(List)o;

        List bookList = BookDao.selectBookByISBN("1");
        int printtime = bookList.get(0).getPrinttime();
        bookList.get(0).setPrinttime(printtime-1);

        for (int i = 0; i < books.size(); i++) {
            String isbn = books.get(i).getISBN();
            if (isbn.equals("1")){
                books.set(i,bookList.get(0));
                break;
            }
        }

        tool.returnFile(books,f);
    }

    //查询指定编号读者的借书信息
    public static List selectBorrowByReaderId(String readerid) throws BookException, IOException, ClassNotFoundException {
        tool.storeLog("查询指定编号读者的借书信息");
        List list = ReaderDao.selectReaderById(readerid);
        System.out.println("读者姓名:"+list.get(0).getName()+",读者类别:"+list.get(0).getType().getTypename());
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\bookborrow.txt");
        Object o = tool.readFile(file);
        List bbl=(List)o;

        boolean flag=false;
        List list1=new ArrayList<>();
        for (BorrowBook borrowBook:bbl){
            String readerid1 = borrowBook.getReaderid();
            if (readerid1.equals(readerid)){
                list1.add(borrowBook.getISBN());
                flag=true;
            }
        }

        System.out.println();
        System.out.println("该读者借阅的图书信息为:");
        System.out.println("编号t图书名称tt作者tt出版社ttt出版日期tt印刷次数t单价tt图书类型");
        for (String s:list1){
            List bookList = BookDao.selectBookByISBN(s);
            for (Book book:bookList){
                System.out.println(book.getISBN()+"   "+book.getBookname()+"    "+book.getAuthor()+"    "+book.getPublish()+"    "+book.getPublishdate()+"    "+book.getPrinttime()+"   "+ book.getPrice()+"      "+book.getTypename().getTypename());
            }
        }

        return bbl;

    }

    //查看所有借阅信息
    public static void selectAllBorrow() throws IOException, ClassNotFoundException {
        tool.storeLog("查看所有借阅信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\bookborrow.txt");
        Object o = tool.readFile(file);
        List bbl=(List)o;

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
        System.out.println("读者编号t图书编号tt借阅日期tttt归还日期ttt罚金");
        for (BorrowBook bb:bbl){
            String s1=sdf.format(bb.getBorrowdate());

            if (bb.getReturndate()==null){
                System.out.println("  "+bb.getReaderid()+"      "+bb.getISBN()+"       "+s1+"              "+bb.getReturndate()+"           "+bb.getFine());
            }else {
                String s2=sdf.format(bb.getReturndate());
                System.out.println("  "+bb.getReaderid()+"      "+bb.getISBN()+"       "+s1+"              "+s2+"     "+bb.getFine());
            }

        }
    }


    //图书借阅,(借阅者的编号,图书编号,借阅日期)
    public static int borrowBook(String readerid,String ISBN, String  borrowdate) throws IOException, ClassNotFoundException, BookException, ParseException {
        tool.storeLog("图书借阅");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\bookborrow.txt");
        Object o = tool.readFile(file);
        List bbl=(List)o;

        //获取最大借阅数量
        List list1 = ReaderDao.selectReaderById(readerid);
        ReaderType rt=list1.get(0).getType();
        int maxborrownum = rt.getMaxborrownum();
        System.out.println("该读者最大可借阅量为:"+maxborrownum);

        int num=0;
        boolean flag=true;

        //使用迭代器(防止异常ConcurrentModificationException)
        ListIterator iterator = bbl.listIterator();
        while (iterator.hasNext()){
            BorrowBook b=iterator.next();
            String readerid1 = b.getReaderid();
            String isbn = b.getISBN();
            Date borrowdate1 = b.getBorrowdate();
            if (readerid1.equals(readerid) && isbn.equals(ISBN)){

                if (b.getBorrowdate()!=null && b.getReturndate()!=null){

                    num++;
                }else {
                    num++;
                    flag=false;
                }
            }
        }

        if (num books=(List)oo;

                List bookList = BookDao.selectBookByISBN(ISBN);
                int printtime = bookList.get(0).getPrinttime();
                bookList.get(0).setPrinttime(printtime-1);

                for (int i = 0; i < books.size(); i++) {
                    String isbn = books.get(i).getISBN();
                    if (isbn.equals(ISBN)){
                        books.set(i,bookList.get(0));
                        break;
                    }
                }

                tool.returnFile(books,f);

                return Integer.parseInt(readerid);
            }else {
                tool.logging("发现了异常,该借阅信息已存在");
                throw new BookException("该借阅信息已存在");
            }
        }else {
            tool.logging("发现了异常,该读者可借阅书籍量已满");
            throw new BookException("该读者可借阅书籍量已满");
        }
    }

    //图书归还,(还书者的编号,图书编号,归还日期)
    public static int returnBook(String readerid,String ISBN,String returndate,List list) throws  IOException, ClassNotFoundException, ParseException, BookException {
        tool.storeLog("图书归还");
        Date borrowdate=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");

        BorrowBook borrowBook=null;
        boolean flag=false;
        for (BorrowBook bb:list){
            String readerid1 = bb.getReaderid();
            String isbn = bb.getISBN();

            if (readerid1.equals(readerid) && isbn.equals(ISBN)){
                borrowdate=bb.getBorrowdate();
                bb.setReturndate(sdf.parse(returndate));
                flag=true;
                borrowBook=bb;
                break;
            }
        }

        //计算罚金
        if (flag){

            //计算借阅日期
            long days=(sdf.parse(returndate).getTime()-borrowdate.getTime())/(60*60*24*1000);
            long daysBetween=0;
            if (days>0){
                daysBetween=days;
            }else {
                daysBetween=30+days;
            }
            System.out.println("借阅时间为:"+daysBetween+"天");

            //获取该读者类型的可借阅天数和罚金
            List list1 = ReaderDao.selectReaderById(readerid);
            ReaderType rt=list1.get(0).getType();
            String typename=rt.getTypename();
            int limit = rt.getLimit();
            System.out.println("最大借阅天数为:"+limit);

            File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\fine.txt");
            Properties pro=new Properties();
            FileReader fr=new FileReader(file);
            pro.load(fr);
            fr.close();
            String fine = pro.getProperty(typename);

            File f=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\bookborrow.txt");

            if (daysBetween>limit){
                Double fine_result=(daysBetween-limit)*Double.parseDouble(fine);
                System.out.println("该读者延期了"+(daysBetween-limit)+"天,罚金总额为:"+fine_result);
                borrowBook.setFine(fine_result);
                tool.returnFile(list,f);

                //改变图书库存
                File f1=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
                Object oo =tool.readFile(f1);
                List books=(List)oo;

                List bookList = BookDao.selectBookByISBN(ISBN);
                int printtime = bookList.get(0).getPrinttime();
                bookList.get(0).setPrinttime(printtime+1);

                for (int i = 0; i < books.size(); i++) {
                    String isbn = books.get(i).getISBN();
                    if (isbn.equals(ISBN)){
                        books.set(i,bookList.get(0));
                        break;
                    }
                }

                tool.returnFile(books,f1);

                return Integer.parseInt(readerid);

            }else {
                Double fine_result=0.0;
                System.out.println("该读者已正常归还书籍");
                borrowBook.setFine(fine_result);
                tool.returnFile(list,f);

                //改变图书库存
                File f2=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\book.txt");
                Object oo =tool.readFile(f2);
                List books=(List)oo;

                List bookList = BookDao.selectBookByISBN(ISBN);
                int printtime = bookList.get(0).getPrinttime();
                bookList.get(0).setPrinttime(printtime+1);

                for (int i = 0; i < books.size(); i++) {
                    String isbn = books.get(i).getISBN();
                    if (isbn.equals(ISBN)){
                        books.set(i,bookList.get(0));
                        break;
                    }
                }

                tool.returnFile(books,f2);
                return Integer.parseInt(readerid);
            }
        }else {
            tool.logging("发现了异常,该编号读者没有借阅该编号书籍");
            throw new BookException("该编号读者没有借阅该编号书籍");
        }
    }
}


读者类别管理
package librarysystem.dao;



import librarysystem.domain.ReaderType;
import librarysystem.tool.BookException;
import librarysystem.tool.tool;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;


public class ReaderTypeDao {

    //打印信息
    public static void printInfor(List list){
        tool.storeLog("打印读者类型信息");
        for (ReaderType readerType:list){
            System.out.print("读者类型编号:"+readerType.getTypeid()+" ");
            System.out.print("读者类型:"+readerType.getTypename()+" ");
            System.out.print("可借阅天数:"+readerType.getLimit()+" ");
            System.out.print("可借阅数量:"+readerType.getMaxborrownum()+" ");
            System.out.println();
        }
    }

    //初始化
    public static void initialization() throws IOException, ClassNotFoundException {
        tool.storeLog("读者类型初始化");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\readertype.txt");
        ReaderType rt=new ReaderType("1","学生",5,3);
        List list=new ArrayList<>();
        list.add(rt);
        tool.returnFile(list,file);
    }

    //查询全部读者类型信息
    public static List selectReaderType() throws IOException, ClassNotFoundException {
        tool.storeLog("查询全部读者类型信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\readertype.txt");
        Object o=null;
        try{
            o = tool.readFile(file);
        }catch (Exception e){
            System.out.println();
        }
        List list=(List) o;

        return list;
    }

    //查询指定类型名的读者类型信息
    public static List selectReaderType(String type) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("查询指定类型名的读者类型信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\readertype.txt");
        Object o = tool.readFile(file);
        List list=(List) o;

        ReaderType readerType=new ReaderType();
        List rtList=new ArrayList<>();
        boolean flag=false;
        for (ReaderType rt:list){
            String typename = rt.getTypename();
            if (typename.equals(type)){
                readerType=rt;
                flag=true;
            }
        }

        if (flag){
            rtList.add(readerType);
            return rtList;
        }else {
            tool.logging("发现了异常,该读者类型不存在");
            throw new BookException("该读者类型不存在");
        }


    }

    //添加读者类型
    public static int insertReaderType(Integer id, String typename, Integer num, Integer limit) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("添加读者类型");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\readertype.txt");
        Object o = tool.readFile(file);
        List rtList=(List)o;

        boolean flag=false;
        for (ReaderType readerType:rtList){
            String typename1 = readerType.getTypename();
            if (typename1.equals(typename)){
                flag=true;
            }
        }

        if (flag){
            tool.logging("出现了异常,该读者类型已存在");
            throw new BookException("该读者类型已存在");
        }else {
            ReaderType rt=new ReaderType(String.valueOf(id),typename,limit,num);
            rtList.add(rt);
            tool.returnFile(rtList,file);
            return id;
        }
    }

    //修改读者类型
    public static int updateReaderType(Integer id, String typename, Integer num, Integer limit) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("修改读者类型");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\readertype.txt");
        Object o = tool.readFile(file);
        List list=(List) o;

        boolean flag=false;
        for (ReaderType rt:list){
            String typeid = rt.getTypeid();
            if (String.valueOf(id).equals(typeid)){
                flag=true;
            }
        }

        if (!flag){
            tool.logging("发现了异常,该编号读者类型不存在");
            throw new BookException("该编号读者类型不存在");
        }else {
            ReaderType r=list.get(id-1);
            r.setTypename(typename);
            r.setLimit(limit);
            r.setMaxborrownum(num);
            tool.returnFile(list,file);
            return id;
        }
    }

    //删除读者类型
    public static int deleteReaderType(Integer id) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("删除读者类型");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\readertype.txt");
        Object o = tool.readFile(file);
        List list=(List) o;

        boolean flag=true;
        for (ReaderType rt:list){
            String typeid = rt.getTypeid();
            if (String.valueOf(id).equals(typeid)){
                flag=false;
            }
        }

        if (flag){
            tool.logging("发现了异常,该编号读者类型不存在");
            throw new BookException("该编号读者类型不存在");
        }else {
            list.remove(id-1);
            for (int i = id-1; i  set = pro.stringPropertyNames();

        boolean flag=false;
        String key=null;
        for (String s:set){
            if (s.equals(typename)){
                flag=true;
                key=s;
            }
        }

        if (flag){
            //修改
            pro.setProperty(key,String.valueOf(fine));
        }else {
            //添加
            pro.put(typename,String.valueOf(fine));
        }
        System.out.println("以下为所设置的罚金:");
        System.out.println(pro);
        FileWriter fw=new FileWriter(file);
        pro.store(fw,null);
        fw.close();
        return pro;

    }
}


图书类别管理
package librarysystem.dao;





import librarysystem.domain.Reader;
import librarysystem.domain.ReaderType;
import librarysystem.tool.BookException;
import librarysystem.tool.tool;
import librarysystem.domain.BookType;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class BookTypeDao {
    //打印输出
    public static void printInfor(List list){
        for (BookType bookType:list){
            System.out.print("图书类别编号:"+bookType.getTypeid()+" ");
            System.out.print("图书类别:"+bookType.getTypename()+" ");
            System.out.println();
        }
    }

    //初始化
    public static void initialization() throws IOException {
        tool.storeLog("图书类型初始化");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\booktype.txt");
        BookType bookType=new BookType("1","爱情");
        List list=new ArrayList<>();
        list.add(bookType);
        tool.returnFile(list,file);
    }

    //查询全部信息
    public static List selectBookType() throws IOException, ClassNotFoundException {
        tool.storeLog("查询全部图书类型信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\booktype.txt");
        Object o=null;
        try{
            o = tool.readFile(file);
        }catch (Exception e){
            System.out.println();
        }
        List list=(List) o;

        return list;
    }

    //查询指定类型名的图书类型信息
    public static List selectBookType(String type) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("查询指定类型名的图书类型信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\booktype.txt");
        Object o = tool.readFile(file);
        List list=(List)o;

        boolean flag=false;
        List bookTypeList = new ArrayList<>();
        for (BookType bookType:list){
            String typename = bookType.getTypename();
            if (typename.equals(type)){
                bookTypeList.add(bookType);
                flag=true;
            }
        }

        if (flag){
            return bookTypeList;
        }else {
            tool.logging("发现了异常,该类型的图书类型信息不存在");
            throw new BookException("该类型的图书类型信息不存在");
        }
    }

    //添加图书类型
    public static int insertBookType(Integer id, String typename) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("添加图书类型");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\booktype.txt");
        Object o = tool.readFile(file);
        List list=(List)o;

        boolean flag=false;
        for (BookType b:list){
            String typeid = b.getTypeid();
            if (typeid.equals(String.valueOf(id))){
                flag=true;
            }
        }
        if (flag){
            tool.logging("发现了异常,该编号的图书类型信息已存在");
            throw new BookException("该编号的图书类型信息已存在");
        }else {
            BookType bookType=new BookType(String.valueOf(id),typename);
            list.add(bookType);
            tool.returnFile(list,file);
            return id;
        }



    }

    //修改图书类型
    public static int updateBookType(Integer id, String typename) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("修改图书类型");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\booktype.txt");
        Object o = tool.readFile(file);
        List list=(List)o;

        boolean flag=false;
        for (BookType bookType:list){
            String typeid = bookType.getTypeid();
            if (typeid.equals(String.valueOf(id))){
                flag=true;
            }
        }

        if (flag){
            list.get(id-1).setTypename(typename);
            tool.returnFile(list,file);
            return id;
        }else {
            tool.logging("发现了异常,该编号的图书类型信息不存在");
            throw new BookException("该编号的图书类型信息不存在");
        }
    }

    //删除图书类型
    public static int deleteBookType(Integer id) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("删除图书类型");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\booktype.txt");
        Object o = tool.readFile(file);
        List list=(List)o;

        boolean flag=false;
        for (BookType bookType:list){
            String typeid = bookType.getTypeid();
            if (typeid.equals(String.valueOf(id))){
                flag=true;
            }
        }

        if (flag){
            list.remove(id-1);
            int id_new=0;
            for (int i = id-1; i  
用户管理 
package librarysystem.dao;




import librarysystem.domain.Users;
import librarysystem.tool.BookException;
import librarysystem.tool.tool;
import team.TSUtility;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class UserDao {
    public static void main(String[] args) throws IOException, ClassNotFoundException, BookException, InterruptedException {


        selectUser();
        deleteUser(1);
        selectUser();
    }
    //判断账号和密码是否有效
    public static Users check(String name, String password) throws IOException, ClassNotFoundException, InterruptedException {
        tool.storeLog("判断账号和密码");
        File file = new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\user.txt");
        List list = (List) tool.readFile(file);

        boolean flag = false;
        Users u = null;
        for (Users user : list) {
            if (user.getName().equals(name)) {
                flag = true;
                u = user;
                break;
            }
        }

        if (flag) {
            System.out.println("账户正确");

            if (u.getPassword().equals(password)) {
                TSUtility.loadSpecialEffects();
                System.out.println("密码正确");
                u.setId("1");
                u.setName(name);
                u.setPassword(password);
                return u;
            } else {
                System.out.println("密码错误");
                return null;
            }
        } else {
            System.out.println("该用户不存在");
            return null;
        }
    }

    //存入默认账号和密码(初始化)
    public static void initialization() throws IOException {
        tool.storeLog("账号和密码(初始化)");
        File file = new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\user.txt");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        ArrayList arr = new ArrayList<>();
        Users user = new Users("1", "秦潇", "123");
        arr.add(user);
        oos.writeObject(arr);
        oos.close();
    }

    //查询所有信息
    public static List selectUser() throws IOException, ClassNotFoundException {
        tool.storeLog("查询所有信息");
        File file = new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\user.txt");
        List list = (List) tool.readFile(file);
        for (Users u : list) {
            String id = u.getId();
            String name = u.getName();
            String password = u.getPassword();
            System.out.println("id:" + id + ",name:" + name + ",password:" + password);
        }

        return list;
    }

    //添加用户信息
    public static int insertUser(String name, String password) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("添加用户信息");
        File file = new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\user.txt");
        List list = (List) tool.readFile(file);

        String newName = null;
        boolean flag = false;
        for (Users u : list) {
            String uName = u.getName();
            if (uName.equals(name)) {
                flag = true;
                break;
            }
        }

        if (flag) {
            tool.logging("发现了异常,该用户已存在");
            throw new BookException("该用户已存在");

        } else {
            int len = list.size();
            Users u = new Users(String.valueOf(len + 1), name, password);
            list.add(u);
            tool.returnFile(list, file);
            return len + 1;
        }
    }

    //修改用户信息
    public static int updateUserPWD(Integer id, String pwd) throws IOException, ClassNotFoundException, BookException, InterruptedException {
        tool.storeLog("修改用户信息");
        File file=new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\user.txt");
        List list=(List) tool.readFile(file);

        //判断
        boolean flag=false;
        for (Users u:list){
            String uId = u.getId();
            if (Integer.parseInt(uId)==id){
                flag=true;
                break;
            }
        }

        int id_new =id-1;

        if (!flag) {
            tool.logging("发现了异常,该编号用户不存在");
            throw new BookException("该编号用户不存在");
        }else{
            if (pwd.equals("账号")){
                Users user=list.get(id_new);
                System.out.print("请输入修改后的账号:");
                String s= TSUtility.readKeyBoard(8,false);

                user.setName(s);
                tool.returnFile(list,file);
                TSUtility.loadSpecialEffects();
                System.out.println("账号修改成功");
                return id_new;

            }else if (pwd.equals("密码")){
                Users user=list.get(id_new);
                System.out.print("请输入修改后的密码:");
                String s= TSUtility.readKeyBoard(16,false);

                user.setPassword(s);
                tool.returnFile(list,file);
                TSUtility.loadSpecialEffects();
                System.out.println("密码修改成功");
                return id_new;

            }else if (pwd.equals("账号和密码")){
                Users user=list.get(id_new);
                System.out.print("请输入修改后的账号:");
                String s1= TSUtility.readKeyBoard(8,false);
                System.out.print("请输入修改后的密码:");
                String s2= TSUtility.readKeyBoard(16,false);

                user.setName(s1);
                user.setPassword(s2);
                tool.returnFile(list,file);
                TSUtility.loadSpecialEffects();
                System.out.println("账号和密码修改成功");
                return id_new;
            }else {
                throw new BookException("选项输入错误");
            }
        }
    }

    //删除用户信息
    public static int deleteUser(Integer id) throws IOException, ClassNotFoundException, BookException {
        tool.storeLog("删除用户信息");
        File file = new File("C:\Users\潇霸霸\IdeaProjects\untitled\src\librarysystem\file\user.txt");
        List list = (List) tool.readFile(file);

        //判断
        boolean flag = false;

        for (int i = 0; i < list.size(); i++) {
            Users u = list.get(i);
            String uId = u.getId();
            if (Integer.parseInt(uId) == id) {
                flag = true;
                break;
            }
        }

        if (!flag) {
            tool.logging("发现了异常,该编号用户不存在");
            throw new BookException("该编号用户不存在");
        } else {
            int id_new = id - 1;
            list.remove(id_new);

            for (int i = id_new; i < list.size(); i++) {
                Users u = list.get(i);
                u.setId(String.valueOf(i + 1));
            }
            tool.returnFile(list, file);
            return id;
        }
    }
}


总结

整体代码展示不全,还有界面和几个实体还需同学们自己补齐。图书管理系统之IO流存储,主要是通过IO的知识将产生的数据存储到文件当中,很多 *** 作都是增删改查,这些 *** 作相信大家都很轻松。需要注意的是,借阅当中,库存容量问题,如超过容量则需要提示不能借阅,库存没有了等提示。还需注意当读者归还超过时间需要缴纳罚金等问题。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5076473.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-16
下一篇2022-11-16

发表评论

登录后才能评论

评论列表(0条)

    保存