Skip to content

Instantly share code, notes, and snippets.

@CaseyLeask
Created October 29, 2016 12:19
Show Gist options
  • Save CaseyLeask/cb53786b99ac603ab1370ad8ed64ebad to your computer and use it in GitHub Desktop.
Save CaseyLeask/cb53786b99ac603ab1370ad8ed64ebad to your computer and use it in GitHub Desktop.
import scala.io.StdIn.{readLine}
import scala.io.Source
import java.util.{GregorianCalendar, Calendar}
object Solution {
def main(args: Array[String]) {
val actualCalendar = createCalendar(readLine())
val expectedCalendar = createCalendar(readLine())
val (diffYear, diffMonth, diffDay) = calculateDateDiff(actualCalendar, expectedCalendar)
println(calculateFine(diffYear, diffMonth, diffDay))
}
def createCalendar(input: String): GregorianCalendar = {
val format = new java.text.SimpleDateFormat("dd MM yyyy")
val date = format.parse(input)
val calendar = new GregorianCalendar()
calendar.setTime(date)
calendar
}
def calculateDateDiff(actualCalendar: Calendar, expectedCalendar: Calendar): (Int, Int, Int) = {
val diffYear = actualCalendar.get(Calendar.YEAR) - expectedCalendar.get(Calendar.YEAR)
val diffMonth = actualCalendar.get(Calendar.MONTH) - expectedCalendar.get(Calendar.MONTH)
val diffDay = actualCalendar.get(Calendar.DAY_OF_MONTH) - expectedCalendar.get(Calendar.DAY_OF_MONTH)
(diffYear, diffMonth, diffDay)
}
def calculateFine(diffYear: Int, diffMonth: Int, diffDay: Int): Int = {
if (diffYear > 0) {
10000
} else if (diffYear < 0) {
0
} else if (diffMonth > 0) {
500 * diffMonth
} else if (diffMonth < 0) {
0
} else if (diffDay > 0) {
15 * diffDay
} else {
0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment