From eedf38bc155698228335f4b91a23e1dd9a27ba90 Mon Sep 17 00:00:00 2001 From: "Anthony D. Mays" Date: Tue, 29 Oct 2024 04:18:46 +0000 Subject: [PATCH] feix: asserts that employee ids are actually in the collection Signed-off-by: Anthony D. Mays --- .../codedifferently/lesson15/EmployeeManager.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/EmployeeManager.java b/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/EmployeeManager.java index 66337295..4ac9edc4 100644 --- a/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/EmployeeManager.java +++ b/lesson_15/tdd/tdd_app/src/main/java/com/codedifferently/lesson15/EmployeeManager.java @@ -4,7 +4,8 @@ import java.util.Map; public class EmployeeManager { - private Map employees; + + private final Map employees; public EmployeeManager() { employees = new HashMap<>(); @@ -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(); }