Java實例---簡單的個人管理系統


代碼分析

FileOperate.java

 1 package com.ftl.testperson;
 2 import java.io.File ;
 3 import java.io.FileInputStream ;
 4 import java.io.FileOutputStream ;
 5 import java.io.ObjectInputStream ;
 6 import java.io.ObjectOutputStream ;
 7 public class FileOperate{    // 此類專門用於保存和讀取
 8     private File file = null ;    // 定義文件對象
 9     public FileOperate(String pathName){    // 通過    構造傳遞文件路徑
10         this.file = new File(pathName) ;
11     }
12     public boolean save(Object obj) throws Exception{    // 保存對象
13         ObjectOutputStream oos = null ;        // 對象輸出流
14         boolean flag = false ;    // 定義操作標志位
15         try{
16             oos = new ObjectOutputStream(new FileOutputStream(this.file)) ;    // 實例化對象輸出流
17             oos.writeObject(obj) ;    // 寫入對象
18             flag = true ;
19         }catch(Exception e){
20             throw e ;    // 有異常交給被調用處處理
21         }finally{
22             if(oos!=null){
23                 oos.close() ;
24             }
25         }
26         return flag ;
27     }
28     public Object load() throws Exception{    // 讀取對象
29         Object obj = null ;    // 接收讀取的內容
30         ObjectInputStream ois = null ;    
31         try{    
32             ois = new ObjectInputStream(new FileInputStream(this.file)) ;    // 實例化對象輸入流
33             obj = ois.readObject() ;    // 讀取對象
34         }catch(Exception e){
35             throw e ;
36         }finally{
37             if(ois!=null){
38                 ois.close() ;    // 關閉
39             }
40         }
41         return obj ;
42     }
43 };
View Code

InputData.java

 1 package com.ftl.testperson;
 2 
 3 import java.io.* ;
 4 import java.util.* ;
 5 import java.text.* ;
 6 public class InputData{
 7     private BufferedReader buf = null ;
 8     public InputData(){// 只要輸入數據就要使用此語句
 9         this.buf = new BufferedReader(new InputStreamReader(System.in)) ;
10     }
11     public String getString(String info){    // 得到字符串信息
12         String temp = null ;
13         System.out.print(info) ;    // 打印提示信息
14         try{
15             temp = this.buf.readLine() ;    // 接收數據
16         }catch(IOException e){
17             e.printStackTrace() ;
18         }
19         return temp ;
20     }
21     public int getInt(String info,String err){    // 得到一個整數的輸入數據
22         int temp = 0 ;
23         String str = null ;
24         boolean flag = true ;    // 定義一個標記位
25         while(flag){
26             str = this.getString(info) ;    // 接收數據
27             if(str.matches("^\\d+$")){    // 判斷是否由數字組成
28                 temp = Integer.parseInt(str) ;    // 轉型
29                 flag = false ;    // 結束循環
30             }else{
31                 System.out.println(err) ;    // 打印錯誤信息
32             }
33         }
34         return temp ;
35     }
36     public float getFloat(String info,String err){    // 得到一個小數的輸入數據
37         float temp = 0 ;
38         String str = null ;
39         boolean flag = true ;    // 定義一個標記位
40         while(flag){
41             str = this.getString(info) ;    // 接收數據
42             if(str.matches("^\\d+.?\\d+$")){    // 判斷是否由數字組成
43                 temp = Float.parseFloat(str) ;    // 轉型
44                 flag = false ;    // 結束循環
45             }else{
46                 System.out.println(err) ;    // 打印錯誤信息
47             }
48         }
49         return temp ;
50     }
51     public Date getDate(String info,String err){    // 得到一個小數的輸入數據
52         Date temp = null ;
53         String str = null ;
54         boolean flag = true ;    // 定義一個標記位
55         while(flag){
56             str = this.getString(info) ;    // 接收數據
57             if(str.matches("^\\d{4}-\\d{2}-\\d{2}$")){    // 判斷是否由數字組成
58                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd") ;
59                 try{
60                     temp = sdf.parse(str) ;    // 將字符串變為Date型數據
61                 }catch(Exception e){}
62                 flag = false ;    // 結束循環
63             }else{
64                 System.out.println(err) ;    // 打印錯誤信息
65             }
66         }
67         return temp ;
68     }
69 };
View Code

