Skip to content

Commit 8080f07

Browse files
deveshchanchlanipedja4
authored andcommitted
BAEL-1758: removing helper class and adding static functions to the Kotlin Enum example (eugenp#4419)
1 parent d389000 commit 8080f07

4 files changed

Lines changed: 47 additions & 59 deletions

File tree

‎core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,20 @@ enum class CardType(val color: String) : ICardLimit {
2929
}
3030
};
3131

32+
companion object {
33+
fun getCardTypeByColor(color: String): CardType? {
34+
for (cardType in CardType.values()) {
35+
if (cardType.color.equals(color)) {
36+
return cardType;
37+
}
38+
}
39+
return null
40+
}
41+
42+
fun getCardTypeByName(name: String): CardType {
43+
return CardType.valueOf(name.toUpperCase())
44+
}
45+
}
46+
3247
abstract fun calculateCashbackPercent(): Float
3348
}

‎core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt‎

Lines changed: 0 additions & 43 deletions
This file was deleted.

‎core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.enums
22

3+
import org.junit.jupiter.api.Assertions
34
import org.junit.jupiter.api.Assertions.assertEquals
45
import org.junit.jupiter.api.Test
56

@@ -49,4 +50,35 @@ internal class CardTypeUnitTest {
4950
fun givenPlatinumCardType_whenCheckColor_thenReturnColor() {
5051
assertEquals("black", CardType.PLATINUM.color)
5152
}
53+
54+
@Test
55+
fun whenGetCardTypeByColor_thenSilverCardType() {
56+
Assertions.assertEquals(CardType.SILVER, CardType.getCardTypeByColor("gray"))
57+
}
58+
59+
@Test
60+
fun whenGetCardTypeByColor_thenGoldCardType() {
61+
Assertions.assertEquals(CardType.GOLD, CardType.getCardTypeByColor("yellow"))
62+
}
63+
64+
@Test
65+
fun whenGetCardTypeByColor_thenPlatinumCardType() {
66+
Assertions.assertEquals(CardType.PLATINUM, CardType.getCardTypeByColor("black"))
67+
}
68+
69+
@Test
70+
fun whenGetCardTypeByName_thenSilverCardType() {
71+
Assertions.assertEquals(CardType.SILVER, CardType.getCardTypeByName("silver"))
72+
}
73+
74+
@Test
75+
fun whenGetCardTypeByName_thenGoldCardType() {
76+
Assertions.assertEquals(CardType.GOLD, CardType.getCardTypeByName("gold"))
77+
}
78+
79+
@Test
80+
fun whenGetCardTypeByName_thenPlatinumCardType() {
81+
Assertions.assertEquals(CardType.PLATINUM, CardType.getCardTypeByName("platinum"))
82+
}
83+
5284
}

0 commit comments

Comments
 (0)