mardi 10 avril 2018

How to add a varying number of Clients to the List along with Date, for 30 days, two different Clients for each date without repeating code?

I have a code like this:

const int IterationsPerDay = 2;
DateTimeOffset currentDate = DateTime.Now.Date;
int offset = 0;
for (int i = 0; i < 30; i += IterationsPerDay)
{
    for (int j = 0; j < IterationsPerDay; j++)
    {
        int iterationIndex = i + j;
            if (iterationIndex < clientsList.Count)
            {
                Client _client = new Client();
        _client.Name = clientsList[iterationIndex].ClientName;
        _client.Date = currentDate.AddDays(offset);
        DatingList.Add(_client);
            } else
            if (iterationIndex >= clientsList.Count && iterationIndex < 2 * clientsList.Count)
            {
                Client _client = new Client();
        _client.Name = clientsList[iterationIndex - clientsList.Count].ClientName;
        _client.Date = currentDate.AddDays(offset);
        DatingList.Add(_client);
            } else
    if (iterationIndex >= 2 * clientsList.Count && iterationIndex < 3 * clientsList.Count)
    {
        Client _client = new Client();
        _client.Name = clientsList[iterationIndex - (2 * clientsList.Count)].ClientName;
        _client.Date = currentDate.AddDays(offset);
        DatingList.Add(_client);
    } else
    if (...)
    { 
        (...)
    } else
    if (iterationIndex >= 29 * clientList.Count && iterationIndex < 30 * clientsList.Count)
    {
        Client _client = new Client();
        _client.name = clientsList[iterationIndex - (29 * clientsList.Count)].ClientName;
        _client.Date = currentDate.AddDays(offset);
        DatingList.Add(_client);
    }
    offset += 1; 
}

Is there better way (and shorter in code as well) to continue adding _client to the DatingList until total of 30 dates are reached?

As you can see I have

30 if/else statements

to ensure that 30 days will be added (just in case there was only 1 _client).

The _client number is not fixed.

There are 2 different _clients for each date.

(The only exception will be _client.count = 1, at this point DatingList will get same client twice for each date).

Aucun commentaire:

Enregistrer un commentaire