BAEL-1758: Working with Enums in Kotlin (#4413)
This commit is contained in:
committed by
Predrag Maric
parent
5c0004c746
commit
be608ae4fb
@@ -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
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.enums
|
||||
|
||||
interface ICardLimit {
|
||||
fun getCreditLimit(): Int
|
||||
}
|
||||
Reference in New Issue
Block a user