From fa415e3b075aad38c6eb1cf82be46fbac3ccbb33 Mon Sep 17 00:00:00 2001 From: BCColton <99459995+BCColton@users.noreply.github.com> Date: Thu, 6 Apr 2023 19:56:50 -0700 Subject: [PATCH 1/2] Added hire and findPosition methods Contributed a depth-first findPosition method that feeds the found result to the hire method. The Hire method then uses this result to update the organization or returns and empty position having done nothing. --- .../aa/act/interview/org/Organization.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/java/com/aa/act/interview/org/Organization.java b/java/com/aa/act/interview/org/Organization.java index 5060509..7620f9a 100644 --- a/java/com/aa/act/interview/org/Organization.java +++ b/java/com/aa/act/interview/org/Organization.java @@ -21,8 +21,41 @@ public Organization() { */ public Optional hire(Name person, String title) { //your code here + Position result = findPosition(root, title); + + // Checks the result and returns an Optional 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; + } + @Override public String toString() { From 0235cfac2a964a47c1f46b3458ec177e9dcca92f Mon Sep 17 00:00:00 2001 From: BCColton <99459995+BCColton@users.noreply.github.com> Date: Thu, 13 Apr 2023 13:22:58 -0700 Subject: [PATCH 2/2] Added findMaxID method to Organization.java Added findMaxID method to Organization.java and implemented to give each employee a unique ID number --- .../aa/act/interview/org/Organization.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/java/com/aa/act/interview/org/Organization.java b/java/com/aa/act/interview/org/Organization.java index 7620f9a..e46a48c 100644 --- a/java/com/aa/act/interview/org/Organization.java +++ b/java/com/aa/act/interview/org/Organization.java @@ -55,7 +55,36 @@ public Position findPosition(Position target, String title) { 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() {