I'm currently programming an appointment finder that automatically syncs with MS Exchange Server. The program should lookup multiple users' calendar when they are available. I have coded some demo data with multiple users and their appointments. The appointments consist of two DateTimes.
So my question is: How can I find a gap between multiple users appointments, where everyone is available. This is the demo data:
var appointment1ForPerson1 = new Appointment(1, new List<Appointment.Time>() { (new Appointment.Time(new DateTime(2020, 7, 7, 16, 45, 0), new DateTime(2020, 7, 7, 17, 0, 0))) });
var appointment2ForPerson1 = new Appointment(3, new List<Appointment.Time>() { (new Appointment.Time(new DateTime(2020, 7, 9, 9, 0, 0), new DateTime(2020, 7, 9, 12, 0, 0))) });
var appointment3ForPerson1 = new Appointment(5, new List<Appointment.Time>() { (new Appointment.Time(new DateTime(2020, 7, 11, 10, 0, 0), new DateTime(2020, 7, 11, 12, 0, 0))) });
var appointmentsForPerson1 = new List<Appointment>() { appointment1ForPerson1, appointment2ForPerson1, appointment3ForPerson1 };
var person1 = new Person("steven@example.de", appointmentsForPerson1);
_userList.Add(person1);
var appointment1ForPerson2 = new Appointment(1, new List<Appointment.Time>() { (new Appointment.Time(new DateTime(2020, 7, 7, 16, 45, 0), new DateTime(2020, 7, 7, 17, 0, 0))) });
var appointment2ForPerson2 = new Appointment(3, new List<Appointment.Time>() { (new Appointment.Time(new DateTime(2020, 7, 9, 9, 0, 0), new DateTime(2020, 7, 9, 12, 0, 0))) });
var appointment3ForPerson2 = new Appointment(5, new List<Appointment.Time>() { (new Appointment.Time(new DateTime(2020, 7, 11, 10, 0, 0), new DateTime(2020, 7, 11, 12, 0, 0))) });
var appointmentsForPerson2 = new List<Appointment>() { appointment1ForPerson2, appointment2ForPerson2, appointment3ForPerson2 };
var person2 = new Person("jonas@example.de", appointmentsForPerson2);
_userList.Add(person2);
Here is the Person class with the Appointment and Time:
class Person
{
private readonly string _email;
private readonly List<Appointment> _appointments;
public Person(string email, List<Appointment> appointments)
{
_email = email;
_appointments = appointments;
}
public string Email
{
get { return _email; }
}
public List<Appointment> Appointments
{
get { return _appointments; }
}
internal class Appointment
{
private readonly List<Time> _appointmentHoursList;
public Appointment(List<Time> appointmentHoursList)
{
_appointmentHoursList = appointmentHoursList;
}
public List<Time> AppointmentHoursList
{
get { return _appointmentHoursList; }
}
internal class Time
{
private readonly DateTime _beginningTime;
private readonly DateTime _endTime;
public Time(DateTime beginningTime, DateTime endTime)
{
_beginningTime = beginningTime;
_endTime = endTime;
}
public DateTime BeginningTime
{
get { return _beginningTime; }
}
public DateTime EndTime
{
get { return _endTime; }
}
}
}
}