java实战——图书馆管理系统(基于I/O流)

本文档介绍了一个使用Java实现的图书馆管理系统,旨在简化图书管理和借阅流程。系统包括Book、BookType、BorrowBook、Reader、ReaderType、Users等类,覆盖了读者、图书类型、借阅信息的增删改查功能,并提供了测试类的详细实现,展示了I/O流在实际项目中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言


【项目说明】
长期以来,人们使用传统的人工方式管理图书馆的日常业务,其操作流程比较烦琐。所以,我们需求设计一个图书管理系统来方便学生的借书和图书馆管理书籍。

【项目内容】
项目功能结构图:
在这里插入图片描述


一、工程文件

文件目录:

在这里插入图片描述

二、Book.java

图书的基本信息
代码如下:

 private static String ISBN;//图书编号ISBN
    private String bookname;//图书名称
    private String author;//作者
    private String publish;//出版社
    private String pubilishdate;//出版日期
    private String printtime;//印刷次数
    private String unitprice;//单价

    public Book() {
   
    }

    public Book(String ISBN, String bookname, String author, String publish, String pubilishdate, String printtime, String unitprice) {
   
        this.ISBN = ISBN;
        this.bookname = bookname;
        this.author = author;
        this.publish = publish;
        this.pubilishdate = pubilishdate;
        this.printtime = printtime;
        this.unitprice = unitprice;
    }

    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 getPubilishdate() {
   
        return pubilishdate;
    }

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

    public String getPrinttime() {
   
        return printtime;
    }

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

    public String getUnitprice() {
   
        return unitprice;
    }

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

    @Override
    public String toString() {
   
        return  ISBN +
                "," + bookname +
                "," + author +
                "," + publish +
                "," + pubilishdate +
                "," + printtime +
                "," + unitprice
                ;
    }

