BAEL-1758 Idiomatic Kotlin usage in enum (#4434)

This commit is contained in:
Grzegorz Piwowarek 2018-06-09 11:29:35 +02:00 committed by Predrag Maric
parent aaa9202883
commit 6dad233602
1 changed files with 8 additions and 33 deletions

View File

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