From be608ae4fb543fbeea0d210218d00cde75c4b6d4 Mon Sep 17 00:00:00 2001 From: Devesh Chanchlani Date: Wed, 6 Jun 2018 03:20:26 +0400 Subject: [PATCH] BAEL-1758: Working with Enums in Kotlin (#4413) --- .../kotlin/com/baeldung/enums/CardType.kt | 33 ++++++++++++ .../com/baeldung/enums/CardTypeHelper.kt | 16 ++++++ .../kotlin/com/baeldung/enums/ICardLimit.kt | 5 ++ .../baeldung/enums/CardTypeHelperUnitTest.kt | 43 +++++++++++++++ .../com/baeldung/enums/CardTypeUnitTest.kt | 52 +++++++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt create mode 100644 core-kotlin/src/main/kotlin/com/baeldung/enums/ICardLimit.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt create mode 100644 core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt diff --git a/core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt b/core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt new file mode 100644 index 0000000000..ae0c707289 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt @@ -0,0 +1,33 @@ +package com.baeldung.enums + +enum class CardType(val color: String) : ICardLimit { + SILVER("gray") { + override fun getCreditLimit(): Int { + return 100000 + } + + override fun calculateCashbackPercent(): Float { + return 0.25f + } + }, + GOLD("yellow") { + override fun getCreditLimit(): Int { + return 200000 + } + + override fun calculateCashbackPercent(): Float { + return 0.5f + } + }, + PLATINUM("black") { + override fun getCreditLimit(): Int { + return 300000 + } + + override fun calculateCashbackPercent(): Float { + return 0.75f + } + }; + + abstract fun calculateCashbackPercent(): Float +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt b/core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt new file mode 100644 index 0000000000..29982192bb --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/enums/CardTypeHelper.kt @@ -0,0 +1,16 @@ +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()) + } +} \ No newline at end of file diff --git a/core-kotlin/src/main/kotlin/com/baeldung/enums/ICardLimit.kt b/core-kotlin/src/main/kotlin/com/baeldung/enums/ICardLimit.kt new file mode 100644 index 0000000000..7994822a52 --- /dev/null +++ b/core-kotlin/src/main/kotlin/com/baeldung/enums/ICardLimit.kt @@ -0,0 +1,5 @@ +package com.baeldung.enums + +interface ICardLimit { + fun getCreditLimit(): Int +} \ No newline at end of file diff --git a/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt new file mode 100644 index 0000000000..8fcd281784 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeHelperUnitTest.kt @@ -0,0 +1,43 @@ +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")) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt new file mode 100644 index 0000000000..0e74e1cf56 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/enums/CardTypeUnitTest.kt @@ -0,0 +1,52 @@ +package com.baeldung.enums + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +internal class CardTypeUnitTest { + + @Test + fun givenSilverCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() { + assertEquals(0.25f, CardType.SILVER.calculateCashbackPercent()) + } + + @Test + fun givenGoldCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() { + assertEquals(0.5f, CardType.GOLD.calculateCashbackPercent()) + } + + @Test + fun givenPlatinumCardType_whenCalculateCashbackPercent_thenReturnCashbackValue() { + assertEquals(0.75f, CardType.PLATINUM.calculateCashbackPercent()) + } + + @Test + fun givenSilverCardType_whenGetCreditLimit_thenReturnCreditLimit() { + assertEquals(100000, CardType.SILVER.getCreditLimit()) + } + + @Test + fun givenGoldCardType_whenGetCreditLimit_thenReturnCreditLimit() { + assertEquals(200000, CardType.GOLD.getCreditLimit()) + } + + @Test + fun givenPlatinumCardType_whenGetCreditLimit_thenReturnCreditLimit() { + assertEquals(300000, CardType.PLATINUM.getCreditLimit()) + } + + @Test + fun givenSilverCardType_whenCheckColor_thenReturnColor() { + assertEquals("gray", CardType.SILVER.color) + } + + @Test + fun givenGoldCardType_whenCheckColor_thenReturnColor() { + assertEquals("yellow", CardType.GOLD.color) + } + + @Test + fun givenPlatinumCardType_whenCheckColor_thenReturnColor() { + assertEquals("black", CardType.PLATINUM.color) + } +}