From b0b296482bafdea0cddb6856c7855af76cdacc1b Mon Sep 17 00:00:00 2001 From: japygo Date: Fri, 14 Nov 2025 17:04:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20`YukymController`=20=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/YukymController.kt | 29 +++++++------ src/test/kotlin/YukymControllerTest.kt | 60 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 src/test/kotlin/YukymControllerTest.kt diff --git a/src/main/kotlin/YukymController.kt b/src/main/kotlin/YukymController.kt index cd4bba7..3869a36 100644 --- a/src/main/kotlin/YukymController.kt +++ b/src/main/kotlin/YukymController.kt @@ -3,9 +3,11 @@ package com.survivalcoding import java.time.LocalDateTime import java.time.format.DateTimeFormatter -class YukymController { +class YukymController( + private val now: LocalDateTime = LocalDateTime.now() +) { - val nowDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-mm-dd")) + val nowDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) lateinit var nowTime: String @@ -34,18 +36,17 @@ class YukymController { val timeDataOne = _getTimeDataOne(nowDate) var result = timeDataOne.first().ty12 - val nowTime = LocalDateTime.now() when { - nowTime.hour >= 0 || nowTime.hour < 2 -> return timeDataOne.first().ty1 - nowTime.hour >= 4 || nowTime.hour < 6 -> return timeDataOne.first().ty2 - nowTime.hour >= 6 || nowTime.hour < 8 -> return timeDataOne.first().ty3 - nowTime.hour >= 8 || nowTime.hour < 10 -> return timeDataOne.first().ty4 - nowTime.hour >= 10 || nowTime.hour < 12 -> return timeDataOne.first().ty5 - nowTime.hour >= 12 || nowTime.hour < 14 -> return timeDataOne.first().ty6 - nowTime.hour >= 16 || nowTime.hour < 18 -> return timeDataOne.first().ty7 - nowTime.hour >= 18 || nowTime.hour < 20 -> return timeDataOne.first().ty8 - nowTime.hour >= 20 || nowTime.hour < 22 -> return timeDataOne.first().ty9 - nowTime.hour >= 22 || nowTime.hour < 24 -> return timeDataOne.first().ty10 + now.hour >= 0 && now.hour < 2 -> return timeDataOne.first().ty1 + now.hour >= 4 && now.hour < 6 -> return timeDataOne.first().ty2 + now.hour >= 6 && now.hour < 8 -> return timeDataOne.first().ty3 + now.hour >= 8 && now.hour < 10 -> return timeDataOne.first().ty4 + now.hour >= 10 && now.hour < 12 -> return timeDataOne.first().ty5 + now.hour >= 12 && now.hour < 14 -> return timeDataOne.first().ty6 + now.hour >= 16 && now.hour < 18 -> return timeDataOne.first().ty7 + now.hour >= 18 && now.hour < 20 -> return timeDataOne.first().ty8 + now.hour >= 20 && now.hour < 22 -> return timeDataOne.first().ty9 + now.hour >= 22 && now.hour < 24 -> return timeDataOne.first().ty10 } return result @@ -73,4 +74,4 @@ data class YukymTimeModel( val ty10: String = "갑자10국", val ty11: String = "갑자11국", val ty12: String = "갑자12국" -) \ No newline at end of file +) diff --git a/src/test/kotlin/YukymControllerTest.kt b/src/test/kotlin/YukymControllerTest.kt new file mode 100644 index 0000000..90fa9fb --- /dev/null +++ b/src/test/kotlin/YukymControllerTest.kt @@ -0,0 +1,60 @@ +import com.survivalcoding.YukymController +import org.junit.jupiter.api.Assertions.* +import java.time.LocalDateTime +import kotlin.test.Test + +class YukymControllerTest { + @Test + fun `해당 월에 맞는 자시의 국 표시된다`() { + assertMonthRange(1..2, "경오1국") + assertMonthRange(3..4, "경오2국") + assertMonthRange(5..6, "경오3국") + assertMonthRange(7..8, "경오4국") + assertMonthRange(9..10, "경오5국") + assertMonthRange(11..12, "경오6국") + } + + @Test + fun `해당 시간에 맞는 자시의 국 표시된다`() { + assertHourRange(0..<2, "갑자1국") + assertHourRange(2..<4, "갑자12국") + assertHourRange(4..<6, "갑자2국") + assertHourRange(6..<8, "갑자3국") + assertHourRange(8..<10, "갑자4국") + assertHourRange(10..<12, "갑자5국") + assertHourRange(12..<14, "갑자6국") + assertHourRange(14..<16, "갑자12국") + assertHourRange(16..<18, "갑자7국") + assertHourRange(18..<20, "갑자8국") + assertHourRange(20..<22, "갑자9국") + assertHourRange(22..<24, "갑자10국") + } + + private fun assertMonthRange(months: IntRange, expected: String) { + months.forEach { month -> + // given + val now = LocalDateTime.of(2020, month, 2, 2, 22) + val yukymController = YukymController(now) + + // when + val tyA = yukymController.getTyA() + + // then + assertEquals(expected, tyA) + } + } + + private fun assertHourRange(hours: IntRange, expected: String) { + hours.forEach { hour -> + // given + val now = LocalDateTime.of(2020, 2, 2, hour, 22) + val yukymController = YukymController(now) + + // when + val tyB = yukymController.getTyB() + + // then + assertEquals(expected, tyB) + } + } +} From abb5a0007d59525080654bdc1c64c45d87fbb2ad Mon Sep 17 00:00:00 2001 From: japygo Date: Fri, 14 Nov 2025 17:34:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20`YukymController`=20=EB=A6=AC?= =?UTF-8?q?=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/YukymController.kt | 97 ++++++++++-------------------- 1 file changed, 33 insertions(+), 64 deletions(-) diff --git a/src/main/kotlin/YukymController.kt b/src/main/kotlin/YukymController.kt index 3869a36..4aabdfd 100644 --- a/src/main/kotlin/YukymController.kt +++ b/src/main/kotlin/YukymController.kt @@ -1,77 +1,46 @@ package com.survivalcoding import java.time.LocalDateTime -import java.time.format.DateTimeFormatter class YukymController( private val now: LocalDateTime = LocalDateTime.now() ) { - - val nowDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) - - lateinit var nowTime: String - - fun getTyA(): String { - val timeDataOne = _getTimeDataOne(nowDate) - - if (timeDataOne.isNotEmpty()) { - nowTime = timeDataOne.first().ty1 - - val month = nowDate.substring(5, 7) - return when (month) { - "01", "02" -> "경오1국" - "03", "04" -> "경오2국" - "05", "06" -> "경오3국" - "07", "08" -> "경오4국" - "09", "10" -> "경오5국" - "11", "12" -> "경오6국" - else -> nowTime - } - } else { - return "경오7국" - } + fun getTyA(): String = when (now.monthValue) { + 1, 2 -> "경오1국" + 3, 4 -> "경오2국" + 5, 6 -> "경오3국" + 7, 8 -> "경오4국" + 9, 10 -> "경오5국" + 11, 12 -> "경오6국" + else -> TY1 } - fun getTyB(): String { - val timeDataOne = _getTimeDataOne(nowDate) - var result = timeDataOne.first().ty12 - - when { - now.hour >= 0 && now.hour < 2 -> return timeDataOne.first().ty1 - now.hour >= 4 && now.hour < 6 -> return timeDataOne.first().ty2 - now.hour >= 6 && now.hour < 8 -> return timeDataOne.first().ty3 - now.hour >= 8 && now.hour < 10 -> return timeDataOne.first().ty4 - now.hour >= 10 && now.hour < 12 -> return timeDataOne.first().ty5 - now.hour >= 12 && now.hour < 14 -> return timeDataOne.first().ty6 - now.hour >= 16 && now.hour < 18 -> return timeDataOne.first().ty7 - now.hour >= 18 && now.hour < 20 -> return timeDataOne.first().ty8 - now.hour >= 20 && now.hour < 22 -> return timeDataOne.first().ty9 - now.hour >= 22 && now.hour < 24 -> return timeDataOne.first().ty10 - } - - return result + fun getTyB(): String = when (now.hour) { + in 0..<2 -> TY1 + in 4..<6 -> TY2 + in 6..<8 -> TY3 + in 8..<10 -> TY4 + in 10..<12 -> TY5 + in 12..<14 -> TY6 + in 16..<18 -> TY7 + in 18..<20 -> TY8 + in 20..<22 -> TY9 + in 22..<24 -> TY10 + else -> TY12 } - private fun _getTimeDataOne(nowDate: String): List { - val timeDataOne = mutableListOf() - for (i in 0..24) { - timeDataOne.add(YukymTimeModel()) - } - return timeDataOne + companion object { + const val TY1: String = "갑자1국" + const val TY2: String = "갑자2국" + const val TY3: String = "갑자3국" + const val TY4: String = "갑자4국" + const val TY5: String = "갑자5국" + const val TY6: String = "갑자6국" + const val TY7: String = "갑자7국" + const val TY8: String = "갑자8국" + const val TY9: String = "갑자9국" + const val TY10: String = "갑자10국" + const val TY11: String = "갑자11국" + const val TY12: String = "갑자12국" } } - -data class YukymTimeModel( - val ty1: String = "갑자1국", - val ty2: String = "갑자2국", - val ty3: String = "갑자3국", - val ty4: String = "갑자4국", - val ty5: String = "갑자5국", - val ty6: String = "갑자6국", - val ty7: String = "갑자7국", - val ty8: String = "갑자8국", - val ty9: String = "갑자9국", - val ty10: String = "갑자10국", - val ty11: String = "갑자11국", - val ty12: String = "갑자12국" -)