Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //Wejście
            int meeting_duration = 30;

            Calendar.working_hours working_hours1 = new Calendar.working_hours();
            working_hours1.start = working_hours1.start.AddHours(8).AddMinutes(0);
            working_hours1.end   = working_hours1.end.AddHours(19).AddMinutes(55);

            Calendar.working_hours working_hours2 = new Calendar.working_hours();
            working_hours2.start = working_hours2.start.AddHours(10).AddMinutes(0);
            working_hours2.end   = working_hours2.end.AddHours(18).AddMinutes(30);

            //Planowane Spotkania - Kalendarz1
            Calendar.planned_meeting planned_meeting1 = new Calendar.planned_meeting();
            planned_meeting1.start.Add(new DateTime().AddHours(9).AddMinutes(0));
            planned_meeting1.end.Add(new DateTime().AddHours(10).AddMinutes(30));

            planned_meeting1.start.Add(new DateTime().AddHours(12).AddMinutes(0));
            planned_meeting1.end.Add(new DateTime().AddHours(13).AddMinutes(0));

            planned_meeting1.start.Add(new DateTime().AddHours(16).AddMinutes(0));
            planned_meeting1.end.Add(new DateTime().AddHours(18).AddMinutes(0));

            //Planowane Spotkania - Kalendarz2
            Calendar.planned_meeting planned_meeting2 = new Calendar.planned_meeting();
            planned_meeting2.start.Add(new DateTime().AddHours(10).AddMinutes(0));
            planned_meeting2.end.Add(new DateTime().AddHours(11).AddMinutes(30));


            planned_meeting2.start.Add(new DateTime().AddHours(12).AddMinutes(30));
            planned_meeting2.end.Add(new DateTime().AddHours(14).AddMinutes(30));

            planned_meeting2.start.Add(new DateTime().AddHours(14).AddMinutes(30));
            planned_meeting2.end.Add(new DateTime().AddHours(15).AddMinutes(0));

            planned_meeting2.start.Add(new DateTime().AddHours(16).AddMinutes(0));
            planned_meeting2.end.Add(new DateTime().AddHours(17).AddMinutes(0));
            //Algorytm
            Calendar.working_hours working_hours_merge = new Calendar.working_hours();
            if (working_hours1.start.CompareTo(working_hours2.start) > 0)
            {
                working_hours_merge.start = working_hours_merge.start.AddHours(get_hours(working_hours1.start.ToString("HH:mm"))).AddMinutes(get_minutes(working_hours1.start.ToString("HH:mm")));
            }
            else
            {
                working_hours_merge.start = working_hours_merge.start.AddHours(get_hours(working_hours2.start.ToString("HH:mm"))).AddMinutes(get_minutes(working_hours2.start.ToString("HH:mm")));
            }
            if (working_hours1.end.CompareTo(working_hours2.end) < 0)
            {
                working_hours_merge.end = working_hours_merge.end.AddHours(get_hours(working_hours1.end.ToString("HH:mm"))).AddMinutes(get_minutes(working_hours1.end.ToString("HH:mm")));
            }
            else
            {
                working_hours_merge.end = working_hours_merge.end.AddHours(get_hours(working_hours2.end.ToString("HH:mm"))).AddMinutes(get_minutes(working_hours2.end.ToString("HH:mm")));
            }
            var merged_calendar     = get_merged_calendar(planned_meeting1, planned_meeting2);
            var normalized_calendar = get_optimized_calendar(merged_calendar);
            var adjusted_calendar   = get_working_hours(normalized_calendar, working_hours_merge);
            var available_slots     = get_available_time_slots(adjusted_calendar, meeting_duration);

            //Wyjscie
            if (available_slots.start.Count == 0)
            {
                Console.WriteLine("Brak wolnych slotów czasowych");
            }
            for (int i = 0; i < available_slots.start.Count; ++i)
            {
                Console.WriteLine(available_slots.start[i].ToString("HH:mm") + " - " + available_slots.end[i].ToString("HH:mm"));
            }
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public static Calendar.planned_meeting get_working_hours(Calendar.planned_meeting calendar, Calendar.working_hours working_hours)
        {
            var pom1_start = calendar.start[0];
            var start_time = pom1_start;
            var pom2_end   = calendar.end[calendar.end.Count - 1];
            var end_time   = pom2_end;

            if (compare_times(start_time.ToString("HH:mm"), working_hours.start.ToString("HH:mm")) == -1)
            {
                int i = 0;
                while (i < calendar.start.Count)
                {
                    if (compare_times(calendar.end[i].ToString("HH:mm"), working_hours.start.ToString("HH:mm")) == -1)
                    {
                        calendar.start.RemoveAt(i);
                        calendar.end.RemoveAt(i);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if (compare_times(end_time.ToString("HH:mm"), working_hours.end.ToString("HH:mm")) != -1)
            {
                int i = calendar.start.Count - 1;
                while (i >= 0)
                {
                    if (compare_times(calendar.start[i].ToString("HH:mm"), working_hours.end.ToString("HH:mm")) != -1)
                    {
                        calendar.end.RemoveAt(i);
                        calendar.start.RemoveAt(i);
                    }
                    i--;
                }
            }
            if (calendar.start.Count > 0 && compare_times(calendar.start[0].ToString("HH:mm"), working_hours.start.ToString("HH:mm")) == -1)
            {
                calendar.start[0] = working_hours.start;
            }
            if (compare_times(end_time.ToString("HH:mm"), working_hours.end.ToString("HH:mm")) == -1)
            {
                calendar.start.Add(working_hours.end);
                calendar.end.Add(new DateTime().AddHours(23).AddMinutes(59));
                int i = calendar.start.Count - 1;
                while (i >= 1)
                {
                    if (compare_times(calendar.end[i].ToString("HH:mm"), calendar.start[i].ToString("HH:mm")) == -1)
                    {
                        calendar.end[i] = calendar.end[i - 1];
                    }
                    i--;
                }
            }
            else if (calendar.start.Count > 0 && compare_times(calendar.end[calendar.start.Count - 1].ToString("HH:mm"), working_hours.end.ToString("HH:mm")) != -1)
            {
                calendar.end[calendar.start.Count - 1] = working_hours.end;
            }
            return(calendar);
        }