Created
August 28, 2019 10:25
-
-
Save Pepijn98/aae87dfb5224bb4ad08adcc6e2b9aa19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Time(var hours: Int, var mins: Int) { | |
operator fun plus(time: Time): Time { | |
val minutes = this.mins + time.mins | |
val hoursInMinutes = minutes / 60 | |
val remainingMinutes = minutes % 60 | |
val hours = this.hours + time.hours + hoursInMinutes | |
return Time(hours, remainingMinutes) | |
} | |
operator fun minus(time: Time): Time { | |
var hoursToRemove = 0 | |
var minutes = this.mins - time.mins | |
if (minutes < 0) { | |
minutes = minutes + 60 | |
hoursToRemove = 1 | |
} | |
var hours = this.hours - time.hours - hoursToRemove | |
if (hours < 0) hours = 0 | |
return Time(hours, minutes) | |
} | |
operator fun inc(): Time { | |
if (this.mins + 1 == 61) { | |
this.hours = this.hours + 2 | |
this.mins = 0 | |
return Time(this.hours, this.mins) | |
} else { | |
this.hours++ | |
this.mins++ | |
return Time(this.hours, this.mins) | |
} | |
} | |
operator fun dec(): Time { | |
if (this.mins - 1 == -1) { | |
this.hours = this.hours - 2 | |
this.mins = 59 | |
return Time(this.hours, this.mins) | |
} else { | |
this.hours-- | |
this.mins-- | |
return Time(this.hours, this.mins) | |
} | |
} | |
} | |
fun main(args: Array<String>) { | |
var newTime = Time(10, 40) + Time(3, 20) | |
println(newTime) // => Time(hours=14, mins=0) | |
println(newTime--) // => Time(hours=12, mins=59) | |
val time = Time(10, 10) - Time(1, 60) | |
println(time) // => Time(hours=8, mins=10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment