//will get the immediate supervisor using currentBoss to track progression public static Employee GetParentHelper(Employee currentBoss, Employee target) { Employee recurseResult; if(target == null){ return null; } if(target.Position == EmpRole.President) return null;//president has no boss //if currexntBoss isn't a possible boss of tartget, return null if(!Util.IsABoss(currentBoss, target)){ return null; } //if the target is the same level as the currentBoss's subordinates, // then we can just check the names to see if any of currentBoss's subs // match the target if(Util.DirectSubordinate(currentBoss.Position) == target.Position){ for(int i=0; i<currentBoss.Subordinates.Count; ++i){ if(currentBoss.Subordinates[i] == target){ return currentBoss; } } return null; } //if the target is not a direct subordinate of the currentBoss, then // we will have to descend into currentBoss's subtree to find them for(int i=0; i<currentBoss.Subordinates.Count; ++i){ recurseResult = GetParentHelper(currentBoss.Subordinates[i], target); if(recurseResult != null){ //if anything but null is returned, the boss is found so pass it up return recurseResult; } } //if nothing is found, return null return null; }
public CLI(Org org) { _org = org; currentEmployee = null; logoutFlag = false; QUIT_INFO_PROMPT = "Press " + QUIT_FULL + " or " + QUIT_SHORT + " at any time to exit."; initOptions(); }
public static bool CanTransfer(Employee boss, Employee toTransfer) { switch(boss.Position){ case EmpRole.President: case EmpRole.VicePresident: return CanFire(boss, toTransfer); default: return false; } }
public static List<Employee> AllPositionsAtHelper(Employee currentEmployee, EmpRole position) { List<Employee> result = new List<Employee>(); if(!currentEmployee.HasSubordinates()) return result; if(Util.DirectSubordinate(currentEmployee.Position) == position){ for(int i=0; i<currentEmployee.Subordinates.Count; ++i){ if(currentEmployee.Subordinates[i].Vacant){ result.Add(currentEmployee.Subordinates[i]); } } }else{ for(int i=0; i<currentEmployee.Subordinates.Count; ++i){ result.AddRange(AllPositionsAtHelper(currentEmployee.Subordinates[i], position)); } } return result; }
public virtual bool Fire(Employee firee) { Employee boss; bool orgFireRemoved; if(!Util.CanFire(this, firee)){ return false; } boss = Org.GetParentHelper(this, firee); if(boss != null && !firee.HasAnySubordinates()){ if(boss == null){ return false; } orgFireRemoved = firee.Vacant = true;//boss.Subordinates.Remove(firee); return orgFireRemoved; } return firee.Vacant = true; }
public static Employee GetEmployeeByNameHelper(Employee emp, string name) { Employee currentSub; Employee result; if(emp.Name == name){ return emp; } if(emp.Subordinates.Count == 0){ return null; } for(int i=0; i<emp.Subordinates.Count; ++i){ currentSub = emp.Subordinates[i]; result = GetEmployeeByNameHelper(currentSub, name); if(result != null){ return result; } } return null; }
public static bool CanPromote(Employee boss, Employee toPromote) { if(!CanFire(boss, toPromote)) return false; switch(boss.Position){ case EmpRole.President: if(toPromote.Position != EmpRole.Supervisor){ return false; } break; case EmpRole.VicePresident: if(toPromote.Position != EmpRole.Worker){ return false; } break; default: return false; } //make sure boss has a vacancy in his direct org if(boss.HasOpening()){ return true; } return false; }
public static bool IsABoss(Employee bossMaybe, Employee subMaybe) { return (int)bossMaybe.Position < (int)subMaybe.Position; }
public static bool CanFire(Employee boss, Employee firee) { return (Org.GetEmployeeByNameHelper(boss, firee.Name) != null); }
//Returns true if new position found, // returns false if layoff isn't possible public bool Layoff(Employee layoffer, Employee laidoff) { Employee boss, laidoffBoss; int replaceIdx, laidoffIdx; List<Employee> tempSubs; if(!Util.CanFire(layoffer, laidoff)) return false; //see if there is a position available Employee closest = ClosestVacantPosition(laidoff); if(closest == null){ return GetParent(laidoff).Fire(laidoff); } replaceIdx = GetSubordinateIdx(closest); laidoffIdx = GetSubordinateIdx(laidoff); boss = GetParent(closest); laidoffBoss = GetParent(laidoff); tempSubs = laidoff.Subordinates; if(closest.HasAnySubordinates()){ laidoff.Subordinates = closest.Subordinates; } boss.Subordinates[replaceIdx] = laidoff; laidoffBoss.Subordinates[laidoffIdx] = Util.VacantEmployee(laidoff.Position); laidoffBoss.Subordinates[laidoffIdx].Subordinates = tempSubs; return true; }
public override bool Quit(Employee boss) { return false; }
public virtual bool Transfer(Employee toTransfer, Employee targetManager) { Employee tempEmp; if(!Util.CanFire(this, toTransfer) || !Util.CanFire(this, targetManager)){ return false; } if(!targetManager.HasOpening()){ if(toTransfer.Position != targetManager.Position){ return false; } } //only transfer if the target position is if(toTransfer.Position != Util.DirectSubordinate(targetManager.Position)){ if(toTransfer.Position == targetManager.Position){ Util.SwapEmployees(this, toTransfer, targetManager); return true; } return false; } tempEmp = Util.EmployeeFromRole(toTransfer.Position, toTransfer.Name); Fire(toTransfer); targetManager.Hire(tempEmp); return true; }
//in a valid case, the manager will be a direct subordinate of // the current employee, and toPromote will be an employee of manager public virtual bool Promote(Employee manager, Employee toPromote) { Employee vacantPosition = null; int vacantPositionIdx = -1; Employee promoted = null; if(!Util.CanPromote(this, toPromote)) return false; //need to make sure there is a vacant position open that // is not the toPromote employees supervisor's position for(int i=0; i<Subordinates.Count; ++i){ if(Subordinates[i] == manager){ continue; } if(Subordinates[i].Vacant){ vacantPosition = Subordinates[i]; vacantPositionIdx = i; break; } } if(vacantPosition == null){ if(Subordinates.Count < MaxSubordinates){ vacantPosition = Util.VacantEmployee(Util.DirectSubordinate(Position)); Subordinates.Add(vacantPosition); vacantPositionIdx = Subordinates.IndexOf(vacantPosition); }else{ return false; } }; Util.SwapEmployees(this, toPromote, vacantPosition); // promoted = Util.EmployeeFromRole(vacantPosition.Position, toPromote.Name); // if(vacantPosition.HasAnySubordinates()){ // promoted.Subordinates = vacantPosition.Subordinates; // } // manager.Fire(toPromote);//shortcut to safely make old position vacant // Subordinates[vacantPositionIdx] = promoted; return true; }
public Org(Employee _president) { this.president = _president; this.Names = new List<string>(); AddName(_president.Name); }
public List<Employee> GetParentStack(Employee target) { List<Employee> result = new List<Employee>(); Employee boss = GetParent(target); while(boss != null){ result.Add(boss); boss = GetParent(boss); } return (result.Count == 0)?null:result; }
public int GetSubordinateIdx(Employee sub) { Employee boss = GetParent(sub); return boss.Subordinates.IndexOf(sub); }
public bool Transfer2(Employee transferer, Employee toTransfer, Employee transferTo) { return transferer.Transfer(toTransfer, transferTo); }
public bool Transfer(Employee transferer, Employee toTransfer) { if(!Util.CanTransfer(transferer, toTransfer)) return false; //calculate which open position is the closest Employee closest = ClosestVacantPosition(toTransfer); if(closest == null) return false; Employee boss, transferBoss; int replaceIdx, transferIdx; List<Employee> tempSubs; List<Employee> parentStack = GetParentStack(closest); if(closest != null){ boss = GetParent(closest); transferBoss = GetParent(toTransfer); replaceIdx = GetSubordinateIdx(closest); transferIdx = GetSubordinateIdx(toTransfer); tempSubs = toTransfer.Subordinates; if(closest.HasAnySubordinates()){ toTransfer.Subordinates = closest.Subordinates; } boss.Subordinates[replaceIdx] = toTransfer; transferBoss.Subordinates[transferIdx] = Util.VacantEmployee(toTransfer.Position); transferBoss.Subordinates[transferIdx].Subordinates = tempSubs; return true; } return false; }
public bool Promote(Employee promoter, Employee toPromote) { return promoter.Promote(GetParent(toPromote), toPromote); }
public static void SwapEmployees(Employee transferer, Employee emp1, Employee emp2) { Employee parent1 = Org.GetParentHelper(transferer, emp1); Employee parent2 = Org.GetParentHelper(transferer, emp2); int emp1Idx = parent1.Subordinates.IndexOf(emp1); int emp2Idx = parent2.Subordinates.IndexOf(emp2); Employee tempEmp = emp1; EmpRole tempRole = emp1.Position; emp1.Position = emp2.Position; emp2.Position = tempRole; List<Employee> tempSubs = emp1.Subordinates; emp1.Subordinates = emp2.Subordinates; emp2.Subordinates = tempSubs; parent1.Subordinates[emp1Idx] = emp2; parent2.Subordinates[emp2Idx] = tempEmp; // string tempName = emp1._Name; // bool tempVacant = emp1.Vacant; // // emp1._Name = emp2._Name; // emp1.Vacant = emp2.Vacant; // emp2._Name = tempName; // emp2.Vacant = tempVacant; }
public void Login() { Employee result = null; string name; Console.WriteLine( QUIT_INFO_PROMPT ); while(result == null){ print( LOGIN_PROMPT ); name = read().Trim(); if(name.Length == 0){ print( INVALID_NAME_INPUT ); } result = _org.GetEmployeeByName(name); if(result == null){ print( BAD_LOGIN ); continue; }else{ currentEmployee = result; print(WELCOME_PRE+currentEmployee.Name); print(WELCOME_POST+Util.GetRoleString(currentEmployee.Position)); } } }
public void Logout() { if(currentEmployee.Name != "*"){ print(LOGOUT_DIALOG+currentEmployee.Name); } currentEmployee = null; logoutFlag = true; }
public virtual bool Hire(Employee hiree) { if(hiree.Position != Util.DirectSubordinate(Position)) return false; Employee sub; //first attempt to find a vacant position if it exists if(HasSubordinates()){ for(var i=0; i<Subordinates.Count; ++i){ sub = Subordinates[i]; if(sub.Vacant){ if(sub.HasAnySubordinates()){ hiree.Subordinates = sub.Subordinates; } Subordinates[i] = hiree; return true; } } } //if no vacancy exists, see if the hiree can be added to the curren list if(Subordinates.Count < MaxSubordinates){ Subordinates.Add(hiree); return true; } return false; }
//returns null on failure public Employee GetParent(Employee target) { return GetParentHelper(president, target); }
//fire public virtual bool Quit(Employee boss) { return boss.Fire(this); }
public static bool HasAnySubordinatesHelper(Employee currentEmployee) { bool recurseResult; //base //if the current employee has no subordinates, return false if(!currentEmployee.HasSubordinates()) return false; //if the current employee is a worker, return false if(currentEmployee.Position == EmpRole.Worker) return false; //otherwise traverse their subordinates for(int i=0; i<currentEmployee.Subordinates.Count; ++i){ recurseResult = HasAnySubordinatesHelper(currentEmployee.Subordinates[i]); if(recurseResult){ return true; } } return false; }
public virtual bool TransferSwap(Employee toTransfer, Employee targetManager) { return false; }
public static bool HasVacancy(Employee emp) { if(emp.Position == EmpRole.Worker){ return false; } if(emp.Subordinates.Count < emp.MaxSubordinates){ return true; } foreach(Employee sub in emp.Subordinates){ if(sub.Vacant){ return true; } } return false; }
public bool Fire(Employee boss, Employee subordinate) { if(boss == null || subordinate == null) return false; bool orgFire; Names.Remove(subordinate.Name); orgFire = boss.Fire(subordinate); return orgFire; }
public bool Hire(Employee boss, Employee subordinate) { AddName(subordinate.Name); return boss.Hire(subordinate); }