Skip to content

Commit caa91cc

Browse files
committed
Add Employees, EmployeesDTO, and Q12EmployeesMapping classes for employee management and mapping
1 parent 663f49e commit caa91cc

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.topfifty;
2+
3+
import java.util.Objects;
4+
5+
public class Employees {
6+
7+
private int id;
8+
private String name;
9+
private String department;
10+
private double salary;
11+
private boolean active;
12+
private String email;
13+
14+
public Employees(int id, String name, String department, double salary, boolean active, String email) {
15+
this.id = id;
16+
this.name = name;
17+
this.department = department;
18+
this.salary = salary;
19+
this.active = active;
20+
this.email = email;
21+
}
22+
23+
public int getId() {
24+
return id;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public String getDepartment() {
32+
return department;
33+
}
34+
35+
public double getSalary() {
36+
return salary;
37+
}
38+
39+
public boolean isActive() {
40+
return active;
41+
}
42+
43+
public String getEmail() {
44+
return email;
45+
}
46+
47+
// Handy for the "Increment Salary by 10% (Immutable)" style problems,
48+
// where you build a new Employee instead of mutating the existing one.
49+
public Employees withSalary(double newSalary) {
50+
return new Employees(this.id, this.name, this.department, newSalary, this.active, this.email);
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) return true;
56+
if (!(o instanceof Employees)) return false;
57+
Employees employee = (Employees) o;
58+
return id == employee.id;
59+
}
60+
61+
@Override
62+
public int hashCode() {
63+
return Objects.hash(id);
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", department='" + department + '\'' + ", salary=" + salary + ", active=" + active + ", email='" + email + '\'' + '}';
69+
}
70+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.topfifty;
2+
3+
4+
import java.util.List;
5+
6+
public class EmployeesDTO {
7+
public static List<Employees> getEmployees() {
8+
return List.of(
9+
new Employees(1, "Alice", "Engineering", 60000, true, "alice@co.com"),
10+
new Employees(2, "Bob", "Engineering", 55000, true, "bob@co.com"),
11+
new Employees(3, "Charlie", "Finance", 70000, false, "charlie@co.com"),
12+
new Employees(4, "David", "Finance", 45000, true, "david@co.com"),
13+
new Employees(5, "Eve", "HR", 50000, true, "eve@co.com"),
14+
new Employees(6, "Frank", "HR", 50000, false, null),
15+
new Employees(7, "Grace", "Engineering", 60000, true, "grace@co.com"),
16+
new Employees(8, "Alice", "Marketing", 40000, true, "alice2@co.com")
17+
);
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.topfifty;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.stream.Collectors;
6+
7+
public class Q12EmployeesMapping {
8+
public static void main(String[] args) {
9+
List<Employees> employees = EmployeesDTO.getEmployees();
10+
Map<Integer, String> map = employees.stream().collect(Collectors.toMap(Employees::getId, Employees::getName));
11+
System.out.println(map);
12+
}
13+
}

0 commit comments

Comments
 (0)