Skip to content

Instantly share code, notes, and snippets.

/Appointments.swift
Created Jun 27, 2017

Embed
What would you like to do?
// Put this in a Swift playground
import Foundation
class Appointment {
var start: Date
var end: Date
init(start: Date, end: Date) {
// Ensure that start is always before end
if start <= end {
self.start = start
self.end = end
} else {
self.end = start
self.start = end
}
}
func conflicts(with other: Appointment) -> Bool {
var appt1: Appointment
var appt2: Appointment
// Ensure that appt1 starts before or at the same time as appt2
if self.start <= other.start {
appt1 = self
appt2 = other
} else {
appt1 = other
appt2 = self
}
if appt1.end > appt2.start {
return true
}
return false
}
var description: String {
return "\(self.start, self.end)"
}
}
let appt1 = Appointment(start: 2, end: 2);
appt1.description
let appt2 = Appointment(start: 2, end: 4);
appt2.description
print(appt1.conflicts(with: appt2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.