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

View File

@ -2,46 +2,21 @@ package com.baeldung.enums
enum class CardType(val color: String) : ICardLimit { enum class CardType(val color: String) : ICardLimit {
SILVER("gray") { SILVER("gray") {
override fun getCreditLimit(): Int { override fun getCreditLimit() = 100000
return 100000 override fun calculateCashbackPercent() = 0.25f
}
override fun calculateCashbackPercent(): Float {
return 0.25f
}
}, },
GOLD("yellow") { GOLD("yellow") {
override fun getCreditLimit(): Int { override fun getCreditLimit() = 200000
return 200000 override fun calculateCashbackPercent(): Float = 0.5f
}
override fun calculateCashbackPercent(): Float {
return 0.5f
}
}, },
PLATINUM("black") { PLATINUM("black") {
override fun getCreditLimit(): Int { override fun getCreditLimit() = 300000
return 300000 override fun calculateCashbackPercent() = 0.75f
}
override fun calculateCashbackPercent(): Float {
return 0.75f
}
}; };
companion object { companion object {
fun getCardTypeByColor(color: String): CardType? { fun getCardTypeByColor(color: String) = values().firstOrNull { it.color == color }
for (cardType in CardType.values()) { fun getCardTypeByName(name: String) = valueOf(name.toUpperCase())
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