基于I/O流的图书馆管理系统

摘要

本篇博文是在接触学习数据库前,利用I/O流来实现一个简单的图书馆管理系统,包括管理书籍以及管理用户两个大功能。如有不对或者改进的地方,可以留言。

代码

控制台

import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.chrono.IsoChronology;
import java.util.*;

public class Administrator implements Serializable {
    private static final long serialVersionUID = 1500;
    File file1 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt");

    public static void main(String[] args) throws IOException, ClassNotFoundException, ParseException {
            //用来存放用户(管理员)信息
        File file = new File("E:\\JAVA\\test\\Library3\\src\\txt\\users.txt");
        file.createNewFile();
        ArrayList<Users> userslist = new ArrayList<Users>();
//        userslist.add(new Users("xyz", "999"));
//        FileUtils.usein(userslist, file);
        //存放读者信息

        file.createNewFile();
        ArrayList<String> readerslist = new ArrayList<String>();
//

        File file2 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt");
        file.createNewFile();
        File file3 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\borrow.txt");

        file.createNewFile();
        List<Book> booklist = new ArrayList<Book>();
        //借阅库
        List<Borrow> borrowlist = new ArrayList();
//        borrowlist.add(new Borrow(1,"教师",5,new Date(2021,5,4)));
//        FileUtils.borrowout2(borrowlist);
        Scanner scanner = new Scanner(System.in);
        //
        ArrayList<Users> userslist2 = new ArrayList<Users>();
        List<Reader> readerLists = new ArrayList<>();

        //开始界面
        while (true) {
            System.out.println("选择登陆方式 1管理员登陆 2.普通用户登陆");
            switch (scanner.nextInt()) {
                case 1:

                    for (int ls = 0; ; ) {
                        System.out.println("----------------欢迎光临----------------");
                        System.out.println("请输入管理员ID");
                        String a = scanner.next();
                        System.out.println("请输入管理员密码");
                        String b = scanner.next();
                        Users ss = new Users(a, b);
                        userslist2 = FileUtils.usersOut(file);
                        if (userslist2.contains(ss)) {
                            for (int ll = 0; ; ) {
                                System.out.println("请选择要进行的操作1.增加用户 2.删除用户 3.修改密码 4.查看用户列表 5.读者管理 6.书籍管理");
                                switch (scanner.nextInt()) {
                                    case 1:
                                        System.out.println("输入用户姓名");
                                        String c = scanner.next();
                                        System.out.println("输入密码");
                                        String d = scanner.next();
                                        System.out.println("注册成功");
                                        userslist2.add(new Users(c, d));
                                        FileUtils.usein(userslist2, file);

                                        break;

                                    case 2:
                                        for (Users u : userslist2) {
                                            System.out.println(u);
                                            System.out.println("--------------------------------");
                                        }
                                        System.out.println("请选择你要删除的对象");
                                        int e = scanner.nextInt();
                                        userslist2.remove(e - 1);
                                        //用方法覆盖全部
                                        FileUtils.usein(userslist2, file);
                                        break;
                                    case 3:
                                        userslist2 = FileUtils.usersOut(file);
                                        for (Users u : userslist2) {
                                            System.out.println(u);
                                            System.out.println("----------------------------");
                                        }
                                        System.out.println("请按顺序选择你要更改的用户信息,并依次输入信息");
                                        int f = scanner.nextInt();
                                        System.out.println("输入用户名");
                                        String g = scanner.next();
                                        System.out.println("输入密码");
                                        String h = scanner.next();
                                        userslist2.set(f - 1, new Users(g, h));
                                        FileUtils.usein(userslist2, file);
                                        userslist2 = FileUtils.usersOut(file);
                                        break;
                                    case 4:
                                        userslist2 = FileUtils.usersOut(file);
                                        for (Users u : userslist2) {
                                            System.out.println(u);
                                            System.out.println("------------------------------");
                                        }
                                        break;
                                    case 5:

                                        System.out.println("1.添加读者信息 2.读者信息删除 3.读者信息修改 4.查看读者信息 5.退出");
                                        switch (scanner.nextInt()) {
                                            case 1:
                                                boolean flag1 = false;
                                                int cc = 0;
                                                System.out.println("输入读者id:");
                                                int a1 = scanner.nextInt();
                                                boolean flag2= false;
                                                flag2 = IOUtils.writeObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt",readerslist);
                                                for (int i = 0; i < readerLists.size(); i++) {
                                                    if (a1 == readerLists.get(i).getReaderid()) {
                                                        flag1 = true;
                                                        System.out.println("读者已存在");
                                                        break;
                                                    }
                                                }
                                                if (!flag1) {

                                                    System.out.println("输入读者姓名:");
                                                    String a2 = scanner.next();
                                                    System.out.println("输入读者性别:");
                                                    String a3 = scanner.next();
                                                    System.out.println("输入读者年龄");
                                                    int a4 = scanner.nextInt();
                                                    System.out.println("输入读者所在院系:");
                                                    String a5 = scanner.next();
                                                    System.out.println("输入读者电话:");
                                                    int a6 = scanner.nextInt();
                                                    System.out.println("输入注册日期");
                                                    Date a7 = null;
                                                    System.out.println("年");
                                                    int a8 = scanner.nextInt();
                                                    System.out.println("月");
                                                    int a9 = scanner.nextInt();
                                                    System.out.println("日 ");
                                                    int a10 = scanner.nextInt();
                                                    a7 = new Date(a8 - 1900, a9 - 1, a10);
                                                    readerLists.add(new Reader(a1, a2, a3, a4, a5, a6, a7));
                                                    IOUtils.writeObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt", readerLists);
//                                                FileUtils.readerout(new Reader());
                                                }

                                                break;
                                            case 2:
                                                Reader reader = new Reader();
                                                readerLists = IOUtils.readObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt",reader);
                                                for (int i = 0; i < readerLists.size(); i++) {
                                                    System.out.println(readerLists.get(i).getName());
                                                }
                                                System.out.println("请输入你要删除的读者的名字");
                                                String rName = scanner.next();
                                                //读取文件里读者的信息

                                                boolean flag = false;
                                                //循环判断是否有输入的读者
//                                            使用for循环遍历集合时,不可以修改集合的长度
//                                            只能通过迭代器遍历集合时删除
                                                Iterator<Reader> it = readerLists.iterator();
                                                while (it.hasNext()) {
//                                                    Reader reader = it.next();
//                                                    if(rName.equals(reader.getName())){
//                                                        it.remove();
//                                                    }
                                                    if (rName.equals(it.next().getName())) {//输入名字,如果在集合中存在,就返回true
                                                        it.remove();
                                                        flag = true;
                                                    }
                                                }
                                                if (flag) {
                                                    //进行删除用户操作,先清空,在写入
                                                    FileUtils.readerout2(readerLists);
                                                } else {
                                                    System.out.println("用户不存在,无法进行次操作");
                                                }
                                                break;
                                            case 3:
                                                Reader reader2 = new Reader();
                                                readerLists = IOUtils.readObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt",reader2);
                                                for (Reader s : readerLists) {
                                                    System.out.println(s);
                                                    System.out.println("------------------------------");
                                                }
                                                System.out.println("请按顺序选择你要修改的读者的信息");
                                                System.out.println("并依次输入要修改的信息");
                                                int xx = scanner.nextInt();//被修改的人
                                                System.out.println("读者编号:");
                                                int r1 = scanner.nextInt();
                                                Reader reader5 = new Reader();
                                                readerLists = IOUtils.readObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt",reader5);
                                                boolean falg2 = false;
                                                for (int i = 0; i < readerLists.size(); i++) {
                                                    if (r1 == readerLists.get(i).getReaderid()) {
                                                        falg2 = true;
                                                        System.out.println("读者已存在");
                                                        break;
                                                    }
                                                }
                                                if (!falg2) {
                                                    System.out.println("读者姓名:");
                                                    String r2 = scanner.next();
                                                    System.out.println("读者性别:");
                                                    String r3 = scanner.next();
                                                    System.out.println("读者年龄:");
                                                    int r4 = scanner.nextInt();
                                                    System.out.println("读者所在院系");
                                                    String r5 = scanner.next();
                                                    System.out.println("读者电话");
                                                    int r6 = scanner.nextInt();
                                                    System.out.println("读者注册日期");
                                                    Date r7 = null;
                                                    System.out.println("年");
                                                    int r8 = scanner.nextInt();
                                                    System.out.println("月");
                                                    int r9 = scanner.nextInt();
                                                    System.out.println("日");
                                                    int r10 = scanner.nextInt();
                                                    r7 = new Date(r8 - 1900, r9 - 1, r10);

                                                    readerLists.set(xx - 1, new Reader(r1, r2, r3, r4, r5, r6, r7));
                                                    FileUtils.readerout2(readerLists);
                                                    break;
                                                }
                                            case 4:
                                                Reader reader1 = new Reader();
                                                readerLists = IOUtils.readObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt", reader1);
                                                for (Reader s : readerLists) {
                                                    System.out.println(s);
                                                    System.out.println("------------------------------");
                                                }
                                                break;
                                        }
                                        break;
                                    case 6:
                                        System.out.println("1.添加书籍 2.删除书籍 3.查看书籍信息 4.图书借阅和归还 5.修改图书信息 6.退出");
                                        switch (scanner.nextInt()) {
                                            case 1:
                                                Book.add();
                                                break;
//
                                            case 2:
                                                System.out.println("请输入需要删除的书籍的ISBN");
                                                int aaaa = scanner.nextInt();
                                                boolean falg2 = false;
                                                for (int i = 0; i < borrowlist.size(); i++) {
                                                    if (aaaa == borrowlist.get(i).getBookISBN()) {
                                                        falg2 = true;
                                                        System.out.println("此书已被借出");
                                                        break;
                                                    }
                                                }
                                                if(!falg2){
                                                Book.delete();
                                                }
//
                                                break;
                                            case 3:
                                                Book.list();
                                                break;

                                            case 4:
                                                System.out.println("1.录入借书信息 2.查看借书信息 3.书籍归还");
                                                switch (scanner.nextInt()) {
                                                    case 1:
                                                        Reader reader = new Reader();
                                                        readerLists = IOUtils.readObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt",reader);
                                                        for (Reader s : readerLists) {
                                                            System.out.println("读者id:");
                                                            System.out.println(s.getReaderid());
                                                            System.out.println("读者姓名:");
                                                            System.out.println(s.getName());
                                                            System.out.println("***************************");
                                                        }
                                                        System.out.println("请输入借阅者的编号");
                                                        int i = scanner.nextInt();
                                                        if (i - 1 > readerLists.size()) {
                                                            System.out.println("没有这个读者");
                                                        } else {
                                                            int readerId = readerLists.get(i - 1).getReaderid();

                                                            System.out.println("请输入读者类别(高级用户或普通用户):");
                                                            String readerType = scanner.next();
                                                            if (readerType.equals("高级用户") || readerType.equals("普通用户")) {

                                                                booklist = FileUtils.bookrin();
                                                                for (Book j : booklist) {
                                                                    System.out.println("书籍名称");
                                                                    System.out.println(j.getBookname());
                                                                    System.out.println("----------------------------------------");
                                                                }
                                                                System.out.println("请输入被借书籍的顺序");
                                                                int ii = scanner.nextInt();
                                                                if (ii - 1 > booklist.size()) {
                                                                    System.out.println("没有这本书");
                                                                } else {
                                                                    int bookIsbn = booklist.get(ii - 1).getISBN();

                                                                    Date borrowTime = new Date();


                                                                    borrowlist = FileUtils.borrowin();
                                                                    if (borrowlist.size() == 0) {
                                                                        borrowlist.add(new Borrow(readerId, readerType, bookIsbn, borrowTime));
                                                                        FileUtils.borrowout2(borrowlist);

                                                                    } else {
                                                                        for (int ji = 0; ji < borrowlist.size(); ji++) {
                                                                            if (borrowlist.get(ji).getBookISBN() == bookIsbn) {
                                                                                System.out.println("此书已被借出");
                                                                            } else {
                                                                                borrowlist.add(new Borrow(readerId, readerType, bookIsbn, borrowTime));
                                                                                FileUtils.borrowout2(borrowlist);

                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            } else {
                                                                System.out.println("请输入正确的信息");
                                                            }
                                                        }
                                                        break;
                                                    case 2:
                                                        borrowlist = FileUtils.borrowin();
                                                        for (Borrow D : borrowlist) {
                                                            System.out.println(D);
                                                            System.out.println("------------------------");
                                                        }
                                                        break;
                                                    case 3:
                                                        borrowlist = FileUtils.borrowin();
                                                        for (Borrow I : borrowlist) {
                                                            System.out.println("读者id为:");
                                                            System.out.println(I.getReaderId());
                                                            System.out.println("读者类型为:");
                                                            System.out.println(I.getReaderType());
                                                            System.out.println("外借书籍的书名为:");
                                                            System.out.println(I.getBookISBN());
                                                            System.out.println("----------------------------");
                                                        }
                                                        System.out.println("输入需要归还书籍的顺序");
                                                        int is = scanner.nextInt();

                                                        String ok = borrowlist.get(is - 1).getReaderType();
                                                        Date date1 = borrowlist.get(is - 1).getBorrowDate();
                                                        Date date2 = new Date();
                                                        //long during = (date2.getTime() - date1.getTime()) / (3600 * 24 * 1000);
                                                        long during = (date2.getTime() - date1.getTime()) / (1000);
                                                        System.out.println("借阅日期为:" + during + "天");
                                                        //可借阅的期限为35天
                                                        if (ok.equals("高级用户")) {
                                                            if (during < 5) {
                                                                System.out.println("您是高级用户,在归还期限内还书,还书成功");
                                                            } else {
                                                                System.out.println("您是普通用户,未在归还期限内还书,罚款50元");
                                                            }
                                                        } else {
                                                            if (during < 3) {
                                                                System.out.println("您是普通用户,在归还期限内还书,还书成功");
                                                            } else {
                                                                System.out.println("您是普通用户,未在归还期限内还书,罚款50元");
                                                            }
                                                        }
                                                        borrowlist.remove(is - 1);
                                                        FileUtils.borrowout2(borrowlist);
                                                        break;
                                                }
                                                break;
                                            case 5:
                                                System.out.println("请输入需要修改的书籍的ISBN");
                                                int aaa = scanner.nextInt();
                                                boolean falg3 = false;
                                                for (int i = 0; i < borrowlist.size(); i++) {
                                                    if (aaa == borrowlist.get(i).getBookISBN()) {
                                                        falg3 = true;
                                                        System.out.println("此书已被借出");
                                                        break;
                                                    }
                                                }
//                                                booklist=FileUtils.bookrin();
//                                                for (Book s : booklist) {
//                                                    System.out.println(s);
//                                                    System.out.println("---------------------------");
//                                                }
//                                                System.out.println("请输入需要修改的书籍的信息");
//                                                int aa = scanner.nextInt();
                                                if (!falg3){
                                                System.out.println("输入书籍ISBN");
                                                int c1=scanner.nextInt();
                                                System.out.println("输入书籍名字");
                                                String c2=scanner.next();
                                                System.out.println("输入书籍作者");
                                                String c3 =scanner.next();
                                                System.out.println("输入书籍出版社");
                                                String c4=scanner.next();
                                                System.out.println("输入出版日期");
                                                System.out.println("年");
                                                int x=scanner.nextInt();
                                                System.out.println("月");
                                                int y=scanner.nextInt();
                                                System.out.println("日");
                                                int z=scanner.nextInt();
                                                Date c5 = new Date(x,y,z);
                                                System.out.println("输入印刷次数");
                                                int c6 = scanner.nextInt();
                                                System.out.println("输入书籍价格");
                                                Double c7 = scanner.nextDouble();
                                                System.out.println("书籍类型");
                                                System.out.println("01名著  02杂志  03教科书  04娱乐读物");
                                                System.out.println("类型编号");
                                                int b81 = scanner.nextInt();
                                                System.out.println("类型");
                                                String b82 = scanner.next();
                                                BookType c8 = new BookType(b81, b82);
                                                booklist.set(aaa-1,new Book(c1,c2,c3,c4,c5,c6,c7,c8));
                                                IOUtils.writeObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt",booklist);
                                                }
                                            case 6:
                                                break;
                                        }
                                }

                            }
                        } else {
                            System.out.println("输入的用户名或密码不正确");
                        }

                    }

                case 2:
                    //System.out.println("1.登陆 (注册请联系管理员)");
//                switch (scanner.nextInt()) {
//                    case 1:
                    for (int aaa; ; ) {
                        System.out.println("请输入用户id:");
                        int dlid = scanner.nextInt();
                        System.out.println("请输入用户名:");
                        String ldyh = scanner.next();
                        Reader reader = new Reader();
                        readerLists = IOUtils.readObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\reader.txt",reader);
                        for (int cc = 0; cc < readerLists.size(); cc++) {
                            if (dlid == readerLists.get(cc).getReaderid() && ldyh.equals(readerLists.get(cc).getName())) {
                                System.out.println("登陆成功");
                                System.out.println("-----你可以查看书籍列表,并借阅书籍-----");
                                System.out.println("-----借阅后请在35天内归还,否则将会罚款-----");
                                booklist = FileUtils.bookrin();
                                for (Book look : booklist) {
                                    System.out.println("书名为:");
                                    System.out.println(look.getBookname());
                                    System.out.println("作者是:");
                                    System.out.println(look.getAuthor());
                                    System.out.println("*****************************");
                                }
                                while (true) {
                                    System.out.println("请输入被借书籍的编号");
                                    int isdn = scanner.nextInt();
                                    if (isdn > booklist.size()) {
                                        System.out.println("没有这本书");
                                        continue;
                                    } else {
                                        int bookisdn = booklist.get(isdn - 1).getISBN();
                                        Date borrowTime = new Date();
                                        borrowlist = FileUtils.borrowin();
                                        if (borrowlist.size() == 0) {
                                            borrowlist.add(new Borrow(dlid, "普通用户", bookisdn, borrowTime));
                                            FileUtils.borrowout2(borrowlist);
                                            System.out.println("借阅成功");
                                            System.out.println("一次只能借一本书哦");
                                            break;
                                        } else {
                                            boolean flag = false;
                                            for (int ji = 0; ji < borrowlist.size(); ji++) {
                                                if (borrowlist.get(ji).getBookISBN() == bookisdn) {
                                                    System.out.println("此书已被借出");
                                                    flag = true;
                                                    break;
                                                }
                                            }

                                            if (!flag) {
                                                borrowlist.add(new Borrow(dlid, "学生", bookisdn, borrowTime));
                                                FileUtils.borrowout2(borrowlist);
                                                System.out.println("借阅成功");
                                                System.out.println("一次只能借一本书哦");
                                            }
                                        }
                                    }
                                    break;
                                }
                                break;
                            } else {
                                System.out.println("输入的用户名错误");
                            }
                        }
                        break;
                    }
                    break;
            }
        }
}
}

各个类
书类

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Book implements Serializable{
    private static final long serialVersionUID = 1100;
   private int ISBN;
   private String bookname;
   private String author;
   private String publish;//出版社
   private Date pubilishdate;//出版时间
   private int printtime;//印刷次数
   private double unitprice;//单价
   private BookType typename;//图书类型

    public Book() {

    }


    @Override
    public String toString() {
        return
                "ISBN是:" + ISBN +
                "书籍名字是:" + bookname + '\'' +
                ", 书籍作者是" + author + '\'' +
                ", 出版社是:" + publish + '\'' +
                ", 出版日期是:" + pubilishdate +
                ", 印刷次数是:" + printtime +
                ", 书籍单价是:" + unitprice +
                ", 书籍类型是:" + typename ;
    }

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

    public int getISBN() {
        return ISBN;
    }

    public void setISBN(int 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 Date getPubilishdate() {
        return pubilishdate;
    }

    public void setPubilishdate(Date pubilishdate) {
        this.pubilishdate = pubilishdate;
    }

    public int getPrinttime() {
        return printtime;
    }

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

    public double getUnitprice() {
        return unitprice;
    }

    public void setUnitprice(double unitprice) {
        this.unitprice = unitprice;
    }

    public BookType getTypename() {
        return typename;
    }

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

    public  static void add () throws IOException, ParseException {
        File file2 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt");
        List<Book> booklist = new ArrayList<Book>();
        boolean flag2 = false;
        Scanner scanner = new Scanner(System.in);
        System.out.println("书籍ISBN号:");
        int b1 = scanner.nextInt();
        booklist = FileUtils.bookrin();
        for (int i = 0; i < booklist.size(); i++) {
            if (b1 == booklist.get(i).getISBN()) {
                flag2 = true;
                System.out.println("书籍ISDN存在");
                break;
            }
        }
        if (!flag2) {
            System.out.println("书名:");
            String b2 = scanner.next();
            System.out.println("书籍作者:");
            String b3 = scanner.next();
            System.out.println("书籍出版社:");
            String b4 = scanner.next();
            System.out.println("出版时间:");
            System.out.println("年");
            int b51 = scanner.nextInt();
            System.out.println("月");
            int b52 = scanner.nextInt();
            System.out.println("日");
            int b53 = scanner.nextInt();
            Date b5 = new Date(b51, b52, b53);
            System.out.println("印刷次数");
            int b6 = scanner.nextInt();
            System.out.println("书籍价格");
            int b7 = scanner.nextInt();
            System.out.println("书籍类型");
            System.out.println("01名著  02杂志  03教科书  04娱乐读物");
            System.out.println("类型编号");
            int b81 = scanner.nextInt();
            System.out.println("类型");
            String b82 = scanner.next();
            BookType b8 = new BookType(b81, b82);
            booklist.add(new Book(b1, b2, b3, b4, b5, b6, b7, b8));
            IOUtils.writeObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt", booklist);
//                                                FileUtils.bookout(new Book(b1, b2, b3, b4, b5, b6, b7, b8));
        }
    }

    public  static void delete () throws IOException, ParseException {
        Scanner scanner = new Scanner(System.in);
        File file2 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt");
        List<Book> booklist = new ArrayList<Book>();
        booklist = FileUtils.bookrin();
        for (int i = 0; i < booklist.size(); i++) {
            System.out.println(booklist.get(i).getBookname());
        }
        System.out.println("请输入你要删除的书籍的名字");
        String rmame = scanner.next();
        boolean flag = false;
        for (Book B : booklist) {
            if (rmame.equals(B.getBookname())) {
                flag = true;
                booklist.remove(B);
                break;
            }
        }
        if (flag) {
            FileUtils.bookout2(booklist);
        }
    }
    public  static void list () throws IOException, ParseException {
        Scanner scanner = new Scanner(System.in);
        File file2 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt");
        List<Book> booklist = new ArrayList<Book>();
        booklist = FileUtils.bookrin();
        booklist = FileUtils.bookrin();
        for (Book s : booklist) {
        System.out.println(s);
        System.out.println("---------------------------");
    }
}
}

书的类型类

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

//图书类型
public final class BookType implements Serializable {
//    Scanner scanner=new Scanner(System.in);
    private int typied;//图书类型编号
    private String typename;//图书类型名称
    //01神话类
    //02小说
    //03专业书
    //04儿童读物

    @Override
    public String toString() {
        return
                "书籍类型编号是:" + typied +
                ", 书籍类型是" + typename + '\'' ;
    }

    public BookType(int typied, String typename) {
        this.typied = typied;
        this.typename = typename;
    }

    public int getTypied() {
        return typied;
    }

    public void setTypied(int typied) {
        this.typied = typied;
    }

    public String getTypename() {
        return typename;
    }

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

    public  void aviod() {
        ArrayList<BookType> booktypelist = new ArrayList<BookType>();
        booktypelist.add(new BookType(1,"名著"));
        booktypelist.add(new BookType(2,"杂志"));
        booktypelist.add(new BookType(3,"教科书"));
        booktypelist.add(new BookType(4,"娱乐读物"));

    }

    }

借阅类

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Objects;

public class Borrow implements Serializable {
    private static final long serialVersionUID = 1000;
    private int readerId;//读者id
    private String readerType;//读者类别
    private int bookISBN;
    private Date borrowDate;

    public Borrow() {

    }

    public Date getBorrowDate() {
        return borrowDate;
    }

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

    public String getReaderType() {
        return readerType;
    }

    public void setReaderType(String readerType) {
        this.readerType = readerType;
    }

    public Borrow(int readerId, String readerType,int bookISBN, Date borrowDate) {
        this.readerId = readerId;
        this.readerType=readerType;
        this.bookISBN = bookISBN;
        this.borrowDate=borrowDate;

    }
    public int getReaderId() {
        return readerId;
    }

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

    public int getBookISBN() {
        return bookISBN;
    }

    public void setBookISBN(int bookISBN) {
        this.bookISBN = bookISBN;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
       else {
           if (o instanceof Borrow){
               Borrow astring=(Borrow)o;
               if (this.getBookISBN()==(astring.getBookISBN())){
                   return true;
               }
           }
           return false;
        }
    }

    @Override
    public String toString() {
        return
                "读者id:" + readerId +
                ", 书籍ISBN:" + bookISBN+"借书日期为:"+borrowDate
                ;
    }

}

I/O流方法

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

public class IOUtils extends ObjectOutputStream{
//    private static final long serialVersionUID = 1900;

    public IOUtils(OutputStream out) throws IOException {
        super(out);
    }

    protected IOUtils() throws IOException, SecurityException {
        return;
    }

    /**
     * 1、针对字符串的读操作
     * 2、参数列表(文件路径)
     * 3、返回值(字符串列表)
     * 4、要求:文本中的字符串必须换行存储
     */
    public static List<String> readStrInfo(String filePath) {


        File file = new File(filePath);
        List<String> strings = new ArrayList<>();
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;

//        判断文件是否存在,如果不存在则无法读取
        if (!file.exists()) {
            System.out.println("文件不存在");
            return null;
        }

//        读文件
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            String temp = new String();
            while ((temp = bufferedReader.readLine()) != null) {
                strings.add(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fileReader != null) {
                        try {
                            fileReader.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return strings;
    }


    /**
     * 1、针对字符串的写操作
     * 2、参数列表(文件路径、写入的内容)
     * 3、返回值(布尔类型: 是否写入成功)
     * 4、要求: 写入的内容必须为字符串列表
     */
    public static boolean writeStrInfo(String filePath, List<String> strings) {
        File file = new File(filePath);
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        try {
            boolean result = file.createNewFile();
            if (result) {
//                System.out.println("创建新文件");
            }

//            进行写入操作
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);

            for (String s : strings) {
                bufferedWriter.write(s);
                bufferedWriter.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fileWriter != null) {
                        try {
                            fileWriter.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return true;
    }

    /**
     * 1、针对对象的读操作
     * 2、参数列表(文件路径、对象类型)
     * 3、返回值(对象列表)
     * 4、要求:必须传入一个对象来明确对象类型,类必须实现Serializable接口,并且声明serialVersionUID的值,以保证版本的兼容性
     */

    public static <T> List<T> readObjectInfo(String filePath, T t) {
        File file = new File(filePath);
        List<T> objects = new ArrayList<>();
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        if (!file.exists()) {
            System.out.println("文件不存在");
            return null;
        }

        try {
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);
            T p = null;
                while ((p = (T) objectInputStream.readObject()) != null) {
                    objects.add(p);
                }


//            T temp = null;
//            while ((temp = (T)objectInputStream.readObject()) != null){
//                objects.add(temp);
//            }
        } catch (EOFException e) {

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            return objects;
        }

    }

    /**
     * 1、针对对象的写操作
     * 2、参数列表(文件路径,写入的内容)
     * 3、返回值(布尔类型:是否写入成功)
     * 4、要求:写入的内容必须是对象列表
     */
    public static <T> boolean writeObjectInfo(String filePath, List<T> objects) {

        File file = new File(filePath);
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
//                System.out.println("创建新文件");
            }

            fileOutputStream = new FileOutputStream(file);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);

            for (T t : objects) {
                objectOutputStream.writeObject(t);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return true;
    }

}

普通用户即读者类

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Book implements Serializable{
    private static final long serialVersionUID = 1100;
   private int ISBN;
   private String bookname;
   private String author;
   private String publish;//出版社
   private Date pubilishdate;//出版时间
   private int printtime;//印刷次数
   private double unitprice;//单价
   private BookType typename;//图书类型

    public Book() {

    }


    @Override
    public String toString() {
        return
                "ISBN是:" + ISBN +
                "书籍名字是:" + bookname + '\'' +
                ", 书籍作者是" + author + '\'' +
                ", 出版社是:" + publish + '\'' +
                ", 出版日期是:" + pubilishdate +
                ", 印刷次数是:" + printtime +
                ", 书籍单价是:" + unitprice +
                ", 书籍类型是:" + typename ;
    }

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

    public int getISBN() {
        return ISBN;
    }

    public void setISBN(int 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 Date getPubilishdate() {
        return pubilishdate;
    }

    public void setPubilishdate(Date pubilishdate) {
        this.pubilishdate = pubilishdate;
    }

    public int getPrinttime() {
        return printtime;
    }

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

    public double getUnitprice() {
        return unitprice;
    }

    public void setUnitprice(double unitprice) {
        this.unitprice = unitprice;
    }

    public BookType getTypename() {
        return typename;
    }

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

    public  static void add () throws IOException, ParseException {
        File file2 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt");
        List<Book> booklist = new ArrayList<Book>();
        boolean flag2 = false;
        Scanner scanner = new Scanner(System.in);
        System.out.println("书籍ISBN号:");
        int b1 = scanner.nextInt();
        booklist = FileUtils.bookrin();
        for (int i = 0; i < booklist.size(); i++) {
            if (b1 == booklist.get(i).getISBN()) {
                flag2 = true;
                System.out.println("书籍ISDN存在");
                break;
            }
        }
        if (!flag2) {
            System.out.println("书名:");
            String b2 = scanner.next();
            System.out.println("书籍作者:");
            String b3 = scanner.next();
            System.out.println("书籍出版社:");
            String b4 = scanner.next();
            System.out.println("出版时间:");
            System.out.println("年");
            int b51 = scanner.nextInt();
            System.out.println("月");
            int b52 = scanner.nextInt();
            System.out.println("日");
            int b53 = scanner.nextInt();
            Date b5 = new Date(b51, b52, b53);
            System.out.println("印刷次数");
            int b6 = scanner.nextInt();
            System.out.println("书籍价格");
            int b7 = scanner.nextInt();
            System.out.println("书籍类型");
            System.out.println("01名著  02杂志  03教科书  04娱乐读物");
            System.out.println("类型编号");
            int b81 = scanner.nextInt();
            System.out.println("类型");
            String b82 = scanner.next();
            BookType b8 = new BookType(b81, b82);
            booklist.add(new Book(b1, b2, b3, b4, b5, b6, b7, b8));
            IOUtils.writeObjectInfo("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt", booklist);
//                                                FileUtils.bookout(new Book(b1, b2, b3, b4, b5, b6, b7, b8));
        }
    }

    public  static void delete () throws IOException, ParseException {
        Scanner scanner = new Scanner(System.in);
        File file2 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt");
        List<Book> booklist = new ArrayList<Book>();
        booklist = FileUtils.bookrin();
        for (int i = 0; i < booklist.size(); i++) {
            System.out.println(booklist.get(i).getBookname());
        }
        System.out.println("请输入你要删除的书籍的名字");
        String rmame = scanner.next();
        boolean flag = false;
        for (Book B : booklist) {
            if (rmame.equals(B.getBookname())) {
                flag = true;
                booklist.remove(B);
                break;
            }
        }
        if (flag) {
            FileUtils.bookout2(booklist);
        }
    }
    public  static void list () throws IOException, ParseException {
        Scanner scanner = new Scanner(System.in);
        File file2 = new File("E:\\JAVA\\test\\Library3\\src\\txt\\book.txt");
        List<Book> booklist = new ArrayList<Book>();
        booklist = FileUtils.bookrin();
        booklist = FileUtils.bookrin();
        for (Book s : booklist) {
        System.out.println(s);
        System.out.println("---------------------------");
    }
}
}


管理员类

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

public class Users implements Serializable {
  private int id;//用户编号
  private  String name;//用户姓名
  private  String passworsd;//用户密码


    @Override
    public String toString() {
        return "用户姓名是"+name+"密码是"+passworsd;
    }

    public Users(String name, String passworsd) {
        this.name = name;
        this.passworsd = passworsd;
    }


    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 getPassworsd() {
        return passworsd;
    }

    public void setPassworsd(String passworsd) {
        this.passworsd = passworsd;
    }


    public boolean equals(Object users){
        if (this == users) {
            return true;
        } else {
            if (users instanceof Users) {
                Users aString = (Users)users;
                if (this.getName() .equals(aString.getName())&&this.getPassworsd().equals(aString.getPassworsd())) {
                    return true;
                }
            }
            return false;
        }
    }


}


评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值