private List <Employee> CreateEmployeesFromLines(string[] lines) { int countLines = lines.Length; List <Employee> tempEmps = new List <Employee>(countLines); //HashSet is for ensuring Employee Ids are unique. If you encounter duplicates, reject entry HashSet <string> addedEmployeeIds = new HashSet <string>(); //List errors is for recording errors in lines/CSV when creating Employee StringBuilder errorStrBuilder = new StringBuilder(); int ceoFound = 0; for (int i = 0; i < countLines; i++) { try { Employee emp = new Employee(lines[i]); if (addedEmployeeIds.Contains(emp.EmpId)) { errorStrBuilder.Append((i + 1) + ". '" + emp.EmpId + "' has already been added.\n"); } else { if (emp.ManagerId == "") { if (ceoFound == 0) { ++ceoFound; this.ceo = emp; this.indexOfCeo = tempEmps.Count;//Before insertion... tempEmps.Add(emp); addedEmployeeIds.Add(emp.EmpId); } else { ++ceoFound; } } else { tempEmps.Add(emp); addedEmployeeIds.Add(emp.EmpId); } } } catch (FormatException fe) { errorStrBuilder.Append((i + 1) + ". " + fe.Message + "\n"); } } if (errorStrBuilder.Length > 0) { throw new FormatException(errorStrBuilder.ToString()); } if (ceoFound != 1) { if (ceoFound == 0) { throw new FormatException("You have no CEO. You have to enter one.\n"); } else { throw new FormatException("You can only have one CEO and you have " + ceoFound + ".\n"); } } if (tempEmps.Count == 0) { throw new FormatException("No employees were created successfully.\n");//Overcautious much?????? } if (lines.Length > tempEmps.Count) { throw new FormatException("Not all lines were successfully added.\n");//Cause only the paranoid survive. } //Are those all the possible errors? return(tempEmps); }
public void AddSubordinate(Employee sub)//subordinate { subordinates.Add(sub); }
private void BuildTree(List <Employee> tempEmps) { //Using a BFS like way of inserting into tree Queue <Employee> queueToBuild = new Queue <Employee>(); queueToBuild.Enqueue(ceo); //Is it better to remove indeces added while looping (cause how long it takes is an issue) or is it better to just record //all indeces added but have to iterate though the whole loop over and over //Iterating seems easier cause removing involves having to shift all the values to your left so will not do that even though will be going through //an employee we may have added already. //Shown both //You can use either but not both. Just comment and uncomment where highlighted. //1. Where we remove elements immediately after adding them. tempEmps.RemoveAt(this.indexOfCeo); while (queueToBuild.Count > 0) { Employee emp = queueToBuild.Dequeue(); int countSubordinates = queueToBuild.Count; List <int> indecesToRemove = new List <int>(); for (int i = 0; i < tempEmps.Count; i++) { if (tempEmps[i].ManagerId == emp.EmpId) { queueToBuild.Enqueue(tempEmps[i]); emp.AddSubordinate(tempEmps[i]); indecesToRemove.Add(i); } } for (int j = indecesToRemove.Count - 1; j >= 0; j--) { tempEmps.RemoveAt(indecesToRemove[j]); } if (queueToBuild.Count > countSubordinates) { emp.HasSubordinates = true; } else { emp.HasSubordinates = false; emp.CumulativeSalary = emp.Salary; } } if (tempEmps.Count > 0) { StringBuilder strNotAdded = new StringBuilder(); strNotAdded.Append("The following employees were not added: "); for (int i = 0; i < tempEmps.Count; i++) { strNotAdded.Append(tempEmps[i].EmpId + ", "); } strNotAdded.Remove(strNotAdded.Length - 2, 2); strNotAdded.Append(".\n"); throw new FormatException(strNotAdded.ToString()); } //end of version 1 // 2 of code that does the same thing /* * List<int> indecesAdded = new List<int>();//Comment this out if you opt to change * indecesAdded.Add(this.indexOfCeo);//Comment this too * while(queueToBuild.Count>0) * { * Employee emp = queueToBuild.Dequeue(); * int countSubordinates = queueToBuild.Count(); * * for(int i=0;i<tempEmps.Count;i++) * { * if(tempEmps[i].ManagerId==emp.EmpId) * { * queueToBuild.Enqueue(tempEmps[i]); * emp.AddSubordinate(tempEmps[i]); * indecesAdded.Add(i); * } * } * if(queueToBuild.Count>countSubordinates) * { * emp.HasSubordinates=true; * } * else * { * emp.HasSubordinates=false; * emp.CumulativeSalary=emp.Salary; * } * } * * if(tempEmps.Count>indecesAdded.Count) * { * //Find the ones not added * StringBuilder strBuild = new StringBuilder(); * * int howMany=0; * int offset = 0; * //Before the first one was entered. Yikes if indecesAdded length is 0 * int firstIndex = indecesAdded[0]; * for(;offset<firstIndex;offset++) * { * strBuild.Append(tempEmps[offset].EmpId+", "); ++howMany; * } * * //The gaps in between * for(int i=0;i<indecesAdded.Count-1;i++) * { * int currentIndex=indecesAdded[i]; * int nextIndex=indecesAdded[i+1]; * if((currentIndex+1)<nextIndex) * { * offset=currentIndex+1; * for(;offset<nextIndex;offset++) * { * strBuild.Append(tempEmps[offset].EmpId+", "); ++howMany; * } * } * } * * //After last added index, there are could be others * if((tempEmps.Count-indecesAdded.Count)>howMany) * { * offset=indecesAdded[indecesAdded.Count-1]+1; * for(;offset<tempEmps.Count;offset++) * { * strBuild.Append(tempEmps[offset].EmpId+", "); ++howMany; * } * } * * strBuild.Remove(strBuild.Length-2,2); * strBuild.Append("."); * throw new FormatException("The following "+howMany+" employee(s) were not added: "+strBuild.ToString()); * } * * //end of version 2. Code calls */ }