Last active
March 14, 2023 13:14
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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