-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemployee2.java
More file actions
55 lines (46 loc) · 1.48 KB
/
Copy pathemployee2.java
File metadata and controls
55 lines (46 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.ArrayList;
import java.util.Iterator;
public class employee2 {
private String name;
private int id;
private double salary;
public employee2(String name, int id, double salary) {
this.name = name;
this.id = id;
this.salary = salary;
}
public int getId() {
return id;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [ID=" + id + ", Name=" + name + ", Salary=" + salary + "]";
}
public static void main(String[] args) {
ArrayList<employee2> employees = new ArrayList<>();
employees.add(new employee2("Nitanshu1", 1, 50000));
employees.add(new employee2("Nitanshu2", 2, 60000));
employees.add(new employee2("Nitanshu3", 3, 70000));
// Update salary of Nitanshu2 (ID = 2)
for (employee2 emp : employees) {
if (emp.getId() == 2) {
emp.setSalary(65000);
break;
}
}
// Remove employee with ID = 1 (Nitanshu1)
Iterator<employee2> iterator = employees.iterator();
while (iterator.hasNext()) {
if (iterator.next().getId() == 1) {
iterator.remove();
}
}
// Print remaining employees
for (employee2 emp : employees) {
System.out.println(emp);
}
}
}