Last active
May 12, 2020 16:58
-
-
Save cesargom/f7163d6a8da84ed2ffb3b40b534a3955 to your computer and use it in GitHub Desktop.
This file contains 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
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