# ‘Library Fine’ problem in Hackerrank – solution in Scala

Problem Statement:

The Head Librarian at a library wants you to make a program that calculates the fine for returning the book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:

If the book is returned on or before the expected return date, no fine will be charged, in other words fine is 0.

If the book is returned in the same month as the expected return date, Fine = 15 Hackos × Number of late days

If the book is not returned in the same month but in the same year as the expected return date, Fine = 500 Hackos × Number of late months

If the book is not returned in the same year, the fine is fixed at 10000 Hackos.

Refer the below link to know more about this problem. https://www.hackerrank.com/challenges/library-fine

Solution in Scala

``````
import java.util.{Calendar, Scanner}

object Solution {

def main(args: Array[String]) {
val scanner: Scanner = new Scanner(System.in)

//Actual Returned Date
val aDate: Int = scanner.nextInt
val aMonth: Int = scanner.nextInt
val aYear: Int = scanner.nextInt

//Due Date
val dDate: Int = scanner.nextInt
val dMonth: Int = scanner.nextInt
val dYear: Int = scanner.nextInt

val isValidData: Boolean = ((aDate >= 1 && aDate = 1 && dDate = 1 && aMonth = 1 && dMonth = 1 && aYear = 1 && dYear <= 3000))

var fineAmount: Int = 0
if (isValidData) {
val actualCalendar: Calendar = Calendar.getInstance()
actualCalendar.set(aYear, aMonth, aDate)

val dCalendar: Calendar = Calendar.getInstance()
dCalendar.set(dYear, dMonth, dDate)

if ((actualCalendar.getTime == dCalendar.getTime) || actualCalendar.getTime.before(dCalendar.getTime)) {
fineAmount = 0
}
else if (actualCalendar.getTime.after(dCalendar.getTime) && aYear == dYear) {
fineAmount = if ((aMonth == dMonth)) 15 * (aDate - dDate) else 500 * (aMonth - dMonth)
}
else {
fineAmount = 10000
}
}
println(fineAmount)
}
}
```
```
Advertisements