初學Java7:簡單設計人力資源管理系統,

來源:互聯網
上載者:User

初學Java7:簡單設計人力資源管理系統,

1,人力資源管理系統,實現查詢、添加、修改、刪除的功能。
2,寫查詢,添加,修改,刪除四個方法,並能實現將輸入的資料儲存到硬碟,待下次啟動程式能從硬碟載入出來。
3,異常處理,資料有效性驗證。
4,單獨建立一個員工類(public class Person),屬性包括編號、姓名、性別、年齡。
5,HRMS原始碼:

  1 package azhi0901;  2   3 import java.io.*;  4 import java.util.*;  5   6 public class HRMS {  7     // 定義輸入  8     public static Scanner sc = new Scanner(System.in);  9     // 定義Map索引值對存輸入資料 10     public static HashMap<Integer, Person> map = new HashMap<Integer, Person>(); 11  12     // 主方法入口 13     // 全文的異常有的用“throws Exception”的方式拋出,(這個簡單) 14     // 有的用“try...catch”形式拋出,(這個更好) 15     public static void main(String[] args) throws Exception { 16         loadData();// 載入資料 17         while (true) {// 死迴圈 18             getLine(3);// 空出3行,方便查看 19             System.out.println("**********************************************"); 20             System.out.println("*            人力資源管理系統                 *"); 21             System.out.println("**********************************************"); 22             System.out.println("*             1,查看員工資訊                  *"); 23             System.out.println("*             2,添加員工資訊                  *"); 24             System.out.println("*             3,修改員工資訊                  *"); 25             System.out.println("*             4,刪除員工資訊                  *"); 26             System.out.println("*             0,退出系統                      *"); 27             System.out.println("**********************************************"); 28             System.out.print("請選擇:"); 29             int num = sc.nextInt();// 輸入num選擇菜單 30             if (num == 0) { 31                 saveData(); 32                 System.out.println("謝謝使用!"); 33                 break; 34             } else { 35                 switch (num) { 36                 case 1: 37                     query();// 查詢 38                     break; 39                 case 2: 40                     add();// 添加 41                     break; 42                 case 3: 43                     update();// 修改 44                     break; 45                 case 4: 46                     del();// 刪除 47                 } 48             } 49         } 50     } 51  52     // 載入資料 53     private static void loadData() throws Exception { 54         // 定義位元組流,從F:/HRMS.txt載入資料 55         FileInputStream in = new FileInputStream("F:/HRMS.txt"); 56         // 轉成字元流 57         InputStreamReader isr = new InputStreamReader(in); 58         // 轉化成緩衝字元流 59         BufferedReader br = new BufferedReader(isr); 60         // 逐行讀取 61         String s = br.readLine(); 62         // 分拆資料 63         while (s != null) { 64             // s.split("\t")API的解釋:根據給定Regex的匹配拆分此字串。 65             // Regex??? 66             String[] ss = s.split("\t"); 67             // 資料類型轉化 68             int id = Integer.parseInt(ss[0]); 69             String name = ss[1]; 70             String sex = ss[2]; 71             int age = Integer.parseInt(ss[3]); 72             // 封裝到Peron 73             Person p = new Person(id, name, sex, age); 74             // 放入map 75             map.put(p.getId(), p); 76             // 讀出下一行 77             s = br.readLine(); 78         } 79         // 這個關閉輸入資料流的我問了老師,為什麼關閉in?isr和br也可以關閉的。 80         // 解釋:in是底層的,關閉in就夠了 81         in.close(); 82     } 83  84     // 儲存資料 85     public static void saveData() throws Exception { 86         // 在退出的時候將整個map裡的資料全部儲存 87         FileOutputStream out = new FileOutputStream("F:/HRMS.txt"); 88         OutputStreamWriter osw = new OutputStreamWriter(out); 89         // 轉成列印流[System.out.println()控制台] 90         PrintWriter pw = new PrintWriter(osw, true); 91         // 遍曆map 92         // map.keySet():返回此映射中包含的鍵的 Set 視圖。(API) 93         Set<Integer> keys = map.keySet();// 所有鍵 94         for (int id : keys) { 95             Person p = map.get(id); 96             String s = p.getId() + "\t" + p.getName() + "\t" + p.getSex() + "\t" + p.getAge(); 97             // 寫入檔案 98             pw.println(s); 99         }100         out.close();101     }102 103     // 查詢104     public static void query() {105         getLine(3);106         System.out.println("==>查詢員工:" + "\n");107         System.out.println("編號\t" + "姓名\t" + "性別\t" + "年齡");108         Set<Integer> keys = map.keySet();109         for (Integer integer : keys) {110             Person p = map.get(integer);111             System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getSex() + "\t" + p.getAge());112         }113     }114 115     // 添加116     public static void add() {117         // 編號,姓名,性別,年齡118         System.out.println("==>添加員工:" + "\n");119         try {120             int id = 1;121             System.out.print("姓名:");122             String name = sc.next();123 124             System.out.print("性別:");125             String sex = sc.next();126 127             System.out.print("年齡:");128             int age = sc.nextInt();129 130             Person person = new Person(id, name, sex, age);131             map.put(person.getId(), person);132             System.out.println("添加成功!");133         } catch (Exception e) {134             System.out.println(e.getMessage());135             System.out.println("添加失敗!");136         }137 138     }139 140     // 修改141     public static void update() {142         try {143             System.out.println("==>修改員工:" + "\n");144             System.out.print("請輸入您要修改員工的編號:");145             int Id = sc.nextInt();146             if (map.containsKey(Id)) {147                 System.out.println("\n編號\t" + "姓名\t" + "性別\t" + "年齡");148                 Person p = map.get(Id);149                 System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getSex() + "\t" + p.getAge());150                 System.out.println("\n請輸入你要修改的 1,姓名 2,性別 3,年齡 :");151                 int num2 = sc.nextInt();152                 switch (num2) {153                 case 1:154                     System.out.print("姓名:");155                     String name = sc.next();156                     map.get(Id).setName(name);157                     break;158                 case 2:159                     System.out.print("性別:");160                     String sex = sc.next();161                     map.get(Id).setSex(sex);162                     break;163                 case 3:164                     System.out.print("年齡:");165                     int age = sc.nextInt();166                     map.get(Id).setAge(age);167                     break;168                 default:169                     System.out.println("輸入錯誤!");170                     break;171                 }172                 System.out.println("修改成功!");173             }174         } catch (Exception e) {175             System.out.println(e.getMessage());176             System.out.println("修改失敗!");177         }178     }179 180     // 刪除181     public static void del() {182         System.out.println("==>刪除員工:" + "\n");183         System.out.print("請輸入您要刪除員工的編號:");184         int Id = sc.nextInt();185         if (map.containsKey(Id)) {186             System.out.println("\n編號\t" + "姓名\t" + "性別\t" + "年齡");187             Person p = map.get(Id);188             System.out.println(p.getId() + "\t" + p.getName() + "\t" + p.getSex() + "\t" + p.getAge());189             System.out.print("\n確認刪除該員工資訊嗎?y/n:");190             String a = sc.next();191             if (a.equals("y")) {192                 map.remove(Id);193                 System.out.println("\n刪除成功!");194             }195         }196     }197 198     // 取得空行199     public static void getLine(int lines) {200         for (int i = 0; i < lines; i++) {201             System.out.print("\n");202         }203     }204 }

6,Person原始碼:

 1 package azhi0901; 2  3 public class Person { 4     // 類屬性,員工總數 5     static int total = 1; 6     // 以下都為員工屬性 7     private int id; 8     private String name; 9     private String sex;10     private int age;11 12     // 構造器13     Person(int id, String name, String sex, int age) {14         this.id = id;15         if (name == null)16             throw new RuntimeException("輸入姓名無效!");17         this.name = name;18 19         if (sex.equals("男") || sex.equals("女"))20             this.sex = sex;21         else22             throw new RuntimeException("輸入性別無效!");23 24         if (age < 0 || age > 200)25             throw new RuntimeException("輸入年齡無效!");26         this.age = age;27 28         this.id = total;29         total++;30     }31 32     public int getId() {33         return id;34     }35 36     public void setId(int id) {37         this.id = id;38     }39 40     public String getName() {41         return name;42     }43 44     public void setName(String name) {45         if (name == null)46             throw new RuntimeException("輸入姓名無效!");47         this.name = name;48     }49 50     public String getSex() {51         return sex;52     }53 54     public void setSex(String sex) {55         if (sex.equals("男") || sex.equals("女"))56             this.sex = sex;57         else58             throw new RuntimeException("輸入性別無效!");59 60     }61 62     public int getAge() {63         return age;64     }65 66     public void setAge(int age) {67         if (age < 0 || age > 200)68             throw new RuntimeException("輸入年齡無效!");69         this.age = age;70     }71 72 }

  這個代碼還有一些bug,之後還會修改。 

7,效果預覽

  添加

  

  資料有效性驗證

  查詢

 

  修改

  

  刪除

  檔案

  這次做的這個人力資源管理系統,綜合性較以往的增加了不少,需要用到的東西也很多,算是之前所學知識點的一個集合運用吧。其中大概需要用到的重點有:String,Integer,資料類型的轉換,階層中的map儲存索引值對,類的聲明與執行個體化,構造方法,一般方法,方法重寫,this關鍵字,static關鍵字,資料有效性驗證,異常處理,輸入輸出等等。對於選擇的if,switch和迴圈的for,while一直都是最基礎的重點。我個人對以上的一些東西掌握也還不熟悉,有的代碼需要參照老師的案列才能寫出來,額...多練吧。有一點需要特彆強調,我是越來越喜歡編程了!很享受寫代碼的過程,尤其結果出來後更讓人興奮不已。雖然很菜,但是就像你不能阻止一個平凡且能力低微的人對一個美好事物的追求並為之努力改變、提升。另外對於自己寫部落格這事,開始是想著記錄自己所學的經曆,當然現在也是。那天跟一個學java已久的同學聊到我在寫部落格,他說“你牛!”,貌似寫部落格的都是大神,而我這樣的菜鳥算是無知無畏吧。我會堅持下去!

 

                                              A_zhi

                                             2016/9/4     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.