-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
76 additions
and
41 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/org/javafreedom/khol/BaseCalculationAlgorithm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.javafreedom.khol | ||
|
||
import kotlinx.datetime.LocalDate | ||
|
||
fun interface BaseCalculationAlgorithm { | ||
|
||
fun calculateBaseDate(year: Int): LocalDate | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/org/javafreedom/khol/algorithm/FirstAdvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@file:Suppress("detekt:style:MagicNumber") | ||
package org.javafreedom.khol.algorithm | ||
|
||
import kotlinx.datetime.DayOfWeek | ||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.LocalDate | ||
import kotlinx.datetime.plus | ||
import org.javafreedom.khol.BaseCalculationAlgorithm | ||
|
||
class FirstAdvent : BaseCalculationAlgorithm { | ||
|
||
override fun calculateBaseDate(year: Int): LocalDate { | ||
// Find November 30th of the given year | ||
val november30 = LocalDate(year, 11, 30) | ||
// Get the day of the week for November 30th | ||
val dayOfWeek = november30.dayOfWeek | ||
// Calculate how many days to add to get to the nearest Sunday (Advent starts) | ||
val daysToSunday = (DayOfWeek.SUNDAY.value - dayOfWeek.value + 7) % 7 | ||
// The first Advent Sunday is the Sunday closest to or on November 30th | ||
return november30.plus(daysToSunday, DateTimeUnit.DAY) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/test/kotlin/org/javafreedom/khol/algorithm/FirstAdventTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.javafreedom.khol.algorithm | ||
|
||
import kotlinx.datetime.number | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class FirstAdventTest { | ||
|
||
private val firstAdvent = FirstAdvent() | ||
|
||
@Test | ||
fun testFirstAdvent() { | ||
val result0 = firstAdvent.calculateBaseDate(2024) | ||
assertEquals(2024, result0.year) | ||
assertEquals(12, result0.month.number) | ||
assertEquals(1, result0.dayOfMonth) | ||
|
||
val result1 = firstAdvent.calculateBaseDate(2023) | ||
assertEquals(2023, result1.year) | ||
assertEquals(12, result1.month.number) | ||
assertEquals(3, result1.dayOfMonth) | ||
} | ||
|
||
} |
26 changes: 7 additions & 19 deletions
26
...vafreedom/khol/HolidayCalculusUtilTest.kt → ...gorithm/GregorianEasterSundayGaussTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,32 @@ | ||
package org.javafreedom.khol | ||
package org.javafreedom.khol.algorithm | ||
|
||
import kotlinx.datetime.number | ||
import org.javafreedom.khol.HolidayCalculusUtil.firstAdvent | ||
import org.javafreedom.khol.HolidayCalculusUtil.gregorianEasterSunday | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
private data class EasterCalculusArguments(val year: Int, val month: Int, val day: Int) | ||
|
||
class HolidayCalculusUtilTest { | ||
class GregorianEasterSundayGaussTest { | ||
|
||
private val argumentsList = mutableListOf( | ||
EasterCalculusArguments(2008, 3, 23), | ||
EasterCalculusArguments(2015, 4, 5), | ||
EasterCalculusArguments(2017, 4, 16), | ||
EasterCalculusArguments(2018, 4, 1), | ||
EasterCalculusArguments(2019, 4, 21), | ||
EasterCalculusArguments(2027, 3, 28)) | ||
EasterCalculusArguments(2027, 3, 28) | ||
) | ||
|
||
private val sut = GregorianEasterSundayGauss() | ||
|
||
@Test | ||
fun testEasterSunday() { | ||
argumentsList.forEach { | ||
val result = gregorianEasterSunday(it.year) | ||
val result = sut.calculateBaseDate(it.year) | ||
assertEquals(it.year, result.year) | ||
assertEquals(it.month, result.month.number) | ||
assertEquals(it.day, result.dayOfMonth) | ||
} | ||
} | ||
|
||
@Test | ||
fun testFirstAdvent() { | ||
val result0 = firstAdvent(2024) | ||
assertEquals(2024, result0.year) | ||
assertEquals(12, result0.month.number) | ||
assertEquals(1, result0.dayOfMonth) | ||
|
||
val result1 = firstAdvent(2023) | ||
assertEquals(2023, result1.year) | ||
assertEquals(12, result1.month.number) | ||
assertEquals(3, result1.dayOfMonth) | ||
} | ||
|
||
} |