Library management system (IO stream)

Posted by lpxxfaintxx on Tue, 14 Dec 2021 05:12:06 +0100

preface

Complete code
Link: https://pan.baidu.com/s/1tTeKtw-dQEnFa1HNFVc3NQ
Extraction code: 80es

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

In this code, I use Properties, FileWrite and FileReader to read and write data, but there is a disadvantage in writing code for this collection, that is, the key value pair is fixed and cannot be added.

It should be noted that if you delete a file, it is easy to make mistakes. You can see this article for precautions File deletion):

public static void readersDelete() throws IOException {
        System.out.println("------------------------------");
        System.out.println("Please enter the name of the reader you want to delete:");
        String number = TSUtility.readKeyBoard(5,true);
        Properties po = new Properties();
        String[] path = path(f);
        boolean flag=true;
        for (String p:path){
            System.out.println(p);
            FileReader fr = new FileReader(f+"\\"+p);
            po.load(fr);
            File f1 = new File(f+"\\"+p);
            fr.close();
            System.gc();
            String name = po.getProperty("ReaderName");
            if (number.equals(name)){
                boolean b = f1.delete();
                System.out.println(b);
                flag=false;
                break;
            }
        }
        if (flag==true){
            System.out.println("There is no such reader in the file!");
        }
    }

2, Library 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.

The first mock exam is similar to the above code.

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.

The code of the first mock exam is the focus of this project. The book is returned and the time of borrowing is System.. currentTimeMillis(); To record the time. Record date with dateutils DateToString (D, the first mock exam of dd MM, yyyy) (DateUtils is a tool class). This module can not add new data with Properties set, and can only record fixed templates. All modules I use are serialization and anti serialization.

 private static void serialize(List list) throws IOException {
        ObjectOutputStream ops = new ObjectOutputStream(new FileOutputStream(file));
        ops.writeObject(list);
        ops.close();
    }
    private static List<BookBorrow> Deserialization() throws IOException, ClassNotFoundException {
        List<BookBorrow> list = new ArrayList<>();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        Object o = ois.readObject();
        list.addAll((ArrayList<BookBorrow>) o);
        return list;
    }

It should also be noted that in order for an object to be serialized, the class to which the object belongs must implement the Serializable interface

java.io.InvalidClassException: thrown when one of the following problems in the class is detected when serialization runs. To solve this problem, add a value to the class to which the object belongs: private static final long serialVersionUID = 42L;

public class BookBorrow implements Serializable  {
//  
    private static final long serialVersionUID = 42L;
    private String Number;
    private String name;
    private long time;
    private String ReturnDate;
}

4, Basic information maintenance

  • Including book category setting, reader category setting and fine 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.

The first mock exam of this module is that when setting up the fine, we should pay attention to the different types of readers and set different fine. The reader category settings use serialization and deserialization. Finally, when adding the book category, it is added at the end of the file. Please pay attention Parameters of FileOutputStream, FileWriter} construction methods
The code is as follows (example):

		if(!file.exists())
        {
            file.createNewFile();
        }
        FileWriter fileWriter =new FileWriter(file, true);
        fileWriter.write("\r\n"+bookstype);
        fileWriter.flush();
        fileWriter.close();

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.

Modify password means that the current user modifies his own password; Therefore, the account and password should be disclosed in the login interface

public class registerView {
	public static String s=null;
    public static String s1=null;
}
public static void changePassword() throws IOException {
        FileReader fe = new FileReader(file);
        String account = registerView.s;
        System.out.println("Please enter the password you changed to:");
        String password = sc.next();
        Properties po = registerView.pop;
        FileWriter fw = new FileWriter(file);
        po.load(fe);
        if(po.containsKey(account)){
            po.setProperty(account,password);
            po.store(fw,null);
            System.out.println("Modification succeeded!");
        }
        fw.flush();
        fe.close();
        fw.close();
    }

summary

Finally, we need to talk about the specification when writing code; Put the steps of adding, deleting, modifying and querying under one package, the operation class and tool class into one class, and finally put the interface class together.

Topics: Java Project