經(jīng)過一段時間的JAVA實訓(xùn)學習,由以前的一竅不通,變成了今天的菜鳥,稍有成就感,o(∩_∩)o
下面就寫一個小程序,把已經(jīng)學過的知識都捎帶著復(fù)習一下.. .. ..
先說程序的要求:小小的簡單的學生信息管理系統(tǒng)。在控制臺上顯示四個選項,分別為增加、瀏覽、修改、退出系統(tǒng);然后選擇相應(yīng)的功能進行操作。
先寫主菜單吧,寫上增加、瀏覽、修改和退出功能,這些功能當然不能寫在這里,有下面的程序去實現(xiàn)。
main和menu:
main package com.dr.demo.main; import com.dr.demo.menu.Menu; public class Main { public static void main(String[] args) { new Menu(); } }
這就是程序的入口,超簡潔。程序的所有代碼都看不到,被封裝在了后面。
menu:
menu package com.dr.demo.menu; import com.dr.demo.op.PersonOperate; import com.dr.demo.util.InputData; public class Menu { InputData input = null; public Menu(){ this.input = new InputData(); //循環(huán)出現(xiàn)菜單 while(true){ this.show(); } } //需要定義的菜單內(nèi)容 public void show(){ System.out.println("\t\t\t1、增加人員信息"); System.out.println("\t\t\t2、瀏覽人員信息"); System.out.println("\t\t\t3、修改人員信息"); System.out.println("\t\t\t4、退出系統(tǒng)"); System.out.print("\n\n請選擇要使用的操作:"); int temp = input.getInt(); switch(temp){ case 1:{ // 增加人員信息 new PersonOperate().add(); //業(yè)務(wù)處理層 break; } case 2:{ // 瀏覽人員信息 new PersonOperate().show(); break; } case 3:{ // 修改人員信息 new PersonOperate().update(); break; } case 4:{ //退出系統(tǒng) System.out.println("選擇的是退出系統(tǒng)"); System.out.println("系統(tǒng)退出!"); System.exit(1); } default:{ System.out.println("輸入的內(nèi)容不正確"); break; } } } }
開始是一段死循環(huán)的代碼,讓主菜單總是顯示。
然后是switch--case,根據(jù)輸入的內(nèi)容執(zhí)行相應(yīng)的case。
退出系統(tǒng)就不用說了,三個功能分別由一下的代碼實現(xiàn):
增加,要知道增加的是什么,增加到什么地方(其他功能雷同)。Person類,然后便是這些功能的實現(xiàn)。
Person:
person package com.dr.demo.vo; import java.io.Serializable; @SuppressWarnings("serial") public class Person implements Serializable{ private String name; private int age; private float score; public Person(){} public Person(String name, int age, float score) { this.name = name; this.age = age; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } public String toString(){ return "姓名:"+this.name+",年齡:"+this.age+",成績:"+this.score; } }
Person類實現(xiàn)Serializable接口(可序列化);設(shè)置Person的屬性,實現(xiàn)set、get等方法。
PersonOperate,寫出主程序中的功能的方法。
PersonOperate package com.dr.demo.op; import com.dr.demo.util.FileOperate; import com.dr.demo.util.InputData; import com.dr.demo.vo.Person; public class PersonOperate { private InputData input = null; public PersonOperate(){ this.input = new InputData(); } //完成具體的Person對象操作 public void add(){ //要使用輸入數(shù)據(jù)的類 String name = null; int age = 0; float score = 0.0f; System.out.print("輸入姓名:"); name = this.input.getString(); System.out.print("輸入年齡:"); age = this.input.getInt(); System.out.print("輸入成績:"); score = this.input.getFloat(); //生成Person對象,把對象保存在文件中 Person p = new Person(name,age,score); try{ new FileOperate().save(p); //io操作層 System.out.println("數(shù)據(jù)保存成功!"); }catch(Exception e){ System.out.println("數(shù)據(jù)保存失敗!"); } } public void show(){ //從文件中把內(nèi)容讀進來 Person p = null; try{ p = (Person) new FileOperate().read(); }catch(Exception e){ System.out.println("內(nèi)容顯示失敗,請確定數(shù)據(jù)是否存在!"); } if(p != null){ System.out.println(p); } } public void update(){ //先將之前的信息查出來 Person p = null; try{ p = (Person) new FileOperate().read(); }catch(Exception e){ System.out.println("內(nèi)容顯示失敗,請確定數(shù)據(jù)是否存在!"); } if(p != null){ String name = null; int age = 0; float score =0.0f; System.out.print("請輸入新的姓名(原姓名:"+p.getName()+")"); name = this.input.getString(); System.out.print("請輸入新的年齡(原年齡:"+p.getAge()+")"); age = this.input.getInt(); System.out.print("請輸入新的成績(原成績:"+p.getScore()+")"); score = this.input.getFloat(); //信息重新設(shè)置 p.setName(name); p.setAge(age); p.setScore(score); try{ new FileOperate().save(p); System.out.println("數(shù)據(jù)更新成功!"); }catch(Exception e){ System.out.println("數(shù)據(jù)更新失敗!"); } } } }
這些數(shù)據(jù)都通過IO保存到文件中。add中的save,show中的read,update則read和save都用。
還有add和update都要識別輸入數(shù)據(jù),都要實現(xiàn)。
Input:
InputDate package com.dr.demo.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class InputData { private BufferedReader buf =null; public InputData(){ buf = new BufferedReader(new InputStreamReader(System.in)); }; public String getString(){ String str = null; try { str = buf.readLine(); } catch (IOException e) {} return str; } public int getInt(){ int temp = 0; //如果輸入的不是數(shù)字,至少應(yīng)該有一個提示,告訴用戶輸入錯了~ //可以使用正則驗證 String str = null; boolean flag = true; while(flag){ //輸入數(shù)據(jù) str = this.getString(); if (!(str.matches("\\d+"))){ //如果輸入的不是一個數(shù)字,則必須重新輸入 System.out.print("輸入的內(nèi)容必須是整數(shù),請重新輸入:"); }else{ //輸入的是一個正確的數(shù)字,則可以進行轉(zhuǎn)換 temp = Integer.parseInt(str); //表示退出循環(huán) flag = false; } } return temp; } public float getFloat(){ float f = 0.0f; //如果輸入的不是數(shù)字,至少應(yīng)該有一個提示,告訴用戶輸入錯了~ //可以使用正則驗證 String str = null; boolean flag = true; while(flag){ //輸入數(shù)據(jù) str = this.getString(); if (!(str.matches("\\d+?.\\d{1,2}"))){ //如果輸入的不是一個數(shù)字,則必須重新輸入 System.out.print("輸入的內(nèi)容必須是小數(shù)(小數(shù)點后兩位),請重新輸入:"); }else{ //輸入的是一個正確的數(shù)字,則可以進行轉(zhuǎn)換 f = Float.parseFloat(str); //表示退出循環(huán) flag = false; } } return f; } }
FileOperate:
FileOperate package com.dr.demo.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import com.dr.demo.vo.Person; public class FileOperate { public static final String FILENAME = "E:\\person.ser"; //把對象保存在文件之中 public void save(Object obj){ ObjectOutputStream out = null; try { out = new ObjectOutputStream(new FileOutputStream(new File(FILENAME))); //寫入對象 out.writeObject(obj); }catch(Exception e){ try { throw e; } catch (Exception e1) {} }finally { try { out.close(); }catch(Exception e){} } } //把對象從文件之中讀出來 public Object read() throws Exception{ Object obj = null; ObjectInputStream input =null; try { input = new ObjectInputStream(new FileInputStream(new File(FILENAME))); obj = input.readObject(); } catch (Exception e) { throw e; }finally{ try{ input.close(); }catch(Exception e){} } return obj; } }
寫完這些代碼就可以實現(xiàn)增加、瀏覽、修改、退出系統(tǒng)的功能了。
運行結(jié)果: