File tree Expand file tree Collapse file tree
main/kotlin/com/baeldung/enums
test/kotlin/com/baeldung/enums Expand file tree Collapse file tree Original file line number Diff line number Diff 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}
Load diff This file was deleted.
Load diff This file was deleted.
Original file line number Diff line number Diff line change 11package com.baeldung.enums
22
3+ import org.junit.jupiter.api.Assertions
34import org.junit.jupiter.api.Assertions.assertEquals
45import 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}
You can’t perform that action at this time.
0 commit comments