diff --git a/java/com/aa/act/interview/org/Organization.java b/java/com/aa/act/interview/org/Organization.java index e1c8ed1..4b9c6e1 100644 --- a/java/com/aa/act/interview/org/Organization.java +++ b/java/com/aa/act/interview/org/Organization.java @@ -1,17 +1,19 @@ package com.aa.act.interview.org; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; public abstract class Organization { private Position root; + private AtomicInteger employeeCounter; // Unique identifier for new employees public Organization() { root = createOrganization(); + employeeCounter = new AtomicInteger(0); // Initialize employee counter } protected abstract Position createOrganization(); - /** * hire the given person as an employee in the position that has that title * @@ -19,8 +21,25 @@ public Organization() { * @param title * @return the newly filled position or empty if no position has that title */ + public Optional hire(Name person, String title) { - //your code here + return hire(root, person, title); + } + + private Optional hire(Position position, Name person, String title) { + if(position.getTitle().equals(title) && !position.isFilled()) { + Employee newEmployee = new Employee(employeeCounter.incrementAndGet(), person); + position.setEmployee(Optional.of(newEmployee)); + return Optional.of(position); + } + + for(Position report : position.getDirectReports()) { + Optional result = hire(report, person, title); + if(result.isPresent()) { + return result; + } + } + return Optional.empty(); }