Library management system (IO stream)

Project content:

Functional structure diagram of the project:

  The project functions are as follows:
(1) Reader information management: including reader information addition and reader information query and modification functions. After logging in successfully, users can browse the information of all readers or retrieve the information of specific readers; At the same time, you can maintain the reader information, including adding, deleting and modifying. The specific information includes the type of Reader (the type of reader determines the maximum number of books he can borrow and the maximum return days), the reader's name, date of birth, gender, telephone, Department, registration date, etc. (relevant stored data can be directly stored in the file through I/O stream, and can also be read directly in the file)
(2) Book information management: including book information addition and book information query and modification functions. After logging in successfully, users can browse all book information and retrieve the information of specific books; You can also maintain book information. Including adding books, deleting books and modifying book information. Specific information includes: Book ISBN, book name, author, publishing house, publishing date, printing times, unit price, book category, etc. (relevant stored data can be directly stored in the file through I/O stream, and can also be read directly in the file)
(3) Book borrowing management: including book borrowing and book return functions. Book borrowing function, first enter the reader's number, then enter the information of the book to be borrowed, and record the current time of the system, that is, the borrowing time; Book return function: input the reader's number, select the borrowed books under its name, judge whether the current date, that is, the difference between the return date and the borrowing date exceeds the specified period, and calculate the penalty, so as to return the books. Specific information includes: borrowing date, return date and penalty. To calculate the penalty, you need to know the reader type of the reader, and judge the number of books that can be borrowed and the number of books that can be borrowed according to the type. (relevant stored data can be directly stored in the file through I/O stream, and can also be read directly in the file)
(4) Basic information maintenance: including book category setting, reader category setting and penalty setting. Book category settings: you can add, delete, modify and query book categories; Reader category settings can add, delete, modify and query reader categories; Penalty setting: you can specify the penalty standard for one day overdue. (relevant stored data can be directly stored in the file through I/O stream, and can also be read directly in the file)
(5) User management: including password modification, user addition and deletion. Modify password means that the current user modifies his own password; User addition and deletion are the maintenance of user information when adding and removing system users. (relevant stored data can be directly stored in the file through I/O stream, and can also be read directly in the file)

Project idea:

My project file structure is as follows:  

The whole project has been queried, added, modified and deleted many times, and a template can be set.

Now take the description of the library information management module:

The contents of the book documents are as follows:

Each line in the file is a Book object. Obtain the data of each line in the file:

 for (int i = 0; i < array.size(); i++) {
            Book b = (Book) array.get(i);
        }

At this time, each i obtains a Book object, that is, a line of data in the file

Create two new classes to read file content and store content to file

Read data from file to collection

public static void readData(String fileName, ArrayList array)throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line=br.readLine())!=null){
            String[] strArray = line.split(",");
            Book b = new Book();
            b.setISBN(strArray[0]);
            b.setTypeid(Integer.parseInt(strArray[1]));
            b.setBookname(strArray[2]);
            b.setAuthor(strArray[3]);
            b.setPublish(strArray[4]);
            b.setPublishdate(strArray[5]);
            b.setPrinttime(Integer.parseInt(strArray[6]));
            b.setUnitprice(Double.parseDouble(strArray[7]));
            b.setTypename(strArray[8]);
            array.add(b);
        }
        br.close();
    }

Store the data in the collection in a file

public static void writeData(String fileName, ArrayList array) throws IOException{
        BufferedWriter bw=new BufferedWriter(new FileWriter(fileName));
        for (int i = 0; i < array.size(); i++) {
            Book b = (Book) array.get(i);
            StringBuilder sb= new StringBuilder();
            sb.append(b.getISBN()).append(",").append(b.getTypeid()).append(",").append(b.getBookname())
                    .append(",").append(b.getAuthor()).append(",").append(b.getPublish()).append(",").append(b.getPublishdate())
                    .append(",").append(b.getPrinttime()).append(",").append(b.getUnitprice()).append(",").append(b.getTypename());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }

The formats of these two files are common. You can read and write different files only by modifying the read and write parts

After writing these two read and store classes, you can query, add, modify and delete book information

Here are the points needing attention when the whole system comes down:

1) Book borrowing information: borrowed books cannot be modified or deleted

         Let's take deletion as an example. First, obtain the ISBN of the books in the borrowing situation file and store it in the set arrayList2. When deleting the books, the ISBN of the books to be deleted does not exist in the set arrayList2, you can delete them. When you still need to judge whether the ISBN of the books to be deleted exists. On the contrary, existence cannot be deleted. Similarly, the modification can also be written in this way.

       for (int i = 0; i < arrayList.size(); i++) {
            BorrowBook bb = (BorrowBook) arrayList.get(i);
            arrayList2.add(bb.getISBN());
        }
        boolean flag = true;
        while (flag){
            try {
                if(!arrayList2.contains(ISBN)){
                    flag = false;
                    int index = -1;
                    while (true){
                        for (int i = 0; i < array.size(); i++) {
                            Book b = (Book)array.get(i);
                            if(b.getISBN().equals(ISBN)){
                                index=i;
                                break;
                            }
                        }
                        if(index==-1){
                            System.out.println("The book does not exist");
                            break;
                        }else {
                            System.out.println("The books you want to delete are as follows:");
                            selectBookByISBN(ISBN);
                            System.out.print("Are you sure you want to delete?(Y/N): ");
                            char yn = TSUtility.readConfirmSelection();
                            if (yn == 'Y') {
                                array.remove(index);
                                writeData(fileName,array);
                                System.out.println("Delete succeeded");
                            }
                            break;
                        }
                    }
                }else{
                    flag = true;
                    throw new Exception("The current book has borrowing status and cannot be deleted");

                }
            }catch (Exception e){
                System.out.println(e.getMessage());
                break;
            }
        }

2) After borrowing books, the number needs to be reduced. Reducing the number of books borrowed is a modification function. When adding book borrowing, you can modify the number of books by one through the borrowed book ISBN.

3) To calculate the penalty, you first need to calculate the number of days between the borrowing date and the return date, and know how to format the time. Then judge the category of readers, obtain the maximum borrowing days of readers through the category, and calculate the penalty.

Summary:

         As a transition project of the basic part of javaSE, there are still some logic that need to be considered carefully. After completing the project, you will encounter some bugs that you didn't consider before doing it. The lesson of this project is that before doing it, I must make a good plan, think about the obvious bugs after doing it, and think about and solve them before doing it.

Tags: Java Back-end set

Posted on Fri, 26 Nov 2021 04:38:25 -0500 by olidenia