用 Array List 实现员工管理系统
package basic.practice;
import java.util.ArrayList;
import java.util.Scanner;
public class StuMngSys_ArrayListVersion {
public static void main(String[] args) {
// 创建集合对象,用于存储员工数据
ArrayList<Employee> array = new ArrayList<Employee>();
// 程序入口
while(true) {
// 系统主界面
System.out.println("------欢迎使用欺负员工就删库跑路公司员工管理系统------");
System.out.println("1 查看所有员工");
System.out.println("2 添加员工");
System.out.println("3 删除员工");
System.out.println("4 修改员工");
System.out.println("5 退出系统");
Scanner sc = new Scanner(System.in);
System.out.println("输入你的选择:");
String choiceString = sc.nextLine();
// 用switch语句判断选择
switch (choiceString) {
case "1":
// 查看所有员工
checkAllEmployee(array);
break;
case "2":
// 添加员工
addEmployee(array);
break;
case "3":
// 删除员工
deleteEmployee(array);
break;
case "4":
// 修改员工
editEmployee(array);
break;
case "886074565166": // 程序员隐藏选项 - 删库跑路 ,翻译:拜拜了,你气死我了,我要溜了
clearAllMessage(array);
break;
case "5":
// 退出
// System.out.println("感谢您使用本系统");
// break;
default:
System.out.println("感谢您使用本系统");
System.exit(0); // JVM退出
// break;
}
}
}
private static void checkAllEmployee(ArrayList<Employee> array) {
if(array.size() == 0) {
System.out.println("无员工信息");
return;
}
System.out.println("柜员号\t姓名\t部门");
for(Employee emp :array) {
System.out.println(emp);
}
}
private static void addEmployee(ArrayList<Employee> array) {
Scanner sc = new Scanner(System.in);
String id;
while(true) {
System.out.println("请输入柜员号:");
id = sc.nextLine();
// 判断该柜员号是否已经存在
boolean flag = false;
for(int x = 0; x < array.size(); x++) {
Employee emp = array.get(x);
if(emp.getId().equals(id)) {
flag = true;
break;
}
}
if(flag) {
System.out.println("该柜员号已经存在,请勿重复添加");
} else {
break;
}
}
System.out.println("请输入员工姓名:");
String name = sc.nextLine();
System.out.println("请输入员工部门");
String department = sc.nextLine();
Employee emp = new Employee(id, name, department);
array.add(emp);
System.out.println("员工" + name + "添加成功");
}
private static void deleteEmployee(ArrayList<Employee> array) {
Scanner sc = new Scanner(System.in);
System.out.println("输入需要删除的员工号:");
String id = sc.nextLine();
int index = -1;
for (int i = 0; i < array.size(); i++) {
Employee emp = array.get(i);
if (emp.getId().equals(id)) {
index = i;
break;
}
}
if (index == -1) {
System.out.println("该员工不存在,无法删除");
} else {
String name = array.get(index).getName();
array.remove(index);
System.out.println("删除员工" + name + "成功");
}
}
private static void editEmployee(ArrayList<Employee> array) {
Scanner sc = new Scanner(System.in);
System.out.println("输入要修改信息的员工的柜员号:");
String id = sc.nextLine();
int index = -1;
for (int i = 0; i < array.size(); i++) {
Employee emp = array.get(i);
if (emp.getId().equals(id)) {
index = i;
break;
}
}
if(index == -1) {
System.out.println("欲修改的员工信息不存在");
} else {
String name = array.get(index).getName();
System.out.println("输入该员工的新部门名称:");
String new_department = sc.nextLine();
Employee emp = new Employee(id, name, new_department);
array.set(index, emp);
System.out.println("员工" + name + "部门修改成功");
}
}
private static void clearAllMessage(ArrayList<Employee> array) {
Scanner sc = new Scanner(System.in);
System.out.println("您确定要删库跑路?Y/N");
String choice = sc.nextLine();
if ("Y".equals(choice)) {
array.clear();
System.out.println("员工信息库清空完毕,您可以跑路了");
} else {
System.out.println("您对公司还是有感情的,继续工作吧");
}
}
}