Skip to content

feix: asserts that employee ids are actually in the collection #482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import java.util.Map;

public class EmployeeManager {
private Map<Integer, Employee> employees;

private final Map<Integer, Employee> employees;

public EmployeeManager() {
employees = new HashMap<>();
Expand All @@ -15,17 +16,27 @@ public void addEmployee(Employee employee) {
}

public Employee getEmployee(int id) {
assertEmployeeInCollection(id);
return employees.get(id);
}

public void updateEmployee(Employee employee) {
assertEmployeeInCollection(employee.getId());
employees.put(employee.getId(), employee);
}

public void removeEmployee(int id) {
assertEmployeeInCollection(id);
employees.remove(id);
}

private void assertEmployeeInCollection(int id) {
if (this.employees.containsKey(id)) {
return;
}
throw new IllegalArgumentException("Employee does not in collection with id " + id);
}

public int getEmployeeCount() {
return employees.size();
}
Expand Down
Loading