Skip to content

Instantly share code, notes, and snippets.

@malihehmoradi
Last active March 14, 2023 13:14
Show Gist options
  • Save malihehmoradi/c5a6671427fc796f3cca9bc9fbc34d69 to your computer and use it in GitHub Desktop.
Save malihehmoradi/c5a6671427fc796f3cca9bc9fbc34d69 to your computer and use it in GitHub Desktop.
Create an enum class for defining two constants with specific name and value.
import java.text.NumberFormat
import java.util.*
enum class PaymentMethod(val balance: Double) {
CASH(14.02) {
override fun printFormattedAmount(): String {
return NumberFormat.getCurrencyInstance(Locale.US).format(balance)
}
},
CREDIT(142.03) {
override fun printFormattedAmount(): String {
return "$balance"
}
};
abstract fun printFormattedAmount(): String
}
// Test the enum class here
fun main(array: Array<String>) {
PaymentMethod.values().forEach {
println("${it.balance} .... ${it.printFormattedAmount()}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment