Skip to content

Commit

Permalink
BAEL-1758 Idiomatic Kotlin usage in enum (eugenp#4434)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored and pedja4 committed Jun 9, 2018
1 parent aaa9202 commit 6dad233
Showing 1 changed file with 8 additions and 33 deletions.
41 changes: 8 additions & 33 deletions core-kotlin/src/main/kotlin/com/baeldung/enums/CardType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,21 @@ 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
}
override fun getCreditLimit() = 100000
override fun calculateCashbackPercent() = 0.25f
},
GOLD("yellow") {
override fun getCreditLimit(): Int {
return 200000
}

override fun calculateCashbackPercent(): Float {
return 0.5f
}
override fun getCreditLimit() = 200000
override fun calculateCashbackPercent(): Float = 0.5f
},
PLATINUM("black") {
override fun getCreditLimit(): Int {
return 300000
}

override fun calculateCashbackPercent(): Float {
return 0.75f
}
override fun getCreditLimit() = 300000
override fun calculateCashbackPercent() = 0.75f
};

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())
}
fun getCardTypeByColor(color: String) = values().firstOrNull { it.color == color }
fun getCardTypeByName(name: String) = valueOf(name.toUpperCase())
}

abstract fun calculateCashbackPercent(): Float
Expand Down

0 comments on commit 6dad233

Please sign in to comment.