private void Switch(Daycare dc, Wish wish) { try { var team = dc.Teams[wish.Employee.Team]; AdjustTeamByWish(team, wish, dc.Employees.Count); var current = Convert.ToInt32(wish.Employee.Shifts[wish.Day].Shift); var wanted = wish.WantedShift; if (current != wanted) { AdjustDaycareByWish(dc, wish, current); } } catch (Exception e) { throw e; } }
private void AdjustTeamByWish(Team team, Wish wish, int empCount) { var teamFirst = team.TeamEmp.FindIndex(e => e.Shifts[wish.Day].Shift <= (empCount / 3 - 1)); var teamLast = team.TeamEmp.FindIndex(e => e.Shifts[wish.Day].Shift >= (empCount / 3 * 2)); var teamMiddle = team.TeamEmp.FindIndex(e => e.Shifts[wish.Day].Shift >= (empCount / 3) && e.Shifts[wish.Day].Shift <= (empCount / 3 * 2 - 1)); var wisher = team.TeamEmp.FindIndex(e => e == wish.Employee); if (wish.WantedShift <= (empCount / 3 - 1) && team.TeamEmp[teamFirst] != wish.Employee) { if (team.TeamEmp[teamFirst].Shifts[wish.Day].Locked) { throw new Exception($"Two wishes in same team for same time on day {wish.Day}. Remove one."); } var current = team.TeamEmp[teamFirst].Shifts[wish.Day]; var newSwitch = wish.Employee.Shifts[wish.Day]; team.TeamEmp[teamFirst].Shifts[wish.Day] = newSwitch; team.TeamEmp[wisher].Shifts[wish.Day] = current; } else if (wish.WantedShift <= (empCount / 3 * 2 - 1) && wish.WantedShift >= (empCount / 3) && team.TeamEmp[teamMiddle] != wish.Employee) { if (team.TeamEmp[teamMiddle].Shifts[wish.Day].Locked) { throw new Exception($"Two wishes in same team for same time on day {wish.Day}. Remove one."); } var current = team.TeamEmp[teamMiddle].Shifts[wish.Day]; var newSwitch = wish.Employee.Shifts[wish.Day]; team.TeamEmp[teamMiddle].Shifts[wish.Day] = newSwitch; team.TeamEmp[wisher].Shifts[wish.Day] = current; } else if (wish.WantedShift >= (empCount / 3 * 2) && team.TeamEmp[teamLast] != wish.Employee) { if (team.TeamEmp[teamLast].Shifts[wish.Day].Locked) { throw new Exception($"Two wishes in same team for same time on day {wish.Day}. Remove one."); } var current = team.TeamEmp[teamLast].Shifts[wish.Day]; var newSwitch = wish.Employee.Shifts[wish.Day]; team.TeamEmp[teamLast].Shifts[wish.Day] = newSwitch; team.TeamEmp[wisher].Shifts[wish.Day] = current; } }
private void AdjustDaycareByWish(Daycare dc, Wish wish, int current) { if (current >= dc.Employees.Count / 3 && current <= dc.Employees.Count / 3 * 2 - 1) { return; } var shiftList = PossibleShifts(wish.WantedShift, dc.Teams.Count); shiftList.Remove(current); foreach (var item in shiftList) { var emp = dc.Employees.Find(e => Convert.ToInt32(e.Shifts[wish.Day].Shift) == item); if (!emp.Shifts[wish.Day].Locked) { var oldShift = wish.Employee.Shifts[wish.Day].Shift; var newShift = emp.Shifts[wish.Day].Shift; wish.Employee.Shifts[wish.Day].Shift = newShift; emp.Shifts[wish.Day].Shift = oldShift; wish.Employee.Shifts[wish.Day].Locked = true; break; } } }