Skip to content
Open
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
37 changes: 35 additions & 2 deletions java/com/aa/act/interview/org/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public abstract class Organization {

private Position root;

private int id = 1;

public Organization() {
root = createOrganization();
}
Expand All @@ -20,10 +21,42 @@ public Organization() {
* @return the newly filled position or empty if no position has that title
*/
public Optional<Position> hire(Name person, String title) {
//your code here
// assign employee if title is CEO/root
if (root.getTitle().equals(title)) {
return assignEmployee(root, person);
} else {
// or visit direct reports
return hire(root, person, title);
}
}

private Optional<Position> hire(Position currentPosition, Name person, String title) {
// visit all direct reports
for (Position pos : currentPosition.getDirectReports()) {
if (pos.getTitle().equals(title)) {
// Assign employee if title matches
return assignEmployee(pos, person);
} else if (!pos.getDirectReports().isEmpty()) {
// Recursively visit direct reports
Optional<Position> newPos = hire(pos, person, title);
if (newPos.isPresent()) {
return newPos;
}
}
}
return Optional.empty();
}

private Optional<Position> assignEmployee(Position position, Name person) {
if (!position.isFilled()) {
position.setEmployee(Optional.of(new Employee(id, person)));
id += 1;
return Optional.of(position);
} else {
throw new IllegalArgumentException("Position is already filled.");
}
}

@Override
public String toString() {
return printOrganization(root, "");
Expand Down