Java每个员工包含姓名及工资两个字段,将以下员工信息存到列表中:张三,4000;李四,3500;王五,4800;赵六,3000;查询张三的员工,将姓名修改为“张小三”,查询李四的员工,将工资在原工资基础上加500;查询王五的员工,将改员工删除;输出所有员工信息
时间: 2024-03-03 17:51:26 浏览: 57
好的,我来解答你的问题。
以下是Java代码实现:
```java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public int getSalary() {
return salary;
}
public void setName(String name) {
this.name = name;
}
public void setSalary(int salary) {
this.salary = salary;
}
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("张三", 4000));
employeeList.add(new Employee("李四", 3500));
employeeList.add(new Employee("王五", 4800));
employeeList.add(new Employee("赵六", 3000));
// 查询张三的员工,将姓名修改为“张小三”
for (Employee employee : employeeList) {
if (employee.getName().equals("张三")) {
employee.setName("张小三");
}
}
// 查询李四的员工,将工资在原工资基础上加500
for (Employee employee : employeeList) {
if (employee.getName().equals("李四")) {
employee.setSalary(employee.getSalary() + 500);
}
}
// 查询王五的员工,将改员工删除
Iterator<Employee> iterator = employeeList.iterator();
while (iterator.hasNext()) {
Employee employee = iterator.next();
if (employee.getName().equals("王五")) {
iterator.remove();
}
}
// 输出所有员工信息
for (Employee employee : employeeList) {
System.out.println("姓名:" + employee.getName() + ",工资:" + employee.getSalary());
}
}
}
```
输出结果为:
```
姓名:张小三,工资:4000
姓名:李四,工资:4000
姓名:赵六,工资:3000
```
注意,为了保证代码的简洁性,我在代码中没有添加异常处理,实际开发中应该添加必要的异常处理。
阅读全文