初学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("	")API的解释:根据给定正则表达式的匹配拆分此字符串。
 65             // 正则表达式???
 66             String[] ss = s.split("	");
 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() + "	" + p.getName() + "	" + p.getSex() + "	" + 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("==>查询员工:" + "
");
107         System.out.println("编号	" + "姓名	" + "性别	" + "年龄");
108         Set<Integer> keys = map.keySet();
109         for (Integer integer : keys) {
110             Person p = map.get(integer);
111             System.out.println(p.getId() + "	" + p.getName() + "	" + p.getSex() + "	" + p.getAge());
112         }
113     }
114 
115     // 添加
116     public static void add() {
117         // 编号,姓名,性别,年龄
118         System.out.println("==>添加员工:" + "
");
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("==>修改员工:" + "
");
144             System.out.print("请输入您要修改员工的编号:");
145             int Id = sc.nextInt();
146             if (map.containsKey(Id)) {
147                 System.out.println("
编号	" + "姓名	" + "性别	" + "年龄");
148                 Person p = map.get(Id);
149                 System.out.println(p.getId() + "	" + p.getName() + "	" + p.getSex() + "	" + p.getAge());
150                 System.out.println("
请输入你要修改的 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("==>删除员工:" + "
");
183         System.out.print("请输入您要删除员工的编号:");
184         int Id = sc.nextInt();
185         if (map.containsKey(Id)) {
186             System.out.println("
编号	" + "姓名	" + "性别	" + "年龄");
187             Person p = map.get(Id);
188             System.out.println(p.getId() + "	" + p.getName() + "	" + p.getSex() + "	" + p.getAge());
189             System.out.print("
确认删除该员工信息吗?y/n:");
190             String a = sc.next();
191             if (a.equals("y")) {
192                 map.remove(Id);
193                 System.out.println("
删除成功!");
194             }
195         }
196     }
197 
198     // 取得空行
199     public static void getLine(int lines) {
200         for (int i = 0; i < lines; i++) {
201             System.out.print("
");
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         else
22             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         else
58             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     

原文地址:https://www.cnblogs.com/a-zhi/p/5839098.html