Java 7: A simple design of the Human Resource Management System,
1. The human resource management system provides the query, add, modify, and delete functions.
2. Four methods are provided: Write query, add, modify, and delete. The input data can be saved to the hard disk. The program can be loaded from the hard disk next time.
3. Exception Handling and Data Validity verification.
4. Create a public class Person. The attributes include the number, name, gender, and age.
5. HRMS source code:
1 package azhi0901; 2 3 import java. io. *; 4 import java. util. *; 5 6 public class HRMS {7 // define input 8 public static completion SC = new partition (System. in); 9 // defines 10 public static HashMap <Integer, Person> Map = new HashMap <Integer, Person> (); 11 12 // main method entry 13 // some full-text exceptions are thrown in throws Exception mode (this is simple) 14 // some use try... catch (this is better) 15 public static void main (String [] args) throws into TiO N {16 loadData (); // load data 17 while (true) {// infinite loop 18 getLine (3); // empty 3 rows for viewing 19 System. out. println ("************************************* *********"); 20 System. out. println ("* Human Resource Management System *"); 21 System. out. println ("************************************* *********"); 22 System. out. println ("* 1, view employee information *"); 23 System. out. println ("* 2, add employee information *"); 24 System. out. println ("* 3, modify employee information *"); 25 System. out. println ("* 4, delete employee information *"); 26 System. out. println ("* 0, exit System *"); 27 System. out. println ("************************************* *********"); 28 System. out. print ("select:"); 29 int num = SC. nextInt (); // input num Select menu 30 if (num = 0) {31 saveData (); 32 System. out. println ("Thank you! "); 33 break; 34} else {35 switch (num) {36 case 1: 37 query (); // query 38 break; 39 case 2: 40 add (); // Add 41 break; 42 case 3: 43 update (); // modify 44 break; 45 case 4: 46 del (); // Delete 47} 48} 49} 50} 51 52 // load data 53 private static void loadData () throws Exception {54 // define the byte stream, from F: /HRMS.txt load data 55 FileInputStream in = new FileInputStream ("F:/HRMS.txt"); 56 // convert it to bytes stream 57 InputStreamReader isr = new InputStreamReader (in); 58 // convert to buffer upstream stream 59 BufferedReader br = new BufferedReader (isr); 60 // read 61 String s = br row by row. readLine (); 62 // split data 63 while (s! = Null) {64 // s. split ("\ t") API explanation: splits the string based on the matching of the given regular expression. 65 // Regular Expression ??? 66 String [] ss = s. split ("\ t"); 67 // data type conversion 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 // encapsulated into Peron 73 Person p = new Person (id, name, sex, age); 74 // put the map 75 map. put (p. getId (), p); 76 // read the next row 77 s = br. readLine (); 78} 79 // This closed input stream I asked the teacher, why is it disabled in? Isr and br can also be disabled. 80 // explanation: in is the underlying layer, and closing in is enough for 81 in. close (); 82} 83 84 // save data 85 public static void saveData () throws Exception {86 // when exiting, all data in the entire map is saved. 87 FileOutputStream out = new FileOutputStream ("F:/HRMS.txt "); 88 OutputStreamWriter osw = new OutputStreamWriter (out); 89 // convert it to a print stream [System. out. println () console] 90 PrintWriter pw = new PrintWriter (osw, true); 91 // traverse map 92 // map. keySet (): returns the Set view of the keys contained in the ing. (API) 93 Set <Integer> keys = map. keySet (); // All keys 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 // Write File 98 pw. println (s); 99} 100 out. close (); 101} 102 103 // query 104 public static void query () {105 getLine (3); 106 System. out. println ("=> query EMPLOYEE:" + "\ n"); 107 System. out. println ("No. \ t" + "Name \ t" + "Gender \ t" + "Age"); 10 8 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 // add 115 public static void add () {116 // No., name, gender, age 117 System. out. println ("=> Add EMPLOYEE:" + "\ n"); 119 try {120 int id = 1; 121 System. out. print ("name:"); 122 String name = SC. next (); 123 124 Sys Tem. out. print ("sex:"); 125 String sex = SC. next (); 126 127 System. out. print ("age:"); 128 int age = SC. nextInt (); 129 130 Person person Person = new Person (id, name, sex, age); 131 map. put (person. getId (), person); 132 System. out. println ("added successfully! "); 133} catch (Exception e) {134 System. out. println (e. getMessage (); 135 System. out. println (" failed to add! "); 136} 137 138} 139 140 // modify 141 public static void update () {142 try {143 System. out. println ("=> modify EMPLOYEE:" + "\ n"); 144 System. out. print ("Enter the employee Id you want to modify:"); 145 int Id = SC. nextInt (); 146 if (map. containsKey (Id) {147 System. out. println ("\ n no. \ t" + "Name \ t" + "Gender \ t" + "Age"); 148 Person p = map. get (Id); 149 System. out. println (p. getId () + "\ t" + p. getName () + "\ t" + p. getSex () + "\ t" + p. getAge (); 150 System. ou T. println ("\ n please enter the 1 you want to modify, name 2, Gender 3, age:"); 151 int num2 = SC. nextInt (); 152 switch (num2) {153 case System. out. print ("name:"); 155 String name = SC. next (); 156 map. get (Id ). setName (name); 157 break; 158 case System. out. print ("sex:"); 160 String sex = SC. next (); 161 map. get (Id ). setSex (sex); 162 break; 163 case 3: 164 System. out. print ("age:"); 165 int age = SC. nextInt (); 166 map. get (Id ). setAge (age); 167 br Ak; 168 default: 169 System. out. println ("input error! "); 170 break; 171} 172 System. out. println (" modified successfully! "); 173} 174} catch (Exception e) {175 System. out. println (e. getMessage (); 176 System. out. println (" modification failed! "); 177} 178} 179 180 // Delete 181 public static void del () {182 System. out. println ("=> Delete EMPLOYEE:" + "\ n"); 183 System. out. print ("Enter the employee Id you want to delete:"); 184 int Id = SC. nextInt (); 185 if (map. containsKey (Id) {186 System. out. println ("\ n no. \ t" + "Name \ t" + "Gender \ t" + "Age"); 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 confirm to delete this Employee information? Y/n: "); 190 String a = SC. next (); 191 if (. equals ("y") {192 map. remove (Id); 193 System. out. println ("\ n deleted successfully! "); 194} 195} 196 197 198 // get empty rows 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 source code:
1 package azhi0901; 2 3 public class Person {4 // class attributes, total number of employees 5 static int total = 1; 6 // all attributes 7 private int id; 8 private String name; 9 private String sex; 10 private int age; 11 12 // constructor 13 Person (int id, String name, String sex, int age) {14 this. id = id; 15 if (name = null) 16 throw new RuntimeException ("the input name is invalid! "); 17 this. name = name; 18 19 if (sex. equals ("male") | sex. equals ("female") 20 this. sex = sex; 21 else22 throw new RuntimeException ("the entered gender is invalid! "); 23 24 if (age <0 | age> 200) 25 throw new RuntimeException (" the input age is invalid! "); 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 ("the input name is invalid! "); 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 ("male") | sex. equals ("female") 56 this. sex = sex; 57 else58 throw new RuntimeException ("the entered gender is invalid! "); 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 ("the input age is invalid! "); 69 this. age = age; 70} 71 72}
This Code also has some bugs and will be modified later.
7. effect Preview
Add
Data Validity Verification
Query
Modify
Delete
File
This human resource management system is more comprehensive than ever before and requires a lot of things. It can be regarded as a collection of previous knowledge points. The key points to be used are String, Integer, data type conversion, map storage key-value pairs in hierarchies, class declaration and instantiation, constructor, and general method, method rewriting, this keyword, static keyword, Data Validity verification, exception handling, input and output, etc. For the Select if, switch, and loop for, while has always been the most basic focus. I am not familiar with the above things. Some codes can be written only by referring to the case column of the teacher. I like programming more and more! The process of writing code is very enjoyable, especially when the results come out. Although it is a good dish, you can't stop an ordinary and incapable person from pursuing a beautiful thing and trying to change and improve it. In addition, I started to record what I learned when I wrote my own blog. I talked to a student who has been studying java that day about my blog. He said, "You are good !", It seems that blogs are all great gods, and I am a newbie like me. I will stick to it!
A_zhi
June