diff --git a/core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt b/core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt index ae0c707289..013c8070bf 100644 --- a/core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt +++ b/core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt @@ -29,5 +29,20 @@ enum class CardType(val color: String) : ICardLimit { } }; + companion object { + fun getCardTypeByColor(color: String): CardType? { + for (cardType in CardType.values()) { + if (cardType.color.equals(color)) { + return cardType; + } + } + return null + } + + fun getCardTypeByName(name: String): CardType { + return CardType.valueOf(name.toUpperCase()) + } + } + abstract fun calculateCashbackPercent(): Float } \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt b/core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt deleted file mode 100644 index 29982192bb..0000000000 --- a/core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.enums - -class CardTypeHelper { - fun getCardTypeByColor(color: String): CardType? { - for (cardType in CardType.values()) { - if (cardType.color.equals(color)) { - return cardType; - } - } - return null - } - - fun getCardTypeByName(name: String): CardType { - return CardType.valueOf(name.toUpperCase()) - } -} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt deleted file mode 100644 index 8fcd281784..0000000000 --- a/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.enums - -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Test - -internal class CardTypeHelperUnitTest { - - @Test - fun whenGetCardTypeByColor_thenSilverCardType() { - val cardTypeHelper = CardTypeHelper() - Assertions.assertEquals(CardType.SILVER, cardTypeHelper.getCardTypeByColor("gray")) - } - - @Test - fun whenGetCardTypeByColor_thenGoldCardType() { - val cardTypeHelper = CardTypeHelper() - Assertions.assertEquals(CardType.GOLD, cardTypeHelper.getCardTypeByColor("yellow")) - } - - @Test - fun whenGetCardTypeByColor_thenPlatinumCardType() { - val cardTypeHelper = CardTypeHelper() - Assertions.assertEquals(CardType.PLATINUM, cardTypeHelper.getCardTypeByColor("black")) - } - - @Test - fun whenGetCardTypeByName_thenSilverCardType() { - val cardTypeHelper = CardTypeHelper() - Assertions.assertEquals(CardType.SILVER, cardTypeHelper.getCardTypeByName("silver")) - } - - @Test - fun whenGetCardTypeByName_thenGoldCardType() { - val cardTypeHelper = CardTypeHelper() - Assertions.assertEquals(CardType.GOLD, cardTypeHelper.getCardTypeByName("gold")) - } - - @Test - fun whenGetCardTypeByName_thenPlatinumCardType() { - val cardTypeHelper = CardTypeHelper() - Assertions.assertEquals(CardType.PLATINUM, cardTypeHelper.getCardTypeByName("platinum")) - } -} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt index 0e74e1cf56..525faebd55 100644 --- a/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt +++ b/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt @@ -1,5 +1,6 @@ package com.baeldung.enums +import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test @@ -49,4 +50,35 @@ internal class CardTypeUnitTest { fun givenPlatinumCardType_whenCheckColor_thenReturnColor() { assertEquals("black", CardType.PLATINUM.color) } + + @Test + fun whenGetCardTypeByColor_thenSilverCardType() { + Assertions.assertEquals(CardType.SILVER, CardType.getCardTypeByColor("gray")) + } + + @Test + fun whenGetCardTypeByColor_thenGoldCardType() { + Assertions.assertEquals(CardType.GOLD, CardType.getCardTypeByColor("yellow")) + } + + @Test + fun whenGetCardTypeByColor_thenPlatinumCardType() { + Assertions.assertEquals(CardType.PLATINUM, CardType.getCardTypeByColor("black")) + } + + @Test + fun whenGetCardTypeByName_thenSilverCardType() { + Assertions.assertEquals(CardType.SILVER, CardType.getCardTypeByName("silver")) + } + + @Test + fun whenGetCardTypeByName_thenGoldCardType() { + Assertions.assertEquals(CardType.GOLD, CardType.getCardTypeByName("gold")) + } + + @Test + fun whenGetCardTypeByName_thenPlatinumCardType() { + Assertions.assertEquals(CardType.PLATINUM, CardType.getCardTypeByName("platinum")) + } + }