BAEL-1758: removing helper class and adding static functions to the Kotlin Enum example (#4419)

This commit is contained in:
Devesh Chanchlani 2018-06-07 10:18:48 +04:00 committed by Predrag Maric
parent d389000afd
commit 8080f07c6c
4 changed files with 47 additions and 59 deletions

View File

@ -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
}

View File

@ -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())
}
}

View File

@ -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"))
}
}

View File

@ -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"))
}
}