diff --git a/java/com/aa/act/interview/org/Organization.java b/java/com/aa/act/interview/org/Organization.java index e1c8ed1..3be0c72 100644 --- a/java/com/aa/act/interview/org/Organization.java +++ b/java/com/aa/act/interview/org/Organization.java @@ -1,13 +1,38 @@ package com.aa.act.interview.org; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; public abstract class Organization { + private AtomicInteger id = new AtomicInteger(); //emp id for new hires (assigned sequentially) + private Map positions = new HashMap<>(); //stores position obj by title for efficient lookup O(1) private Position root; public Organization() { root = createOrganization(); + // single call to populate all the positions in the map during org creation/setup + populateAllPositions(root); + } + + /** + * update map with all the positions in the org starting at the root + * (this function uses recursion but is still efficient as the computation is done only once at the beginning) + * + * @param position + */ + private void populateAllPositions(Position position) { + Optional.ofNullable(position) + .ifPresent(p -> { + positions.put(p.getTitle(), p); + Optional.ofNullable(p.getDirectReports()) + .ifPresent(drs -> + // add position(s) of direct report(s) recursively + drs.forEach(this::populateAllPositions)); + } + ); } protected abstract Position createOrganization(); @@ -20,8 +45,14 @@ public Organization() { * @return the newly filled position or empty if no position has that title */ public Optional hire(Name person, String title) { - //your code here - return Optional.empty(); + return Optional.ofNullable(positions.get(title)) + .map(position -> { + position.setEmployee(Optional.of(new Employee(id.incrementAndGet(), person))); + // update position in map to reflect hire, this isn't necessary but more of a nice-to-have + positions.put(title, position); + return position; + } + ); } @Override