Skip to content

Instantly share code, notes, and snippets.

@cesargom
Last active May 12, 2020 16:58
Show Gist options
  • Save cesargom/f7163d6a8da84ed2ffb3b40b534a3955 to your computer and use it in GitHub Desktop.
Save cesargom/f7163d6a8da84ed2ffb3b40b534a3955 to your computer and use it in GitHub Desktop.
interface AccountType {
var balance: Float
val rate: Float
fun calculateInterest(): Float
}
interface InterestCalculator {
fun calculateInterest(balance: Float, rate: Float): Float
}
class InterestCalculatorImpl : InterestCalculator {
override fun calculateInterest(balance: Float, rate: Float): Float {
return balance * rate
}
}
class SecuritiesInterestCalculator : InterestCalculator {
override fun calculateInterest(balance: Float, rate: Float): Float {
return balance * rate * 1.3f
}
}
class Stocks(delegate: AccountType, interestCalculator: InterestCalculator) : AccountType by delegate,
InterestCalculator by interestCalculator
fun main() {
val stocksAccount = Stocks(AccountTypeImpl(100f), SecuritiesInterestCalculator())
println(
with(stocksAccount) {
"The interest of the money market account is ${calculateInterest(
balance,
rate
)}" // Outputs 6.5
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment