CRUD collection may be implemented, the following set of items do with ArrayList instance, several common functions set ArrayList
.size () set size
.add () to add objects to the collection
.remove () object deletion collection
Other functions used
.equals () string comparison
System.exit (-1) to exit the system, non-zero
1 / * 2 * Author: white passenger C 3 * Time: 28 February 2020 4 * Content: Simple employee management system 5 * / 6 7 Package Penalty for com.beekc.www; 8 Import java.io. * ; 9 Import * Classes in java.util. ; 10 . 11 public class Beekc { 12 is public static void main (String [] args) throws Exception { 13 is 14 // Create EmpManage 15 EmpManage EM = new new EmpManage (); 16 . 17 // input stream 18 is the InputStreamReader ISR = new new the InputStreamReader (the System.in); . 19 the BufferedReader br = new new the BufferedReader (ISR); 20 is 21 is // simplified menu 22 is em.menu (); 23 is the while ( to true ) 24 { 25 26 is the System. Out.print ( "Please select your operation to be performed:" ); 27 // receiving a selection item 28 String operType = br.readLine (); 29 30 IF (operType.equals ( ". 1" )) { 31 is 32 of System.out.print ( "Enter Number:" ); 33 is String the empNo = br.readLine (); 34 is of System.out.print ( "Please enter the name:" ); 35 String name = br.readLine (); 36 of System.out.print ( "Please enter salary:" ); 37 [ the Float SAL = Float.parseFloat as (br.readLine ()); 38 is 39 Emp = EMP new new Emp (the empNo, name, SAL); 40 em.addEmp ( EMP); 41 is 42 is } the else IF (operType.equals ( "2")) 43 is { 44 is of System.out.print ( "Enter Number:" ); 45 String the empNo = br.readLine (); 46 is em.showInfo (the empNo); 47 48 } the else IF (operType.equals ( ". 3" )) 49 { 50 of System.out.print ( "enter number:" ); 51 is String the empNo = br.readLine (); 52 is of System.out.print ( "Please enter a new payroll:" ); 53 is the Float SAL = the Float .parseFloat (br.readLine ()); 54 em.updateSal(empNo,sal); 55 56 }else if (operType.equals("4")) 57 { 58 System.out.print("请输入编号:"); 59 String empNo = br.readLine(); 60 em.delEmp(empNo); 61 62 }else if (operType.equals("5")) 63 { 64 em.showAll(); 65 } 66 } 67 } 68 } 69 70 // employee management class 71 is class EmpManage { 72 Private the ArrayList Al = null ; 73 is 74 // constructor 75 public EmpManage () 76 { 77 Al = new new the ArrayList (); 78 } 79 80 // menu 81 public void MENU () 82 { 83 System.out.println ( "add an employee. 1." ); 84 System.out.println (. "Find a 2 employee"); 85 System.out.println ( "3 modify an employee's wages." ); 86 System.out.println ( "delete a 4 employees." ); 87 System.out.println (. "5 query for all employees" ) ; 88 } 89 90 // added staff 91 is public void addEmp (EMP Emp) 92 { 93 // add objects to the collection 94 al.Add (EMP); 95 } 96 97 // display the employee information 98 public void the showInfo (String the empNo ) 99 { 100 //Traversing the whole the ArrayList 101 for ( int I = 0; I <al.size (); I ++ ) 102 { 103 // remove Emp objects 104 Emp = EMP (Emp) al.get (I); 105 106 // comparison number, Note that string comparison with the equals () 107 IF (emp.getEmpNo () the equals (the empNo).) 108 { 109 System.out.println ( "the employee information:" ); 110 System.out.println ( "Code: "+ the empNo); 111 System.out.println (" name: "+ emp.getName ()); 112 System.out.println ( "salary:" + emp.getSal ()); 113 } 114 } 115 } 1 16 117 // show all the employee information 1 18 public void is showAll, () 119 { 120 for ( int I = 0; I < al.size (); I ++ ) 121 { 122 of System.out.print ( "No \ T" ); 123 of System.out.print ( "name \ T" ); 124 System.out.println ( "salary" ); 125 } 126 127 for(int i = 0 ; i < al.size() ; i++) 128 { 129 Emp emp = (Emp)al.get(i); 130 System.out.print(emp.getEmpNo() + "\t\t"); 131 System.out.print(emp.getName() + "\t\t"); 132 System.out.println(emp.getSal()); 133 } 134 } 135 136 //修改员工薪水 137 public void updateSal(String empNo ,float newSal) 138 { 139 for(int i = 0 ; i < al.size() ; i++) 140 { 141 Emp emp = (Emp)al.get(i); 142 if(emp.getEmpNo().equals(empNo)) 143 { 144 //修改成功 145 emp.setSal(newSal); 146 } 147 } 148 } 149 150 //删除员工 151 public void delEmp(String empNo) 152 { 153 for(int i = 0 ; i < al.size() ; i++) 154 { 155 Emp = EMP (Emp) al.get (I); 156 IF (. Emp.getEmpNo () the equals (the empNo)) 157 { 158 // delete the collection object 159 al.remove (I); 160. // al.remove (EMP); 161 } 162 } 163 } 164 is // -------------------------- 165 } 166 167 is // employee class 168 class Emp { 169 // school number 170 Private String empNo; 171 // Name 172 Private String name; 173 // salary 174 Private a float SAL; 175 176 // Constructor 177 public Emp (the empNo String, String name, a float SAL) 178 { 179 the this .empNo = the empNo; 180 [ the this .name = name; 181 the this .sal = SAL; 182 } 183 is 184 public String getEmpNo () { 185 return the empNo; 186 } 187 188 public void setEmpNo(String empNo) { 189 this.empNo = empNo; 190 } 191 192 public String getName() { 193 return name; 194 } 195 196 public void setName(String name) { 197 this.name = name; 198 } 199 200 public float getSal() { 201 return sal; 202 } 203 204 public voidsetSal ( of float sal Is this) { 205 Deel hierdie sal = sal Is this; 206 } 207 }