图书的增删改查方法
代码如下:

  //删除图书信息
    public static void deleteBook() {
   
        try {
   
            FileReader f2 = new FileReader("book.txt");
            int x=0;
            char[] chars = new char[50];
            if (( x= f2.read(chars))==-1){
   
                System.out.println("图书信息为空请先添加!!!");
                return;
            }
            f2.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        ArrayList<String> arrayBook = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书编号:");
        String s = scanner.next();
        try {
   
            BufferedReader b1 = new BufferedReader(new FileReader("book.txt"));
            String x;
            while ((x=b1.readLine())!=null){
   
                arrayBook.add(x);
            }
//            System.out.println(arrayBook);
            b1.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        String[] chars = new String[9];
        boolean flag = false;
        for (String s1:arrayBook){
   
            chars=s1.split(",");
            if (chars[0].equals(s)){
   
                flag=true;
            }
        }
        if(!flag) {
   
            System.out.println("输入的图书编号不存在!!!");
            return;
        }
        ArrayList<String> borrowBook = new ArrayList<>();
        try {
   
            BufferedReader b2 = new BufferedReader(new FileReader("borrowbook.txt"));
            String x;
            while ((x=b2.readLine())!=null){
   
                borrowBook.add(x);
            }
            b2.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        String[] chars1 = new String[5];
        for (String a:borrowBook){
   
            chars1=a.split(",");
            if (chars1[1].equals(s)) {
   
                System.out.println("该图书已被读者借去,还未归还,不能删除!!!");
                return;
            }
        }
        String[] b = new String[9];
        Iterator iterator = arrayBook.iterator();
        while (iterator.hasNext()){
   
            String s1 = (String) iterator.next();
            b = s1.split(",");
            if (b[0].equals(s)) {
   
                iterator.remove();
            }
        }
        try {
   
            FileWriter fileWriter = new FileWriter("book.txt", false);
            StringBuffer stringBuffer = new StringBuffer();
            for(String s2 : arrayBook){
   
                stringBuffer.append(s2);
                stringBuffer.append("\n");
            }
            fileWriter.write(stringBuffer.toString());
            fileWriter.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        //提示信息
        System.out.println("删除图书信息成功");
    }
    //修改图书信息
    public static void updateReader() {
   
        System.out.println("请删除这本书,再添加一本书!!!");
    }

    //查看图书信息
    public static void fandAllBook() {
   
        try {
   
            FileReader f2 = new FileReader("book.txt");
            int x=0;
            char[] chars = new char[50];
            if (( x= f2.read(chars))==-1){
   
                System.out.println("图书信息为空请先添加!!!");
                return;
            }else {
   
                do {
   
                    System.out.print(new String(chars, 0, x));
//                    f2.read(chars);
                } while ((x = f2.read(chars)) != -1);
            }
            f2.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }
    //添加图书信息
    public static void addBook() {
   
        ArrayList<String> arrayBook = new ArrayList<>();
        try {
   
        BufferedReader b1 = new BufferedReader(new FileReader("book.txt"));
        String x;
        while ((x=b1.readLine())!=null){
   
            arrayBook.add(x);
        }
            b1.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书编号:");
        String ISBN=scanner.next();
        String[] strings = new String[9];
        for (String b:arrayBook){
   
            strings=b.split(",");
            if (strings[0].equals(ISBN)){
   
                System.out.println("该图书已存在!!!");
                return;
            }
        }
        System.out.println("请输入图书名称:");
        String bookname=scanner.next();
        System.out.println("请输入图书作者:");
        String author=scanner.next();
        System.out.println("请输入图书出版社:");
        String publish=scanner.next();
        System.out.println("请输入图书出版日期:");
        String pubilishdate=scanner.next();
        System.out.println("请输入图书印刷次数:");
        String printtime=scanner.next();
        System.out.println("请输入图书单价:");
        String unitprice=scanner.next();
        //创建读者对象
        Book book = new Book();
        book.setISBN(ISBN);
        book.setBookname(bookname);
        book.setAuthor(author);
        book.setPublish(publish);
        book.setPubilishdate(pubilishdate);
        book.setPrinttime(printtime);
        book.setUnitprice(unitprice);

        //提示信息
        System.out.println("添加图书信息成功");
        try {
   
            FileWriter fileWriter = new FileWriter("book.txt",true);
            fileWriter.write(book+"\n");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }

三、BookType.java

图书类别的基本信息
代码如下:

public class BookType extends Book{
   
    private String typeid;//图书类型编号
    private String typename;//图书类型名称

    public BookType() {
   
    }

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

    @Override
    public String toString() {
   
        return typeid +
                "," + typename +
                "," + getISBN()
               ;
    }

    public String getTypeid() {
   
        return typeid;
    }

    public void setTypeid(String typeid) {
   
        this.typeid = typeid;
    }

    public String getTypename() {
   
        return typename;
    }

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

图书类别的增删改查方法
代码如下:

 public static void selectBookType() {
   
        ArrayList<String> bookType = new ArrayList<>();
        try {
   
        BufferedReader b1 = new BufferedReader(new FileReader("booktype.txt"));
        String x;
        while ((x=b1.readLine())!=null){
   
            bookType.add(x);
        }
            b1.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要添加的图书类别编号:");
        String n=scanner.next();
        String[] chars = new String[3];
//        for (String s:bookType){
   
//            chars=s.split(",");
//            if (chars[0].equals(n)){
   
//                System.out.println("输入的图书类别编号已存在!!!");
//                return;
//            }
//        }
        System.out.println("请输入要添加的图书类别名称:");
        String m=scanner.next();
        boolean flag=true;
        for (String s:bookType){
   
            chars=s.split(",");
            if (chars[0].equals(n)&&chars[1].equals(m)){
   
              break;
            }
            if (chars[0].equals(n)){
   
                System.out.println("输入的图书类别编号已存在!!!");
                return;
            }
            if (chars[1].equals(m)){
   
                flag=false;
                break;
            }
        }
        if (!flag){
   
            System.out.println("输入的图书类别名称已存在!!!");
            return;
        }
        System.out.println("请输入要添加的图书类别的图书编号:");
        String b=scanner.next();
        ArrayList<String> book = new ArrayList<>();
        try {
   
        BufferedReader b1 = new BufferedReader(new FileReader("book.txt"));
        String x;
        while ((x=b1.readLine())!=null){
   
            book.add(x);
        }
            b1.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        boolean falg=false;
        String[] chars1 = new String[9];
        for (String s:book){
   
            chars1=s.split(",");
            if (chars1[0].equals(b)){
   
                falg=true;
            }
        }
        if (!falg) {
   
            System.out.println("输入的图书编号不存在!!!");
            return;
        }
        for (String s:bookType){
   
            chars=s.split(",");
            if (chars[2].equals(b)){
   
                System.out.println("输入的图书已存在类别!!!");
                return;
            }
        }
        BookType type = new BookType();
        type.setTypeid(n);
        type.setTypename(m);
        type.setISBN(b);
        //提示信息
        System.out.println("添加图书类别成功!!!");
        try {
   
        FileWriter f1 = new FileWriter("booktype.txt",true);
        f1.write(type+"\n");
            f1.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }
    //删除图书类别
    public static void deleteBookType() {
   
        ArrayList<String> bookType = new ArrayList<>();
        try {
   
        FileReader f = new FileReader("booktype.txt");
        char[] chars1 = new char[50];
        if ((f.read(chars1))==-1){
   
            System.out.println("图书类型为空请先添加!!!");
            return;
        }else {
   
            BufferedReader b1 = new BufferedReader(new FileReader("booktype.txt"));
            String x;
            while ((x=b1.readLine())!=null){
   
                bookType.add(x);
            }
            b1.close();
        }
            f.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书类别编号:");
        String n=scanner.next();
        boolean flag=false;
        String[] chars = new String[3];
        Iterator<String> iterator = bookType.iterator();
        while (iterator.hasNext()){
   
            String s=iterator.next();
            chars=s.split(",");
            if (chars[0].equals(n)){
   
                iterator.remove();
                flag=true;
            }
        }
        if (!flag){
   
            System.out.println("输入的类别编号不存在!!!");
            return;
        }
        System.out.println("恭喜你,输入的图书类别编号删除成功!!!");
        try {
   
        FileWriter f1 = new FileWriter("booktype.txt",false);
        StringBuffer stringBuffer = new StringBuffer();
        for (String b:bookType){
   
            stringBuffer.append(b);
            stringBuffer.append("\n");
        }
        f1.write(stringBuffer.toString());
            f1.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }

    }
    //修改图书类别
    public static void updateBookType() {
   
        System.out.println("请删除这个类型,再添加一个新类型!!!");
    }
    //查询图书类别
    public static void insertBookType() {
   
        try {
   
            FileReader f1 = new FileReader("booktype.txt");
            int i = 0;
            char[] chars = new char[3];
            if ((i = f1.read(chars)) == -1) {
   
                System.out.println("图书类型为空请先添加!!!");
                return;
            }
            do {
   
                System.out.print(new String(chars, 0, i));
            } while ((i = f1.read(chars)) != -1);
            f1.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }

四、BorrowBook.java

借阅图书的基本信息
代码如下:

public class BorrowBook extends Book{
   
    private String readerid;//读者编号
    private String ISBN;//图书编号
    private String borrowdate;//借书日期
    private String returndate;//还书日期
    private double fine;//违约罚款

    public BorrowBook() {
   
    }

    public BorrowBook(String readerid, String ISBN, String borrowdate, String returndate, double fine) {
   
        this.readerid = readerid;
        this.ISBN = ISBN;
        this.borrowdate = borrowdate;
        this.returndate = returndate;
        this.fine = fine;
    }

    @Override
    public String toString()</
图书资料管理信息系统,带源代码、数据库sql文件、课设报告,具备如下基本功能: 1、 系统管理功能有:角色管理、用户管理、修改密码。主要实现系统的安全管理,不同的操作者有不同的权限,可以执行不同的操作。普通读者的权限只能是查询图书及自己的借阅情况;而图书馆管理员可以对图书信息进行管理,如对新书入库,也可以管理用户,如添加新用户和删除不用的账号等。 2、 进书管理功能有:登记基本的图书信息。这部分的功能用于登记新书的书名、作者、出版社、价格、进书的册数、进书日期、ISBN等。 3、 图书入库管理功能有:对新书分类编目,及时更新图书库中的图书信息。这部分的功能用于对所购进的新书,按其种类学科进行编目,给与唯一的书号;及时更新书库中的图书信息,包括书名、书号、作者、出版社、价格、库存位置和库存册数这些信息,方便读者查询借阅。 4、 查询功能功能有:查询图书的信息,查询读者的借阅情况。这部分的功能主要提供多种方式的查询服务。读者可以根据书名、作者或关键字模糊查询图书信息;读者也可以根据自己的借书证号查询自己的借阅情况,如已借了几本书,借书日期,还书日期,有没有续借等。 5、 借书/还书管理功能有:借书管理、还书管理。这部分的功能是当读者借书时,系统根据借书证号识别读者身份,核对读者的借书信息,做出判断如可不可以借、还可借几本,成功借阅后记录在借书信息并修改书库图书信息。当读者还书时,系统根据借书证号识别读者身份,核对读者的借书信息,做出判断如有没有超期,要不要罚款,需要罚多少等,最后还书成功,修改书库图书信息。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值