Exemplo n.º 1
0
        // Get boss list of an employee from bottom to top
        public static List<Person> GetBossList(Person person)
        {
            List<Person> bossList = new List<Person>();

            while (person.HasBoss())
            {
                Person boss = person.GetBoss();
                bossList.Add(boss);
                person = boss;
            }

            return bossList;
        }
Exemplo n.º 2
0
 public void AddEmployee(Person employee)
 {
     if (this.Employees[MinimumEmployeesPerBoss - 1] == null)
     {
         for (int i = 0; i < this.Employees.Length; i++)
         {
             if (this.Employees[i] == null)
             {
                 this.Employees[i] = employee;
                 break;
             }
         }
     }
     else
     {
         throw new System.ArgumentOutOfRangeException("Cannot add more than " + MinimumEmployeesPerBoss + " employees per person");
     }
 }
Exemplo n.º 3
0
 public Person(string name, Person boss = null)
 {
     this.Name = name;
     this.Boss = boss;
     this.Employees = new Person[MinimumEmployeesPerBoss];
 }
Exemplo n.º 4
0
        public static void Main()
        {
            const string ExitMessage = "exit";

            List<Person> personList = new List<Person>();
            string hierarchy;

            Console.WriteLine("This program aims to find the first common boss of two employees in hierarchical structure of manager - employee");
            Console.WriteLine("Please enter first name: ");
            string employeeInput1 = Console.ReadLine().Trim();

            Console.WriteLine("Please enter second name: ");
            string employeeInput2 = Console.ReadLine().Trim();

            Console.WriteLine("Please enter N relations of {{manager}} - {{employee}} or click exit to execute the programe and find the common boss:");
            while ((hierarchy = Console.ReadLine().Trim()) != ExitMessage)
            {
                if (string.IsNullOrEmpty(hierarchy) || !hierarchy.Contains(" - "))
                {
                    Console.WriteLine("Wrong Input. Try again!");
                    continue;
                }

                string[] hierarchyArray = hierarchy.Split(new string[] { " - " }, StringSplitOptions.None);
                Person manager;
                if (personList.Count == 0)
                {
                    manager = new Person(hierarchyArray[0]);
                    personList.Add(manager);
                }
                else
                {
                    manager = personList.Find(employee => employee.Name == hierarchyArray[0]);
                }

                Person emp = new Person(hierarchyArray[1], manager);
                try
                {
                    manager.AddEmployee(emp);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    Console.WriteLine(ex.ParamName);
                }
                
                personList.Add(emp);
            }

            // Find the equivalent from the input of the employees from string to type Person
            Person employeePerson1 = personList.Find(employee => employee.Name == employeeInput1);
            Person employeePerson2 = personList.Find(employee => employee.Name == employeeInput2);
            
            List<Person> listBossesPerson1 = GetBossList(employeePerson1);
            List<Person> listBossesPerson2 = GetBossList(employeePerson2);
            try
            {              
                string commonBoss = GetCommonBoss(listBossesPerson1, listBossesPerson2).Name;
                Console.WriteLine("Common boss of {0} and {1} is {2}", employeeInput1, employeeInput2, commonBoss);
            }
            catch (ArgumentNullException ex)
            {
                Console.WriteLine(ex.ParamName);
            }        
        }