import java.util.Calendar
case class BirthDate(year: Int, month: Int, day: Int)
abstract class Person(val name: String, val surname: String, val birthDate: BirthDate) {
def isAdult: Boolean = {
var result: Boolean = true
val now = Calendar.getInstance()
val currentYear = now.get(Calendar.YEAR)
val currentMonth = now.get(Calendar.MONTH) + 1
val currendDay = now.get(Calendar.DAY_OF_MONTH)
if (currentYear - birthDate.year <= 17) {
result = false
} else if (currentYear - birthDate.year == 18) {
if (currentMonth - birthDate.month < 0) {
result = false
} else if (currentMonth == birthDate.month) {
if (currendDay - birthDate.day < 0) {
result = false
}
}
}
result
}
}
Miałem takie klasy jak wyzej, miałem dopisać metodę sprawdzającą, czy osoba jest pełnoletnia. Nie podoba mi się ta drabinka ifów. Czy da się to zrobić jakoś mądrzej? (modyfikując TYLKO funkcję isAdult)