java实现员工信息的增删改查
时间: 2023-07-07 18:06:12 浏览: 207
好的,这里提供一份基于Java的员工信息增删改查的示例代码。
首先,我们需要定义一个员工类,包含员工的基本信息:
```java
public class Employee {
private int id;
private String name;
private int age;
private String gender;
private String department;
private String title;
// 构造方法
public Employee(int id, String name, int age, String gender, String department, String title) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.department = department;
this.title = title;
}
// getter和setter方法
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
```
接下来,我们需要定义一个员工信息管理类,包含增删改查的方法:
```java
import java.util.ArrayList;
import java.util.List;
public class EmployeeManager {
// 员工列表
private List<Employee> employees = new ArrayList<>();
// 增加员工
public void addEmployee(Employee employee) {
employees.add(employee);
}
// 删除员工
public void deleteEmployee(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
employees.remove(employee);
break;
}
}
}
// 修改员工信息
public void updateEmployee(Employee employee) {
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).getId() == employee.getId()) {
employees.set(i, employee);
break;
}
}
}
// 查询员工信息
public Employee getEmployee(int id) {
for (Employee employee : employees) {
if (employee.getId() == id) {
return employee;
}
}
return null;
}
}
```
以上代码实现了基本的增删改查员工信息的功能,你可以根据具体需求进行修改和优化。同时,需要注意线程安全和异常处理等问题。
阅读全文