O código deste artigo é um sistema de gerenciamento simples original, que executa apenas testes funcionais. Não melhorou todas as funções devidas, mas pesquisou apenas a entrada e saída apenas para referência!
Seção de menu:
importar java.util.scanner; menu de classe pública {int min = 1; int max = 3; public void getMenu () {System.out.println ("1, display/2, entrada/3, encontre"); } public void getFindMenu () {System.out.println ("1, número/2, título do livro/3, autor"); } public int setMenu () {System.out.println ("Número de série de entrada:"); Scanner Reader = new Scanner (System.in); int num = reader.NextInt (); if (num> = min || num <= max) retorna num; caso contrário, retorne -1; }}Peças de gerenciamento -chave:
importar java.io.file; importar java.io.fileNotfoundException; importar java.io.fileOutputStream; importar java.util.scanner; importar java.io.ioException; public class Book {public void find () {menu do novo menu (); MENU.GETFINDMENU (); Scanner Reader = new Scanner (System.in); int num = menu.setMenu (); switch (num) {case 1: system.out.println ("digite o número"); Encontre (leitor.next (), 0); quebrar; Caso 2: System.out.println ("Por favor, insira o título do livro"); Encontre (leitor.next (), 1); quebrar; Caso 3: System.out.println ("Digite o autor"); Encontre (leitor.next (), 2); quebrar; }} public void find (string s, int n) {try {scanner in = new scanner (novo arquivo ("res/book.txt")); while (in.hasNextLine ()) {String str = in.NextLine (); String [] book = str.trim (). Split ("#"); if (livro [n] .compareto (s) == 0) system.out.println (livro [0] + "" + livro [1] + "" + livro [2]); }} catch (filenotfoundException e) {e.printStackTrace (); }} public string findnum (string s, int n) {try {scanner in = new scanner (novo arquivo ("res/book.txt")); while (in.hasNextLine ()) {String str = in.NextLine (); String [] book = str.trim (). Split ("#"); if (livro [n] .compareto (s) == 0) retornar livro [n]; }} catch (filenotfoundException e) {e.printStackTrace (); } retornar "não encontrado"; } public string message () {scanner leitor = new Scanner (System.in); String str = ""; String s = ""; System.out.println ("Digite o número"); str = leitor.Next (); if (findNum (str, 0) .compareto ("não encontrado")! = 0) {System.out.println ("Este número tem um erro de entrada"); retornar "@@!!"; } s + = str + "#"; System.out.println ("Por favor, digite o título"); str = leitor.Next (); s + = str + "#"; System.out.println ("Insira o autor"); str = leitor.Next (); s + = str + "#/n"; retorno s; } public void SetBook () {FileOutputStream fop = null; Arquivo de arquivo; String content = message (); if (content.comPareto ("@@ !!") == 0) retornar; tente {file = new File ("res/book.txt"); fop = new FileOutputStream (arquivo, true); byte [] contentInbytes = content.getBytes (); fop.write (contentInbytes); fop.flush (); fop.close (); System.out.println ("done"); } catch (ioexception e) {e.printStackTrace (); } finalmente {tente {if (fop! = null) {fop.close (); }} catch (ioexception e) {e.printStackTrace (); }}} public void getBook () {try {scanner in = new Scanner (novo arquivo ("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 (livro [i]); } System.out.println ("/n ************************"); livro de retorno; }} Parte da função principal:
classe pública ManageBook {public static void main (string [] agse) {menu menu = new Menu (); Livro do livro = novo livro (); while (true) {menu.getMenu (); int num = menu.setMenu (); switch (num) {case 1: book.getbook (); quebrar; Caso 2: book.setbook (); quebrar; Caso 3: book.find (); quebrar; caso -1: System.out.println ("entrada está incorreta"); quebrar; }}}}Para obter mais informações sobre o sistema de gerenciamento, clique em "Tópico especial do sistema de gerenciamento" para aprender
O exposto acima é todo o conteúdo deste artigo. Espero que seja útil para o aprendizado de todos e espero que todos apoiem mais o wulin.com.