简体   繁体   中英

How do I arrange a list to a pre-built templates? (Console)

I have a generic list of names whose number depends on what the user selects. I want to arrange those names in a range of times of the day that the user input before. For example: we have 3 names, the user input 08:00-17:00. From 08:00-17:00 we have 9 hours -> 9/3=3, so arrange the names in a 3 hours templates.

This is how it looks in code:

List<string> myList = new List<string>();
myList.Add(Name1);
myList.Add(Name2);
myList.Add(Name3);

Console.WriteLine("Enter hours range: ");
Console.ReadLine(); //in this case the user input is 08:00-17:00

if (myList.Count == 3)
{
    Console.WriteLine("8-11 " + myList.ElementAt(0)); //Name1
    Console.WriteLine("11-14 " + myList.ElementAt(1)); //Name2
    Console.WriteLine("14-17 " + myList.ElementAt(2)); //Name3
}

The problem comes, when there are more than 3 names and the hours are halved (lets say 17:30).

Any ideas on how to do this kind of thing?

You could do something like

private static void CalculateTimeIntervals()
    {
        var startTime = new DateTime(2014, 8, 15, 8, 0, 0);
        var endTime = new DateTime(2014, 8, 15, 17, 0, 0);
        var lengthOfTime = endTime - startTime;
        var numberOfPeople = 4;
        var dividedLengthOfTime = lengthOfTime.Ticks / numberOfPeople;
        var people = new List<Person>();

        for (int i = 1; i <= numberOfPeople; i++)
        {
            people.Add(
                new Person
                {
                    Id = i,
                    StartTime = startTime.AddTicks((dividedLengthOfTime * i) - dividedLengthOfTime),
                    EndTime = startTime.AddTicks(dividedLengthOfTime * i)
                });
        }
    }

where Person looks like

public class Person
{
    public int Id { get; set; }
    public DateTime StartTime { get; set; }
    public DateTime EndTime { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM