From c3dd331941f3e4488ab6b218d74ae9ea29778236 Mon Sep 17 00:00:00 2001 From: mujjuham Date: Mon, 23 Oct 2023 21:26:57 -0700 Subject: [PATCH] Hire Method Changes --- dotnet/MyOrganization/Organization.cs | 46 +++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/dotnet/MyOrganization/Organization.cs b/dotnet/MyOrganization/Organization.cs index 7480481..a89ea4a 100644 --- a/dotnet/MyOrganization/Organization.cs +++ b/dotnet/MyOrganization/Organization.cs @@ -9,7 +9,7 @@ namespace MyOrganization internal abstract class Organization { private Position root; - + private int empId = 0; public Organization() { root = CreateOrganization(); @@ -26,9 +26,51 @@ public Organization() */ public Position? Hire(Name person, string title) { - //your code here + + Position? position = GetPosition(title, root); + + if (position != null) + { + //Checking for availed position + if (!position.IsFilled()) + { + //New hiring + Employee newEmployee = new Employee(GetId(), person); + position.SetEmployee(newEmployee); + return position; + } + } return null; } + /// + /// To get the position that has the title + /// + /// + /// + /// + private Position? GetPosition(string title, Position currentPosition) + { + if (currentPosition.GetTitle() != title) + { + foreach (Position? position in from Position pos in currentPosition.GetDirectReports() + let position = GetPosition(title, pos) + where position != null + select position) + { + return position; + } + + return null; + } + + return currentPosition; + } + + private int GetId() + { + empId++; + return empId; + } override public string ToString() {