用java写.List: 员工管理系统 员工属性:员工编号、员工姓名、性别、年龄、所在部门 1————员工添加 2————员工删除 3————员工信息修改 4————根据员工编号查询员工信息 5————查询所有员工信息
时间: 2023-11-18 12:05:38 浏览: 185
好的,以下是一个基本的员工管理系统的Java实现,包含了员工添加、删除、修改和查询等功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeManagementSystem {
private static ArrayList<Employee> employees = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
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("0. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addEmployee();
break;
case 2:
deleteEmployee();
break;
case 3:
updateEmployee();
break;
case 4:
queryEmployeeById();
break;
case 5:
queryAllEmployees();
break;
case 0:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("输入有误,请重新选择!");
break;
}
}
}
private static void addEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入员工编号:");
String id = scanner.nextLine();
System.out.println("请输入员工姓名:");
String name = scanner.nextLine();
System.out.println("请输入员工性别:");
String gender = scanner.nextLine();
System.out.println("请输入员工年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入员工所在部门:");
String department = scanner.nextLine();
employees.add(new Employee(id, name, gender, age, department));
System.out.println("添加成功!");
}
private static void deleteEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的员工编号:");
String id = scanner.nextLine();
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
employees.remove(employee);
System.out.println("删除成功!");
return;
}
}
System.out.println("员工编号不存在,删除失败!");
}
private static void updateEmployee() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的员工编号:");
String id = scanner.nextLine();
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
System.out.println("请输入员工姓名(留空表示不修改):");
String name = scanner.nextLine();
if (!name.isEmpty()) {
employee.setName(name);
}
System.out.println("请输入员工性别(留空表示不修改):");
String gender = scanner.nextLine();
if (!gender.isEmpty()) {
employee.setGender(gender);
}
System.out.println("请输入员工年龄(留空表示不修改):");
String ageStr = scanner.nextLine();
if (!ageStr.isEmpty()) {
int age = Integer.parseInt(ageStr);
employee.setAge(age);
}
System.out.println("请输入员工所在部门(留空表示不修改):");
String department = scanner.nextLine();
if (!department.isEmpty()) {
employee.setDepartment(department);
}
System.out.println("修改成功!");
return;
}
}
System.out.println("员工编号不存在,修改失败!");
}
private static void queryEmployeeById() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要查询的员工编号:");
String id = scanner.nextLine();
for (Employee employee : employees) {
if (employee.getId().equals(id)) {
System.out.println(employee);
return;
}
}
System.out.println("员工编号不存在,查询失败!");
}
private static void queryAllEmployees() {
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
class Employee {
private String id;
private String name;
private String gender;
private int age;
private String department;
public Employee(String id, String name, String gender, int age, String department) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
this.department = department;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", department='" + department + '\'' +
'}';
}
}
```
在这个实现中,我们使用了一个 `ArrayList` 来存储所有的员工信息,并通过一个 `switch` 语句实现了不同功能的选择。在添加、删除、修改和查询员工信息时,我们通过 `Scanner` 类来获取用户输入的信息,并对员工信息进行相应的操作。同时,我们还定义了一个 `Employee` 类来表示每个员工的基本信息,方便后续的操作。
阅读全文