catalogue
preface
After learning the contents of Java container collection and file IO stream, we ushered in another practical project < library management system >; This time, different from project 1, the project has to be implemented with IO flow knowledge. Next, let's take a look at the needs and objectives of the project
It is said that when you see the requirements document of project 2, you really look at a loss. That's it? That's it? A picture and a few paragraphs of demand instructions sent me away? How should I do it? From what angle? This is really a big problem. After wandering and melancholy for a few hours, it is like society lowering its head after all. I read this article written by others on my blog, like the big guys around me Everything is difficult at the beginning, but we have to take the first step! Next, enter the theme. Rush, rush!!!
Functional process
After carefully considering the demand map given in the document, we'd better not write it in the order given (otherwise it's too troublesome later), because readers and books contain corresponding categories, and book borrowing and return are also related to readers and books (reader type and book type).
I realize the project process:
- user management
- Basic information maintenance
- Reader information management
- Library information management
- Book borrowing management
When writing a project, we first have to figure out how to build the block diagram of the project. I refer to the project framework, an interface display area (view), an entity class area (domain) and a function implementation area (service); because the project uses IO flow knowledge, a package must be used to load the file content.
experience
[1] Because the implementation code of IO stream must involve file reading and writing operations, I use object serialization, and I can also use the Properties collection. However, because there are too many Properties involved in the class, I can't skillfully apply split segmentation, and finally choose serialization. Since the file needs to be read and written repeatedly, I can encapsulate it into the tool class! What's the acquisition time Can also be encapsulated.
//Deserialization public static ArrayList deserialization(File f) throws IOException, ClassNotFoundException { ArrayList<Object> o = null; if (!f.exists()) { f.createNewFile(); } else { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f)); Object obj = ois.readObject(); o = (ArrayList) obj; ois.close(); } return o; } //serialize public static void serialization(ArrayList<Object> r, File f) throws IOException { if (!f.exists()) { f.createNewFile(); } else { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f)); oos.writeObject(r); oos.close(); } }
//current time public static String currentTime() { Date time = new Date(); SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); return ft.format(time); } //Judge gender public static char isSex() { char c; for (; ; ) { String str = readKeyBoard(1, false).toUpperCase(); c = str.charAt(0); if (c == 'male' || c == 'female') { break; } else { System.out.print("Selection error, please re-enter:"); } }
[2] when writing reader and book related content, when defining entity classes, the attribute should not be defined as the basic data type, otherwise the subsequent content cannot be saved in files and the project requirements cannot be realized.
[3] Most of the items are added, deleted, modified and queried. Write one corresponding class and the other. The code implementation is almost the same. You can Ctrl+C and Ctrl+V
In fact, the most difficult part of the project is the borrowing and return of books, as well as the setting of fines. Because I don't know how to use loop nesting, when writing a loan, the for loop and while loop dizzy my brain. I really can't stand it. Here are the relevant codes for borrowing books.
private static File file = new File("project2\\src\\file\\borrowInfo.txt"); //Borrow books public static void borrowBooks() throws IOException, ClassNotFoundException { int readerId = 0; //Reader id String readerName = null;//Reader name int bookId = 0;//Book id String bookName = null;//Book name int count = 0;//Borrowing times limit String typeName = null; //Reader type int maxborrownum = 0; //Maximum borrowing days int limit = 0; boolean flag = true; while (flag) { File file1 = new File("project2\\src\\file\\readerInfo.txt"); ArrayList readerArray = TSUtility.deserialization(file1); System.out.println("readers id\t full name\t Gender age\t Telephone\t\t Department\t\t category\t Loanable quantity\t Borrowing days"); for (int i = 0; i < readerArray.size(); i++) { Readers readers = (Readers) readerArray.get(i); System.out.println(readers.getReaderId() + "\t\t" + readers.getName() + "\t" + readers.getSex() + "\t" + readers.getAge() + "\t" + readers.getPhoneNumber() + "\t" + readers.getFaculty() + "\t\t" + readers.getRt().getName() + "\t\t" + readers.getRt().getLimit() + "\t\t" + readers.getRt().getMaxborrownum()); } boolean flags = true; while (flags) { System.out.println("Please enter the readers you want to select id: "); readerId = TSUtility.readBookBorrowInt(); for (int i = 0; i < readerArray.size(); i++) { Readers r = (Readers) readerArray.get(i); if (readerId == r.getReaderId()) { readerName = r.getName(); typeName = r.getRt().getName(); //Reader category count = r.getRt().getLimit(); //Loanable quantity maxborrownum = r.getRt().getMaxborrownum(); //Maximum borrowing days flags = false; break; } } if (flags) { System.out.println("No such reader id,Please re-enter!"); } } if (count == 0) { System.out.println("The number of books borrowed by this reader has been used up. Please return the books first"); flag = false; } else { File file2 = new File("project2\\src\\file\\bookInfo.txt"); ArrayList bookArray = TSUtility.deserialization(file2); System.out.println("books id\t Book number\t \t Book name author\t Book unit price\t press\t\t Publication date"); for (int j = 0; j < bookArray.size(); j++) { Books books = (Books) bookArray.get(j); /*int id = books.getBt().getId(); String name = books.getBt().getName();*/ System.out.println(books.getBookId() + "\t\t" + books.getISBN() + "\t" + books.getBookName() + "\t " + books.getAuthor() + "\t " + books.getMoney() + "\t " + books.getPublisher() + "\t\t" + books.getPublishDate()); } boolean flagss = true; while (flagss) { boolean f1 = true; //Do you want to borrow this book System.out.println("Please enter the book of your choice id: "); bookId = TSUtility.readBookBorrowInt(); for (int i = 0; i < bookArray.size(); i++) { Books books = (Books) bookArray.get(i); if (bookId == books.getBookId()) { bookId = books.getBookId(); bookName =books.getBookName(); flagss = false; break; } } if (flagss) { System.out.println("No such book id,Please re-enter!"); } ObjectInputStream oos2 = new ObjectInputStream(new FileInputStream("project2\\src\\file\\borrowInfo.txt")); Object obj = oos2.readObject(); ArrayList array2 = (ArrayList) obj; for (int i = 0; i < array2.size(); i++) { BorrowBook bb = (BorrowBook) array2.get(i); if (bb.getReaderId() == readerId && bb.getBookId() == bookId) { f1 = false; flagss = true; System.out.println("The reader has borrowed the book!"); break; } } if (f1) { //Borrowing time String borrowTime = TSUtility.currentTime(); if (file.length() == 0) { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("project2\\src\\file\\borrowInfo.txt")); ArrayList<BorrowBook> array = new ArrayList<>(); BorrowBook bb = new BorrowBook(readerId, readerName, bookId, bookName, borrowTime); array.add(bb); // count--; oos.writeObject(array); System.out.println("Borrowing succeeded"); flag = false; flagss = false; } else if (file.length() != 0) { ArrayList array1 = TSUtility.deserialization(file); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("project2\\src\\file\\borrowInfo.txt")); BorrowBook bb = new BorrowBook(readerId, readerName, bookId, bookName, borrowTime); array1.add(bb); oos.writeObject(array1); System.out.println("*****************************************"); System.out.println(); System.out.println("The reader's borrowing is as follows:"); System.out.println("readers id\t Reader name\t books id\t Book name\t hours of loan service"); System.out.println(readerId + "\t\t" + readerName + "\t\t" + bookId + "\t" + bookName + "\t" + borrowTime); for (int i = 0; i < readerArray.size(); i++) { Readers r = (Readers) readerArray.get(i); if (readerId == r.getReaderId()) { count--; System.out.println("You can also borrow it" + count + "This book~·"); r.setRt(new ReaderType(typeName, count, maxborrownum)); break; } } ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream("project2\\src\\file\\readerInfo.txt")); oos1.writeObject(readerArray); flag = false; flagss = false; } } } } } }