Java面向过程实现员工管理系统(利用集合存储数据实现员工增删改查排序)
作者:互联网
界面:
1 package staffSystem; 2 3 import java.util.Scanner; 4 5 /** 6 * 交互界面 7 * @author 123 8 * 9 */ 10 public class Surface { 11 12 public static void main(String[] args) { 13 14 // Staff sta = new Staff(); 15 // Model mode = new Model(); 16 Scanner scan = new Scanner(System.in); 17 // int index=0; 18 19 menu(scan); 20 scan.close(); 21 22 } 23 public static void menu(Scanner scan) { 24 while (true) { 25 26 System.out.println("******************员工管理系统******************"); 27 System.out.println("1--添加员工 2--修改员工 3--查询员工"); 28 System.out.println("4--删除员工 5--员工排名 6--退出系统 "); 29 System.out.println("************************************************"); 30 System.out.println("请输入序号进行操作(1-6):"); 31 int n = scan.nextInt(); 32 if (n == 1) { 33 Model.addEmp();//添加 34 } else if (n == 2) { 35 Model.setEmp();//修改 36 } else if (n == 3) { 37 Model.search();//查询 38 } else if (n == 4) { 39 Model.remove();//删除 40 } else if (n == 5) { 41 Model.sort();//排序 42 } else if (n == 6) { 43 break; 44 } else { 45 System.out.println("输入不合法!请重新输入!"); 46 } 47 48 } 49 System.out.println("已退出系统!!!"); 50 51 } 52 public static void search1() { 53 System.out.println("此员工不存在!"); 54 System.out.println(); 55 System.out.println("请选择选项:"); 56 System.out.println("1--重新查询 2--返回主界面"); 57 } 58 }
员工类:
1 package staffSystem; 2 3 /** 4 * 员工类 5 * 6 * @author 123 7 * 8 */ 9 public class Staff { 10 11 private String name;// 员工姓名 12 private int id;// 员工号 13 private String sex;// 员工性别 14 private int age;// 员工年龄 15 private float salary;// 工资 16 17 public Staff() { 18 19 } 20 21 public Staff(Integer id, String name, String sex, Integer age, Integer salary) { 22 this.id = id; 23 this.name = name; 24 this.age = age; 25 this.sex = sex; 26 this.salary = salary; 27 } 28 29 public void setSex(String sex) { 30 this.sex = sex; 31 } 32 33 public String getName() { 34 return name; 35 } 36 37 public void setName(String name) { 38 this.name = name; 39 } 40 41 public int getId() { 42 return id; 43 } 44 45 public void setId(int id) { 46 this.id = id; 47 } 48 49 public String getSex() { 50 return sex; 51 } 52 53 public int getAge() { 54 return age; 55 } 56 57 public void setAge(int age) { 58 this.age = age; 59 } 60 61 public float getSalary() { 62 return salary; 63 } 64 65 public void setSalary(float salary) { 66 this.salary = salary; 67 } 68 69 @Override 70 public String toString() {//方便查看员工信息 71 return "员工号:" + id +"\t姓名:" + name + "\t性别:" + sex + "\t年龄:" + age + "\t工资:" + salary; 72 } 73 74 75 }
功能实现:
1 package staffSystem; 2 3 import java.util.ArrayList; 4 import java.util.Scanner; 5 6 /** 7 * 对象模型类 8 * 9 * @author 123 10 * 11 */ 12 public class Model { 13 14 static ArrayList<Staff> sta = new ArrayList<>(); 15 static {// 初始化 16 Staff sta1 = new Staff(1, "张三", "女", 18, 5000); 17 Staff sta2 = new Staff(2, "李四", "男", 23, 8000); 18 Staff sta3 = new Staff(3, "王五", "男", 26, 6500); 19 // Staff sta4 = new Staff(67, "李5四", "男", 23, 56); 20 // Staff sta5 = new Staff(7, "王7五", "男", 26, 12000); 21 sta.add(sta1); 22 sta.add(sta2); 23 sta.add(sta3); 24 // sta.add(sta4); 25 // sta.add(sta5); 26 } 27 28 /** 29 * 查询员工信息 30 */ 31 public static void search() { 32 boolean t = true; 33 while (t) { 34 System.out.println("**********员工信息表**********"); 35 for (int i = 0; i < sta.size(); i++) { 36 System.out.println("工号:" + sta.get(i).getId() + " \t" + "姓名:" + sta.get(i).getName()); 37 } 38 System.out.println("请选择查询员工方式:"); 39 System.out.println("1--按员工姓名查找 2--按员工工号查找 3--返回主界面"); 40 Scanner scan1 = new Scanner(System.in); 41 int n = scan1.nextInt(); 42 if (n == 1) { 43 int a = 0; 44 System.out.println("请输入要查询的员工名字:"); 45 String name = scan1.next(); 46 for (int i = 0; i < sta.size(); i++) { 47 if (name.equals(sta.get(i).getName())) { 48 System.out.println(sta.get(i)); 49 System.out.println("查询成功!"); 50 System.out.println(); 51 System.out.println("1--继续查询 2--返回主界面"); 52 int y1 = scan1.nextInt(); 53 if (y1 == 1) { 54 a = 1; 55 } else if (y1 == 2) { 56 a = 1; 57 t = false; 58 } else { 59 System.out.println("输入不合法!已退出查询!"); 60 System.out.println("******************************"); 61 a = 1; 62 t = false; 63 } 64 } 65 } 66 if (a == 0) { 67 Surface.search1(); 68 int n1 = scan1.nextInt(); 69 while (true) { 70 if (n1 == 1) { 71 break; 72 } else if (n1 == 2) { 73 t = false; 74 break; 75 } else { 76 System.out.println("输入错误!请重新输入!"); 77 n1 = scan1.nextInt(); 78 } 79 } 80 } 81 } else if (n == 2) { 82 int a1 = 0; 83 System.out.println("请输入要查询的员工工号:"); 84 int id = scan1.nextInt(); 85 for (int i = 0; i < sta.size(); i++) { 86 if (id == sta.get(i).getId()) { 87 System.out.println(sta.get(i)); 88 System.out.println("查询成功!"); 89 System.out.println(); 90 System.out.println("1--继续查询 2--返回主界面"); 91 int y = scan1.nextInt(); 92 if (y == 1) { 93 a1 = 1; 94 } else if (y == 2) { 95 a1 = 1; 96 t = false; 97 } else { 98 System.out.println("输入不合法!已退出查询!"); 99 System.out.println("******************************"); 100 a1 = 1; 101 t = false; 102 } 103 } 104 } 105 if (a1 == 0) { 106 Surface.search1(); 107 int n1 = scan1.nextInt(); 108 while (true) { 109 if (n1 == 1) { 110 break; 111 } else if (n1 == 2) { 112 t = false; 113 break; 114 } else { 115 System.out.println("输入错误!请重新输入!"); 116 n1 = scan1.nextInt(); 117 } 118 } 119 } 120 } else if (n == 3) { 121 t = false; 122 } else { 123 System.out.println("输入不合法!请重新输入!"); 124 System.out.println("******************************"); 125 } 126 } 127 } 128 129 /** 130 * 添加员工信息 131 */ 132 public static void addEmp() { 133 boolean t = true; 134 while (t) { 135 Scanner scan3 = new Scanner(System.in); 136 137 System.out.print("请输入员工工号:"); 138 int addid = scan3.nextInt(); 139 System.out.print("请输入员工姓名:"); 140 String addname = scan3.next(); 141 System.out.print("请输入员工性别:"); 142 String addsex = scan3.next(); 143 System.out.print("请输入员工年龄:"); 144 int addage = scan3.nextInt(); 145 System.out.print("请输入员工工资:"); 146 float addsalary = scan3.nextFloat(); 147 148 boolean b = true; 149 for (int i = 0; i < sta.size(); i++) { 150 if (sta.get(i).getId() == addid) { 151 b = false; 152 break; 153 } 154 } 155 if (b) { 156 Staff staff = new Staff(); 157 staff.setId(addid); 158 staff.setName(addname); 159 staff.setSex(addsex); 160 staff.setAge(addage); 161 staff.setSalary(addsalary); 162 sta.add(staff); 163 System.out.println("员工添加成功!"); 164 System.out.println("1--继续添加 2--返回主界面"); 165 System.out.println("******************************"); 166 int y = scan3.nextInt(); 167 if (y == 1) { 168 169 } else if (y == 2) { 170 t = false; 171 } else { 172 System.out.println("输入不合法!已退出添加!"); 173 System.out.println("******************************"); 174 t = false; 175 } 176 } else { 177 System.out.println("添加失败!请不要重复添加!"); 178 System.out.println("1--继续添加 2--返回主界面"); 179 System.out.println("******************************"); 180 int y1 = scan3.nextInt(); 181 if (y1 == 1) { 182 183 } else if (y1 == 2) { 184 t = false; 185 } else { 186 System.out.println("输入不合法!已退出添加!"); 187 System.out.println("******************************"); 188 t = false; 189 } 190 } 191 } 192 } 193 194 /** 195 * 删除员工信息 196 */ 197 public static void remove() { 198 boolean t = true; 199 while (t) { 200 System.out.println("**********员工信息表**********"); 201 for (int i = 0; i < sta.size(); i++) { 202 System.out.println("工号:" + sta.get(i).getId() + "\t" + "姓名:" + sta.get(i).getName()); 203 } 204 System.out.println("请选择要删除的员工工号:"); 205 Scanner scan4 = new Scanner(System.in); 206 int id = scan4.nextInt(); 207 for (int i = 0; i < sta.size(); i++) { 208 if (sta.get(i).getId() == id) { 209 System.out.println("是否删除此人?"); 210 System.out.println("1--是 2--否 3--返回主界面"); 211 int a = scan4.nextInt(); 212 if (a == 1) { 213 sta.remove(i); 214 System.out.println("删除成功!!"); 215 System.out.println("1--继续删除 2--返回主界面"); 216 System.out.println("******************************"); 217 int y = scan4.nextInt(); 218 while (true) { 219 if (y == 1) { 220 break; 221 } else if (y == 2) { 222 t = false; 223 break; 224 } else { 225 System.out.println("输入错误,请重新输入!"); 226 } 227 } 228 break; 229 } else if (a == 2) { 230 System.out.println("已取消删除!"); 231 System.out.println("********************"); 232 break; 233 } else if (a == 3) { 234 t = false; 235 break; 236 } else { 237 System.out.println("输入错误!请重新输入!"); 238 System.out.println("********************"); 239 i--; 240 } 241 } else if (i == sta.size() - 1) { 242 System.out.println("输入工号错误,没有此员工!"); 243 System.out.println("1--重新输入 2--返回主界面"); 244 System.out.println("******************************"); 245 int a1 = scan4.nextInt(); 246 if (a1 == 1) { 247 248 } else if (a1 == 2) { 249 t = false; 250 } else { 251 System.out.println("输入错误!已退出删除程序"); 252 t = false; 253 } 254 } 255 } 256 } 257 } 258 259 /** 260 * 修改员工信息 261 */ 262 public static void setEmp() { 263 boolean t = true; 264 while (t) { 265 System.out.println("**********员工信息表**********"); 266 for (int i = 0; i < sta.size(); i++) { 267 System.out.println("工号:" + sta.get(i).getId() + "\t" + "姓名:" + sta.get(i).getName()); 268 } 269 System.out.println("请选择要修改的员工工号:"); 270 Scanner scan5 = new Scanner(System.in); 271 int id = scan5.nextInt(); 272 for (int i = 0; i < sta.size(); i++) { 273 if (sta.get(i).getId() == id) { 274 int w = 0; 275 System.out.println("是否修改此人?"); 276 System.out.println("1--是 2--否 3--返回主界面"); 277 int a = scan5.nextInt(); 278 if (a == 1) { 279 280 System.out.print("请输入员工工号:"); 281 int addid = scan5.nextInt(); 282 System.out.print("请输入员工姓名:"); 283 String addname = scan5.next(); 284 System.out.print("请输入员工性别:"); 285 String addsex = scan5.next(); 286 System.out.print("请输入员工年龄:"); 287 int addage = scan5.nextInt(); 288 System.out.print("请输入员工工资:"); 289 float addsalary = scan5.nextFloat(); 290 291 for (int j = 0; j < sta.size(); j++) { 292 if (j == i) { 293 continue; 294 } 295 if (addid == sta.get(j).getId()) { 296 w = 1; 297 } 298 } 299 if (w == 0) { 300 sta.remove(i); 301 Staff staff = new Staff(); 302 staff.setId(addid); 303 staff.setName(addname); 304 staff.setSex(addsex); 305 staff.setAge(addage); 306 staff.setSalary(addsalary); 307 sta.add(i, staff); 308 System.out.println("员工修改成功!"); 309 System.out.println("1--继续修改 2--返回主界面"); 310 System.out.println("******************************"); 311 int y = scan5.nextInt(); 312 if (y == 1) { 313 break; 314 } else if (y == 2) { 315 t = false; 316 break; 317 } else { 318 System.out.println("输入不合法!已退出修改!"); 319 System.out.println("******************************"); 320 t = false; 321 break; 322 } 323 } 324 325 } else if (a == 2) { 326 System.out.println("已取消修改!"); 327 System.out.println("********************"); 328 break; 329 } else if (a == 3) { 330 t = false; 331 break; 332 } else { 333 System.out.println("输入错误!请重新输入!"); 334 System.out.println("********************"); 335 break; 336 } 337 338 if (w == 1) { 339 System.out.println("工号重复,请重新输入!"); 340 System.out.println("*************************"); 341 break; 342 } 343 } else if (i == sta.size() - 1) { 344 System.out.println("输入工号错误,没有此员工!"); 345 System.out.println("1--重新输入 2--返回主界面"); 346 System.out.println("******************************"); 347 int a1 = scan5.nextInt(); 348 if (a1 == 1) { 349 350 } else if (a1 == 2) { 351 t = false; 352 } else { 353 System.out.println("输入错误!已退出修改程序"); 354 t = false; 355 } 356 } 357 } 358 } 359 } 360 361 /** 362 * 对员工进行排序 363 */ 364 public static void sort() { 365 boolean t = true; 366 while (t) { 367 System.out.println("请选择要排序的方式:"); 368 System.out.println("1--按工号进行排序 2--按工资进行排序 3--返回主界面"); 369 System.out.println("******************************"); 370 Scanner scan6 = new Scanner(System.in); 371 int n = scan6.nextInt(); 372 if (n == 1) { 373 System.out.println("工号从小到大排序:"); 374 System.out.println("**********员工信息表**********"); 375 Staff staff = new Staff(); 376 377 for (int i = 0; i < sta.size() - 1; i++) {// 按照工号从小到大排序 378 int min = sta.get(i).getId(); 379 for (int x = i + 1; x < sta.size(); x++) { 380 if (sta.get(x).getId() < min) { 381 int k = 0; 382 for (int j = 0; j < x; j++) { 383 if (sta.get(j).getId() == min) { 384 k = j; 385 break; 386 } 387 } 388 staff = sta.get(k); 389 sta.set(k, sta.get(x)); 390 sta.set(x, staff); 391 } 392 } 393 } 394 for (int i = 0; i < sta.size(); i++) { 395 System.out.println(sta.get(i)); 396 } 397 System.out.println(); 398 System.out.println("员工排序成功!"); 399 System.out.println("1--继续排序 2--返回主界面"); 400 System.out.println("******************************"); 401 int y1 = scan6.nextInt(); 402 while (true) { 403 if (y1 == 1) { 404 break; 405 } else if (y1 == 2) { 406 t = false; 407 break; 408 } else { 409 System.out.println("输入错误,请重新输入!"); 410 } 411 } 412 } else if (n == 2) { 413 System.out.println("工资从小到大排序:"); 414 System.out.println("**********员工信息表**********"); 415 Staff staff = new Staff(); 416 for (int i = 0; i < sta.size() - 1; i++) {// 按照工资从小到大排序 417 for (int x = 0; x < sta.size() - 1 - i; x++) { 418 if (sta.get(x).getSalary() > sta.get(x + 1).getSalary()) {// 冒泡排序 419 staff = sta.get(x); 420 sta.set(x, sta.get(x + 1)); 421 sta.set(x + 1, staff); 422 } 423 } 424 } 425 for (int i = 0; i < sta.size(); i++) { 426 System.out.println(sta.get(i)); 427 } 428 System.out.println(); 429 System.out.println("员工排序成功!"); 430 System.out.println("1--继续排序 2--返回主界面"); 431 System.out.println("******************************"); 432 int y = scan6.nextInt(); 433 while (true) { 434 if (y == 1) { 435 break; 436 } else if (y == 2) { 437 t = false; 438 break; 439 } else { 440 System.out.println("输入错误,请重新输入!"); 441 } 442 } 443 } else if (n == 3) { 444 t = false; 445 } else { 446 System.out.println("输入错误!请重新输入!"); 447 System.out.println(); 448 } 449 450 } 451 } 452 453 }View Code
输出结果:
标签:Java,sta,int,改查,System,员工,println,else,out 来源: https://www.cnblogs.com/ybw9296-blog/p/16488255.html