int firstAppointmentIndex = parent.getSchedule().getFirstAppointment().getSlotIndex();
int lastAppointmentIndex = parent.getSchedule().getLastAppointment().getSlotIndex();
int index; //chosen index for the appointment
ScheduleIntersection intersection = new ScheduleIntersection(parent.getSchedule(), teacher.getSchedule());
//try to find a non-conflicting appointment for that parent
//try first in the range [firstAppointmentIndex, lastAppointmentIndex]
lastIndex = intersection.findFirstFreeSlotIndexForAppointment(firstAppointmentIndex, appointmentSlotsCount);
/*
* now check: did we find an index, that is later than the lastAppointmentIndex?
* if yes, this would increase the waiting time, so lets search from startIndex and see,
* whether we find an index firstIndex, that is nearer to firstAppointmentIndex
* than lastIndex to lastAppointmentIndex
*/
if(lastIndex > lastAppointmentIndex || lastIndex == -1) {
int startIndex = Math.max(parent.getSchedule().getBeginIndex(), teacher.getSchedule().getBeginIndex());
int firstDistance;
int lastDistance = lastIndex - lastAppointmentIndex;
firstIndex = intersection.findFirstFreeSlotIndexForAppointment(startIndex, appointmentSlotsCount);
firstDistance = firstAppointmentIndex - firstIndex;
if(firstIndex == -1 && lastIndex == -1) {
//both are invalid => return false
out.println("could not make an appointment for " + parent + " and " + teacher);