Skip to content

Commit be608ae

Browse files
deveshchanchlanipedja4
authored andcommitted
BAEL-1758: Working with Enums in Kotlin (eugenp#4413)
1 parent 5c0004c commit be608ae

5 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.enums
2+
3+
enum class CardType(val color: String) : ICardLimit {
4+
SILVER("gray") {
5+
override fun getCreditLimit(): Int {
6+
return 100000
7+
}
8+
9+
override fun calculateCashbackPercent(): Float {
10+
return 0.25f
11+
}
12+
},
13+
GOLD("yellow") {
14+
override fun getCreditLimit(): Int {
15+
return 200000
16+
}
17+
18+
override fun calculateCashbackPercent(): Float {
19+
return 0.5f
20+
}
21+
},
22+
PLATINUM("black") {
23+
override fun getCreditLimit(): Int {
24+
return 300000
25+
}
26+
27+
override fun calculateCashbackPercent(): Float {
28+
return 0.75f
29+
}
30+
};
31+
32+
abstract fun calculateCashbackPercent(): Float
33+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.enums
2+
3+
class CardTypeHelper {
4+
fun getCardTypeByColor(color: String): CardType? {
5+
for (cardType in CardType.values()) {
6+
if (cardType.color.equals(color)) {
7+
return cardType;
8+
}
9+
}
10+
return null
11+
}
12+
13+
fun getCardTypeByName(name: String): CardType {
14+
return CardType.valueOf(name.toUpperCase())
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.enums
2+
3+
interface ICardLimit {
4+
fun getCreditLimit(): Int
5+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.enums
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Test
5+
6+
internal class CardTypeHelperUnitTest {
7+
8+
@Test
9+
fun whenGetCardTypeByColor_thenSilverCardType() {
10+
val cardTypeHelper = CardTypeHelper()
11+
Assertions.assertEquals(CardType.SILVER, cardTypeHelper.getCardTypeByColor("gray"))
12+
}
13+
14+
@Test
15+
fun whenGetCardTypeByColor_thenGoldCardType() {
16+
val cardTypeHelper = CardTypeHelper()
17+
Assertions.assertEquals(CardType.GOLD, cardTypeHelper.getCardTypeByColor("yellow"))
18+
}
19+
20+
@Test
21+
fun whenGetCardTypeByColor_thenPlatinumCardType() {
22+
val cardTypeHelper = CardTypeHelper()
23+
Assertions.assertEquals(CardType.PLATINUM, cardTypeHelper.getCardTypeByColor("black"))
24+
}
25+
26+
@Test
27+
fun whenGetCardTypeByName_thenSilverCardType() {
28+
val cardTypeHelper = CardTypeHelper()
29+
Assertions.assertEquals(CardType.SILVER, cardTypeHelper.getCardTypeByName("silver"))
30+
}
31+
32+
@Test
33+
fun whenGetCardTypeByName_thenGoldCardType() {
34+
val cardTypeHelper = CardTypeHelper()
35+
Assertions.assertEquals(CardType.GOLD, cardTypeHelper.getCardTypeByName("gold"))
36+
}
37+
38+
@Test
39+
fun whenGetCardTypeByName_thenPlatinumCardType() {
40+
val cardTypeHelper = CardTypeHelper()
41+
Assertions.assertEquals(CardType.PLATINUM, cardTypeHelper.getCardTypeByName("platinum"))
42+
}
43+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.enums
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
internal class CardTypeUnitTest {
7+
8+
@Test
9+
fun givenSilverCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() {
10+
assertEquals(0.25f, CardType.SILVER.calculateCashbackPercent())
11+
}
12+
13+
@Test
14+
fun givenGoldCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() {
15+
assertEquals(0.5f, CardType.GOLD.calculateCashbackPercent())
16+
}
17+
18+
@Test
19+
fun givenPlatinumCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() {
20+
assertEquals(0.75f, CardType.PLATINUM.calculateCashbackPercent())
21+
}
22+
23+
@Test
24+
fun givenSilverCardType_whenGetCreditLimit_thenReturnCreditLimit() {
25+
assertEquals(100000, CardType.SILVER.getCreditLimit())
26+
}
27+
28+
@Test
29+
fun givenGoldCardType_whenGetCreditLimit_thenReturnCreditLimit() {
30+
assertEquals(200000, CardType.GOLD.getCreditLimit())
31+
}
32+
33+
@Test
34+
fun givenPlatinumCardType_whenGetCreditLimit_thenReturnCreditLimit() {
35+
assertEquals(300000, CardType.PLATINUM.getCreditLimit())
36+
}
37+
38+
@Test
39+
fun givenSilverCardType_whenCheckColor_thenReturnColor() {
40+
assertEquals("gray", CardType.SILVER.color)
41+
}
42+
43+
@Test
44+
fun givenGoldCardType_whenCheckColor_thenReturnColor() {
45+
assertEquals("yellow", CardType.GOLD.color)
46+
}
47+
48+
@Test
49+
fun givenPlatinumCardType_whenCheckColor_thenReturnColor() {
50+
assertEquals("black", CardType.PLATINUM.color)
51+
}
52+
}

0 commit comments

Comments
 (0)