BAEL-1758: removing helper class and adding static functions to the Kotlin Enum example (#4419)
This commit is contained in:
parent
d389000afd
commit
8080f07c6c
|
@ -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
|
abstract fun calculateCashbackPercent(): Float
|
||||||
}
|
}
|
|
@ -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())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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"))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.baeldung.enums
|
package com.baeldung.enums
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
@ -49,4 +50,35 @@ internal class CardTypeUnitTest {
|
||||||
fun givenPlatinumCardType_whenCheckColor_thenReturnColor() {
|
fun givenPlatinumCardType_whenCheckColor_thenReturnColor() {
|
||||||
assertEquals("black", CardType.PLATINUM.color)
|
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"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue