package 员工管理系统;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//定义一个员工类,里面有名字、年龄、职位三个属性
class Employee {
private String name;
private int age;
private String position;
//通过全参数构造器来实现员工信息初始化
public Employee(String name, int age, String position) {
this.name = name;
this.age = age;
this.position = position;
}
//通过get set方法实现后续操作
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getPosition() {
return position;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "员工管理系统.Employee{" +
"name='" + name + '\'' +
", age=" + age +
", position='" + position + '\'' +
'}';
}
}
public class ZZR{
定义一个集合,通过泛型来保存员工信息
private List<Employee> employees;
public ZZR() {
employees = new ArrayList<>();
}
//添加员工,通过传入的员工信息,调用集合的add方法实现员工添加
public void addEmployee(Employee employee) {
employees.add(employee);
System.out.println("员工已成功添加!");
}
//删除员工,通过传入的当前员工索引,判断员工索引是否在区间内,调用集合的remove方法来删除当前员工
public void removeEmployee(int index) {
if (index >= 0 && index < employees.size()) {
employees.remove(index);
System.out.println("员工已成功删除!");
} else {
System.out.println("无效的索引!");
}
}
//修改员工信息通过索引和新的员工名字,通过索引判断是否在区间内,如果在区间内通过当前索引获得要修改的员工具体信息,
// 然后通过set方法修改信息
public void modifyEmployeeName(int index, String newName) {
if (index >= 0 && index < employees.size()) {
Employee employee = employees.get(index);
employee.setName(newName);
System.out.println("员工姓名已成功修改!");
} else {
System.out.println("无效的索引!");
}
}
//查看所有员工信息方法,首先判断员工集合是否为空,如果为空的话提示员工列表为空
//如果不为空的话,通过for each循环来实现员工信息的打印
public void displayAllEmployees() {
if (employees.isEmpty()) {
System.out.println("员工列表为空!");
} else {
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
public static void main(String[] args) {
ZZR system = new ZZR();
Scanner scanner = new Scanner(System.in);
int choice = 0;
// While实现主菜单循环
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. 退出");
System.out.println("=====================");
System.out.print("请输入数字操作选项:");
choice = scanner.nextInt();
// 通过choice来实现接收主菜单循环时选择的哪一个具体数字
//通过该参数实现,判断具体要执行哪一个具体的方法
switch (choice) {
case 1:
System.out.print("请输入员工姓名:");
String name = scanner.next();
System.out.print("请输入员工年龄:");
int age = scanner.nextInt();
System.out.print("请输入员工职位:");
String position = scanner.next();
system.addEmployee(new Employee(name, age, position));
break;
case 2:
System.out.print("请输入要删除的员工索引:");
int removeIndex = scanner.nextInt();
system.removeEmployee(removeIndex);
break;
case 3:
System.out.print("请输入要修改姓名的员工索引:");
int modifyIndex = scanner.nextInt();
System.out.print("请输入新的姓名:");
String newName = scanner.next();
system.modifyEmployeeName(modifyIndex, newName);
break;
case 4:
system.displayAllEmployees();
break;
case 5:
System.out.println("退出程序!");
scanner.close();
System.exit(0);
default:
System.out.println("无效的选项!");
break;
}
}
}
}
01-29
748
