How to appoint a unique employee on weekends in C #

I have a list of 7 employees. I repeat the cycle of dates of the current month and I want to appoint two employees for each date, but on weekends they should not repeat until all employees are appointed. For example: I have seven employees:

John         
Sophia       
Olivia
Davis          
Clark          
Paul         
Thomas         

Now my date loop:

for (int i = 0; i < dates.Length; i++)
 {
       DateTime newDate = new DateTime();
        newDate = dates[i];
      /*if(newdate == "Saturday")
        var EmpName1 = emplist[i];
        var EmpName2 = emplist[i];*/
 }

In the above cycle, I want to appoint two employees each on Saturday and Sunday, until all the others have been appointed earlier. Something like that:

4th March: John and Sophia
5th March: Olivia and Davis
11th March: Clark and Paul
12th March: Thomas and John

etc. John will not be appointed until all of them are appointed. After that, the list will start again. Can someone help me with this?

+4
source share
3 answers

, , . :

index = (index + 1) % employees.Length // Number fo employees

% ( modulo) , 0 , employee.Length .

- :

var empIndex = 0;

for (int i = 0; i < dates.Length; i++)
{
   DateTime newDate = new DateTime();
    newDate = dates[i];
    if(newdate == "Saturday") // and Sunday, use or: || (newData == "Sunday"))
    {
       var EmpName1 = emplist[empIndex];
       empIndex = (empIndex + 1) % empList.Length;
       var EmpName2 = emplist[empIndex];
       empIndex = (empIndex + 1) % empList.Length;
   }
}
+1

, DateOfLastWeekendOnCall?

var e = Employees.OrderBy(i=>i.DateOfLastWeekendOnCall).First();
e.DateOfLastWeekendOnCall = weekendThatNeedsAssigning;

Explaination:

" , "

" , ( )"

, , , , , .

MinDate

+1

You can use the queue:

var weekendWarriors = new Queue<string>();
CheckRefreshQueue<string>(weekendWarriors, employees);

for (int i = 0; i < dates.Length; i++)
{
    DateTime newDate = new DateTime();
    newDate = dates[i];

    if (newDate.DayOfWeek == DayOfWeek.Saturday || newDate.DayOfWeek == DayOfWeek.Sunday)
    {   
        string emp1;
        string emp2;
        CheckRefreshQueue<string>(weekendWarriors, employees);
        emp1 = weekendWarriors.Dequeue();
        CheckRefreshQueue<string>(weekendWarriors, employees);
        emp2 = weekendWarriors.Dequeue();
    }
}

Here CheckRefreshQueue:

private static void CheckRefreshQueue<T>(Queue<T> toRefresh, IEnumerable<T> fromCollection)
{
    if (toRefresh.Count == 0) foreach (T item in fromCollection) toRefresh.Enqueue(item);
}
0
source

Source: https://habr.com/ru/post/1671886/


All Articles