The code of this article is an original simple management system, which only performs functional testing. It has not improved all the due functions, but only searched input and output for reference only!
Menu section:
import java.util.Scanner;public class Menu { int Min = 1; int Max = 3; public void getMenu(){ System.out.println("1, display/2, input/3, find"); } public void getFindMenu(){ System.out.println("1, number/2, book title/3, author"); } public int setMenu(){ System.out.println("Input serial number:"); Scanner reader = new Scanner(System.in); int num = reader.nextInt(); if(num >= Min || num <= Max) return num; else return -1; }}Key management parts:
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.util.Scanner;import java.io.IOException;public class Book { public void find(){ Menu menu = new Menu(); menu.getFindMenu(); Scanner reader = new Scanner(System.in); int num = menu.setMenu(); switch(num){ case 1: System.out.println("Please enter the number"); Find(reader.next(), 0); break; case 2: System.out.println("Please enter the title of the book"); Find(reader.next(), 1); break; case 3: System.out.println("Please enter the author"); Find(reader.next(), 2); break; } } public void Find(String s,int n){ try { Scanner in = new Scanner(new File("res/Book.txt")); while (in.hasNextLine()) { String str = in.nextLine(); String[] book = str.trim().split("#"); if(book[n].compareTo(s) == 0) System.out.println(book[0] +" "+ book[1] +" "+ book[2]); } } catch (FileNotFoundException e) { e.printStackTrace(); } } public String findNum(String s,int n){ try { Scanner in = new Scanner(new File("res/Book.txt")); while (in.hasNextLine()) { String str = in.nextLine(); String[] book = str.trim().split("#"); if(book[n].compareTo(s) == 0) return book[n]; } } catch (FileNotFoundException e) { e.printStackTrace(); } return "Not found"; } public String message(){ Scanner reader = new Scanner(System.in); String str = ""; String s = ""; System.out.println("Please enter the number"); str = reader.next(); if(findNum(str,0).compareTo("Not found") != 0){ System.out.println("This number has an input error"); return "@@!!"; } s += str + "#"; System.out.println("Please enter the title"); str = reader.next(); s += str + "#"; System.out.println("Please enter the author"); str = reader.next(); s += str + "#/n"; return s; } public void setBook() { FileOutputStream fop = null; File file; String content = message(); if(content.compareTo("@@!!") == 0) return ; try { file = new File("res/Book.txt"); fop = new FileOutputStream(file,true); byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fop != null) { fop.close(); } } catch (IOException e) { e.printStackTrace(); } } } public void getBook() { try { Scanner in = new Scanner(new File("res/Book.txt")); while (in.hasNextLine()) { String str = in.nextLine(); splitt(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } } public static String[] splitt(String str) { String[] book = str.trim().split("#"); for (int i = 0; i < book.length; i++) { System.out.println(book[i]); } System.out.println("/n************************"); return book; }} Main function part:
public class ManageBook { public static void main(String[] agse){ Menu menu = new Menu(); Book book = new Book(); while(true){ menu.getMenu(); int num = menu.setMenu(); switch(num){ case 1: book.getBook(); break; case 2: book.setBook(); break; case 3: book.find(); break; case -1: System.out.println("Input is incorrect"); break; } } }}For more information about the management system, please click "Management System Special Topic" to learn
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.