Menu.java

 1 package com.ftl.testperson;
 2 
 3 public class Menu
 4 {
 5     public void start(){
 6         while(true){
 7             try {
 8                 Menu.show();
 9             } catch (Exception e) {
10                 // TODO 自動生成的 catch 塊
11                 e.printStackTrace();
12             }
13         }
14     }
15     public static void show() throws Exception
16     {
17         System.out.println("===================個人系統===================");
18         System.out.println("                  【1】、增加數據                                              ");
19         System.out.println("                  【2】、刪除數據                                              ");
20         System.out.println("                  【3】、修改數據                                              ");
21         System.out.println("                  【4】、查看數據                                              ");
22         System.out.println("                  【0】、退出系統                                              ");
23         InputData input = new InputData();
24         int i = input.getInt("請選擇:", "請輸入正確的選項!");
25         switch(i)
26         {
27             case 1:
28             {
29                 Operate.add();
30                 break;
31             }
32             case 2:
33             {
34                 Operate.delete();
35                 break;
36             }
37             case 3:
38             {
39                 Operate.update();
40                 break;
41             }
42             case 4:
43             {
44                 Operate.find();
45                 break;
46             }
47             case 0:
48             {
49                 System.exit(1);
50                 break;
51             }
52             default:
53             {
54                 System.out.println("請輸入正確的操作!!!");
55             }
56         }
57     }
58 }
View Code

Operate.java

 1 package com.ftl.testperson;
 2 public class Operate{
 3     public static void add(){    // 增加操作
 4         InputData input = new InputData() ;        // 實例化輸入數據對象
 5         FileOperate fo = new FileOperate("E:\\ test.txt") ;
 6         String name = input.getString("請輸入姓名:") ;
 7         int age = input.getInt("請輸入年齡:" , "年齡必須是數字!") ;
 8         Person per = new Person(name,age) ;    // 實例化Person對象
 9         try{
10             fo.save(per) ;    // 保存對象
11         }catch(Exception e){
12             e.printStackTrace() ;
13         }
14         System.out.println("信息增加成功!") ;
15     }
16     public static void delete(){    // 刪除操作
17         FileOperate fo = new FileOperate("d:\\test.per") ;
18         try{
19             fo.save(null) ;    // 保存對象
20         }catch(Exception e){
21             e.printStackTrace() ;
22         }
23         System.out.println("信息刪除成功!") ;
24     }
25     public static void update(){    // 更新操作
26         InputData input = new InputData() ;        // 實例化輸入數據對象
27         FileOperate fo = new FileOperate("d:\\test.per") ;
28         Person per = null ;
29         try{
30             per = (Person)fo.load() ;    // 讀取對象
31         }catch(Exception e){
32             e.printStackTrace() ;
33         }
34         String name = input.getString("請輸入姓名(原姓名:"+per.getName()+"):") ;
35         int age = input.getInt("請輸入年齡(原年齡:"+per.getAge()+"):" , "年齡必須是數字!") ;
36         per = new Person(name,age) ;    // 實例化Person對象
37         try{
38             fo.save(per) ;    // 保存對象
39         }catch(Exception e){
40             e.printStackTrace() ;
41         }
42         System.out.println("信息修改成功!") ;
43     }
44     public static void find(){    // 查看操作
45         FileOperate fo = new FileOperate("d:\\test.per") ;
46         Person per = null ;
47         try{
48             per = (Person)fo.load() ;    // 讀取對象
49         }catch(Exception e){
50             e.printStackTrace() ;
51         }
52         System.out.println(per) ;
53     }
54 };
View Code

Person.java

 1 package singleperson;
 2 
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 import java.io.Serializable;
 6 
 7 import javax.swing.JFrame;
 8 
 9 public class Person implements Serializable {
10     private String name;
11     private int age;
12     @Override
13     public String toString() {
14         return "姓名:"  + this.getName() + "\t " + "年齡:"   + this.getAge() + "\n";
15     }
16     public Person(String name, int age) {
17         super();
18         this.name = name;
19         this.age = age;
20     }
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public int getAge() {
28         return age;
29     }
30     public void setAge(int age) {
31         this.age = age;
32     }
33 }
View Code

MainTest.java

1 package com.ftl.testperson;
2 
3 public class MainTest {
4     public static void main(String[] args) throws Exception {
5         Menu menu = new Menu();
6         menu.start();
7     }
8 }
View Code

 

程序截圖:

源碼下載

點擊下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM