Skip to content

Instantly share code, notes, and snippets.

@rocketpages
Created July 21, 2013 23:07
Show Gist options
  • Select an option

  • Save rocketpages/6050354 to your computer and use it in GitHub Desktop.

Select an option

Save rocketpages/6050354 to your computer and use it in GitHub Desktop.
A crude appointment scheduling conflict finder
def getAvailabilities(date: Date, length: String): Seq[AvailableTime] = {
val at = AvailableTime(None, new Date(System.currentTimeMillis()), "10", "30")
val at2 = AvailableTime(None, new Date(System.currentTimeMillis()), "10", "45")
val at3 = AvailableTime(None, new Date(System.currentTimeMillis()), "11", "15")
val at4 = AvailableTime(None, new Date(System.currentTimeMillis()), "14", "30")
val at5 = AvailableTime(None, new Date(System.currentTimeMillis()), "15", "45")
val at6 = AvailableTime(None, new Date(System.currentTimeMillis()), "16", "15")
val at7 = AvailableTime(None, new Date(System.currentTimeMillis()), "17", "30")
val times = Seq(at, at2, at3, at4, at5, at6, at7)
val appt = Appointment(None, new Date(), "11", "45", 60, "housecall", 1)
val appt2 = Appointment(None, new Date(), "19", "00", 45, "housecall", 1)
val appts = Some(Seq(appt, appt2))
times.filter(time => isTimeAvailable(time, appts))
}
private def isTimeAvailable(availableTime: AvailableTime, maybeAppointments: Option[Seq[Appointment]]) = {
maybeAppointments match {
case Some(appointments) => {
val conflict = appointments.exists(appt => {
isDateOverlap(appt.startDate, appt.endDate, availableTime.startDate, availableTime.endDate)
})
!conflict
}
case None => true
}
}
private def isDateOverlap(startDate1: DateTime, endDate1: DateTime, startDate2: DateTime, endDate2: DateTime) = {
(startDate1 <= endDate2) && (startDate2 <= endDate1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment