I'm new in Kotlin and I need to create 2 date ranges. One that shows the dates as they are created in the constructor and another that modifies the end date with a default date in case it is null or greater than the default date. For this I had thought to create an abstract class with a start and end date and then create 2 data classes to implement this abstract class and put this business rule in one of them. This is the code I have so far:
abstract class BaseLocalDateRange (
open val startDate: LocalDate?,
open val endDate: LocalDate?
)
data class LocalDateRange(
override val startDate: LocalDate?,
override val endDate: LocalDate?
): BaseLocalDateRange(
startDate,
endDate
)
data class LocalDateRangeEndDate(
override val startDate: LocalDate?,
override val endDate: LocalDate?
): BaseLocalDateRange(
startDate,
if (endDate== null || endDate > LocalDate.of(2099, 12, 31)) LocalDate.of(2099, 12, 31) else endDate
)
My questions are:
- Is this ok or there is a better way to approach this problem?
- I would like to have the "default date" in properties and use it here, but I don't really know how, so if someone has an idea of how to handle this it would be great.
Thank you!