4. Java 员工管理系统 员工属性:员工编号、员工姓名、性别、年龄、所在部门 1————员工添加 2- 3- 4- 5 -一员工删除 —一员工信息修改 -—根据员工编号查询员工信息 - 查询所有员工信息
时间: 2023-10-25 22:08:50 浏览: 169
以下是一个简单的 Java 员工管理系统,实现了员工添加、删除、修改和查询功能。
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeManagementSystem {
private static List<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. 查询所有员工信息");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addEmployee(scanner);
break;
case 2:
deleteEmployee(scanner);
break;
case 3:
updateEmployee(scanner);
break;
case 4:
queryEmployeeById(scanner);
break;
case 5:
queryAllEmployees();
break;
default:
System.out.println("无效的选择!");
break;
}
}
}
private static void addEmployee(Scanner scanner) {
System.out.println("请输入员工编号:");
int id = scanner.nextInt();
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();
Employee employee = new Employee(id, name, gender, age, department);
employees.add(employee);
System.out.println("员工添加成功!");
}
private static void deleteEmployee(Scanner scanner) {
System.out.println("请输入员工编号:");
int id = scanner.nextInt();
boolean found = false;
for (Employee employee : employees) {
if (employee.getId() == id) {
employees.remove(employee);
found = true;
break;
}
}
if (found) {
System.out.println("员工删除成功!");
} else {
System.out.println("未找到该员工!");
}
}
private static void updateEmployee(Scanner scanner) {
System.out.println("请输入员工编号:");
int id = scanner.nextInt();
boolean found = false;
for (Employee employee : employees) {
if (employee.getId() == id) {
scanner.nextLine(); // 读取换行符
System.out.println("请输入员工姓名(原值:" + employee.getName() + "):");
String name = scanner.nextLine();
System.out.println("请输入员工性别(原值:" + employee.getGender() + "):");
String gender = scanner.nextLine();
System.out.println("请输入员工年龄(原值:" + employee.getAge() + "):");
int age = scanner.nextInt();
scanner.nextLine(); // 读取换行符
System.out.println("请输入员工所在部门(原值:" + employee.getDepartment() + "):");
String department = scanner.nextLine();
employee.setName(name);
employee.setGender(gender);
employee.setAge(age);
employee.setDepartment(department);
found = true;
break;
}
}
if (found) {
System.out.println("员工信息修改成功!");
} else {
System.out.println("未找到该员工!");
}
}
private static void queryEmployeeById(Scanner scanner) {
System.out.println("请输入员工编号:");
int id = scanner.nextInt();
boolean found = false;
for (Employee employee : employees) {
if (employee.getId() == id) {
System.out.println(employee);
found = true;
break;
}
}
if (!found) {
System.out.println("未找到该员工!");
}
}
private static void queryAllEmployees() {
for (Employee employee : employees) {
System.out.println(employee);
}
}
}
class Employee {
private int id;
private String name;
private String gender;
private int age;
private String department;
public Employee(int 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 int getId() {
return id;
}
public void setId(int 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 + '\'' +
'}';
}
}
```
这个程序通过一个 `List<Employee>` 来保存所有员工的信息,每个员工由员工编号、员工姓名、性别、年龄和所在部门五个属性组成。在主函数中,程序通过读取用户输入的选项来执行相应的操作:
- 员工添加:读取用户输入的员工信息,创建一个新的 `Employee` 对象并添加到 `employees` 列表中。
- 员工删除:读取用户输入的员工编号,遍历 `employees` 列表寻找该员工,如果找到则从列表中删除。
- 员工信息修改:读取用户输入的员工编号,遍历 `employees` 列表寻找该员工,如果找到则读取用户输入的新员工信息并更新该员工的属性。
- 根据员工编号查询员工信息:读取用户输入的员工编号,遍历 `employees` 列表寻找该员工,如果找到则打印该员工的信息。
- 查询所有员工信息:遍历 `employees` 列表打印所有员工的信息。
这个程序比较简单,没有做输入校验等复杂的处理,但足以演示 Java 中如何实现一个员工管理系统。
阅读全文