Sha256: aed39a23519428ef36f8b3033ffb2b97c70f623023f0491076addd5f776e6c5b

Contents?: true

Size: 1.31 KB

Versions: 181

Compression:

Stored size: 1.31 KB

Contents

import java.time.DayOfWeek
import java.time.LocalDate

class Meetup(monthOfYear: Int, year: Int) {

    private val startOfMonth: LocalDate = LocalDate.of(year, monthOfYear, 1)

    fun day(dayOfWeek: DayOfWeek, schedule: MeetupSchedule): LocalDate {
        var result = cycleToNext(dayOfWeek, startOfMonth)

        @Suppress("NON_EXHAUSTIVE_WHEN")
        when (schedule) {
            MeetupSchedule.SECOND -> result = result.plusWeeks(1)
            MeetupSchedule.THIRD  -> result = result.plusWeeks(2)
            MeetupSchedule.FOURTH -> result = result.plusWeeks(3)
            MeetupSchedule.LAST   -> result = cycleToPrev(dayOfWeek, startOfMonth.plusMonths(1).minusDays(1))
            MeetupSchedule.TEENTH -> {
                while (result.dayOfMonth < 13) result = result.plusWeeks(1)
            }
        }

        return result
    }

    private fun cycleToPrev(dayOfWeek: DayOfWeek, current: LocalDate): LocalDate {
        var result = current

        while (result.dayOfWeek != dayOfWeek) {
            result = result.minusDays(1)
        }

        return result
    }

    private fun cycleToNext(dayOfWeek: DayOfWeek, current: LocalDate): LocalDate {
        var result = current

        while (result.dayOfWeek != dayOfWeek) {
            result = result.plusDays(1)
        }

        return result
    }

}

Version data entries

181 entries across 181 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.97 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.96 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.95 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.94 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.93 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.92 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.91 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.90 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.89 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.88 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.87 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.86 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.85 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.84 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.83 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.82 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.81 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.80 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt
trackler-2.2.1.79 tracks/kotlin/exercises/meetup/.meta/src/reference/kotlin/Meetup.kt