一個簡陋的java圖書管理系統(tǒng)
本文代碼為原創(chuàng)一個簡陋的管理系統(tǒng),只做功能的測試。并沒有去完善所有應(yīng)有的功能,只做了輸入輸出查找,僅供參考!
菜單部分:
import java.util.Scanner; public class Menu { int Min = 1; int Max = 3; public void getMenu(){ System.out.println("1、顯示/2、輸入/3、查找"); } public void getFindMenu(){ System.out.println("1、編號/2、書名/3、作者"); } public int setMenu(){ System.out.println("輸入序號:"); Scanner reader = new Scanner(System.in); int num = reader.nextInt(); if(num >= Min || num <= Max) return num; else return -1; } }
重點(diǎn)的管理部分:
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("請輸入編號"); Find(reader.next(), 0); break; case 2: System.out.println("請輸入書名"); Find(reader.next(), 1); break; case 3: System.out.println("請輸入作者"); 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 "沒有找到"; } public String message(){ Scanner reader = new Scanner(System.in); String str = ""; String s = ""; System.out.println("請輸入編號"); str = reader.next(); if(findNum(str,0).compareTo("沒有找到") != 0){ System.out.println("此編號存在輸入錯誤"); return "@@!!"; } s += str + "#"; System.out.println("請輸入書名"); str = reader.next(); s += str + "#"; System.out.println("請輸入作者"); 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; } }
主函數(shù)部分:/strong>
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("輸入有誤"); break; } } } }
關(guān)于管理系統(tǒng)的更多內(nèi)容請點(diǎn)擊《管理系統(tǒng)專題》進(jìn)行學(xué)習(xí)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 圖書管理系統(tǒng)java版
- 圖書管理系統(tǒng)java代碼實(shí)現(xiàn)
- JAVA初級項(xiàng)目——實(shí)現(xiàn)圖書管理系統(tǒng)
- java實(shí)現(xiàn)圖書管理系統(tǒng)
- java實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
- JAVA實(shí)現(xiàn)圖書管理系統(tǒng)項(xiàng)目
- Java圖書管理系統(tǒng)課程設(shè)計(jì)
- java實(shí)現(xiàn)簡單圖書管理系統(tǒng)
- 配置idea將Java與數(shù)據(jù)庫連接起來實(shí)現(xiàn)一個簡單的圖書管理系統(tǒng)
相關(guān)文章
java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法
本文主要介紹了java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03springboot整合activity自動部署及部署文件命名流程
這篇文章主要介紹了springboot整合activity自動部署及部署文件命名流程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Java 高并發(fā)五:JDK并發(fā)包1詳細(xì)介紹
本文主要介紹 Java高并發(fā)JDK并發(fā)包1的資料,這里對1.各種同步控制工具的使用 2.并發(fā)容器及典型源碼分析,有需要的小伙伴可以參考下2016-09-09JDBC 實(shí)現(xiàn)通用的增刪改查基礎(chǔ)類方法
下面小編就為大家分享一篇JDBC 實(shí)現(xiàn)通用的增刪改查基礎(chǔ)類方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01