Skip to content
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

Added hire and findPosition methods #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
62 changes: 62 additions & 0 deletions java/com/aa/act/interview/org/Organization.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,70 @@ public Organization() {
*/
public Optional<Position> hire(Name person, String title) {
//your code here
Position result = findPosition(root, title);

// Checks the result and returns an Optional<Employee> if one was found.
if (result != null) {
result.setEmployee(Optional.of(new Employee(0, person)));
return Optional.of(result);
}

// Target was not found by the search so and empty optional is returned.
return Optional.empty();
}

/*
* Finds the position in the organization tree who's title matches the given String
* using a depth-first tree traversal
*
* @param target
* @param title
* @return the wanted position or null if not found.
*/
public Position findPosition(Position target, String title) {

if (target.getTitle().equals(title)) {
return target;
} else if (!target.getDirectReports().isEmpty()) {
for(Position position : target.getDirectReports()) {
Position newTarget = findPosition(position, title);
if (newTarget != null)
return newTarget;
}
}

return null;
}

/*
* Finds the largest Identifier number in the given organization and returns it
*
* @param Position position
* @param int result
* @return an integer primitive relating to the largest Identifier in the organization or
* the given integer primitive "result" if none are found.
*/
public int findMaxID(Position position, int result) {

// compares the given position's employee's ID, if present, to the integer primitive parameter
if (position.getEmployee().isPresent()) {
if (position.getEmployee().get().getIdentifier() > result) {
result = position.getEmployee().get().getIdentifier();
}
}

// Checks the set of direct reports, if any, recursively and depth first
if (!position.getDirectReports().isEmpty()) {
for(Position newPosition : position.getDirectReports()) {
int newResult = findMaxID(newPosition, result);
if (newResult > result) {
result = newResult;
}
}
}

return result;
}

@Override
public String toString() {
Expand Down