// BankPolymorphic.kt Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2020-10-28 File created. /* This program is an example about inheritance. - class BankAccount has two subclasses named AccountWithCredit and RestrictedAccount - method withdrawMoney() is overridden in both subclasses - a subclass method calls the corresponding superclass method */ open class BankAccount( var account_owner : String, var account_number : Int, var account_balance : Double ) { fun show_account_data() { print( "\nB A N K A C C O U N T D A T A : " ) print( "\n Account owner : " + account_owner ) print( String.format( "\n Account number: %d", account_number ) ) print( String.format( "\n Current balance: %.2f\n", account_balance ) ) } fun deposit_money( amount_to_deposit : Double ) { print( String.format( "\n\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)", account_owner, account_number ) ) print( String.format( "\n Amount deposited: %.2f", amount_to_deposit )) print( String.format( "\n Old account balance: %.2f", account_balance )) account_balance = account_balance + amount_to_deposit print( String.format( " New balance: %.2f\n", account_balance )) } open fun withdraw_money( amount_to_withdraw : Double ) { print( String.format( "\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)", account_owner, account_number ) ) if ( account_balance < amount_to_withdraw ) { print( "\n -- Transaction not completed: " + String.format( "Not enough money to withdraw %.2f\n", amount_to_withdraw ) ) } else { print( String.format( "\n Amount withdrawn %.2f", amount_to_withdraw )) print( String.format( " Old account balance: %.2f", account_balance )) account_balance = account_balance - amount_to_withdraw print( String.format( " New balance: %.2f\n", account_balance )) } } fun transfer_money_to( receiving_account : BankAccount, amount_to_transfer : Double ) { print( String.format( "\n\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)", account_owner, account_number ) ) if ( account_balance >= amount_to_transfer ) { receiving_account.account_balance = receiving_account.account_balance + amount_to_transfer print( String.format( "\n %.2f was transferred to %s (Account no. %d).", amount_to_transfer, receiving_account.account_owner, receiving_account.account_number ) ) print( String.format( "\n Balance before transfer: %.2f", account_balance ) ) account_balance = account_balance - amount_to_transfer print( String.format( "\n New balance: %.2f\n", account_balance )) } else { print( "\n -- Not enough money for transfer.\n" ) } } } class AccountWithCredit( account_owner : String, account_number : Int, account_balance : Double, var credit_limit : Double ) : BankAccount( account_owner, account_number, account_balance ) { override fun withdraw_money( amount_to_withdraw : Double ) { print( String.format( "\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)", account_owner, account_number ) ) if ( account_balance + credit_limit < amount_to_withdraw ) { print( " -- Transaction not completed: " + String.format( "Not enough credit to withdraw %.2f\n", amount_to_withdraw ) ) } else { print( String.format( "\n Amount withdrawn %.2f", amount_to_withdraw )) print( String.format( "\n Old account balance: %.2f", account_balance ) ) account_balance = account_balance - amount_to_withdraw print( String.format( " New balance: %.2f\n", account_balance )) } } } class RestrictedAccount( account_owner : String, account_number : Int, account_balance : Double, var maximum_amount_to_withdraw : Double ) : BankAccount( account_owner, account_number, account_balance ) { override fun withdraw_money( amount_to_withdraw : Double ) { if ( amount_to_withdraw > maximum_amount_to_withdraw ) { print( String.format( "\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)", account_owner, account_number ) ) print( "\n -- Transaction not completed: " ) print( String.format( " Cannot withdraw %.2f. Withrawal limit %.2f\n", amount_to_withdraw, maximum_amount_to_withdraw ) ) } else { // Withdrawal is possible. // Here we call the corresponding method in superclass super.withdraw_money( amount_to_withdraw ) } } } fun main() { var beatles_account = BankAccount( "John Lennon", 222222, 2000.00 ) var stones_account = AccountWithCredit( "Brian Jones", 333333, 2000.00, 1000.00 ) var doors_account = RestrictedAccount( "Jim Morrison", 444444, 4000.00, 1000.00 ) beatles_account.withdraw_money( 2500.00 ) stones_account.withdraw_money( 2500.00 ) doors_account.withdraw_money( 2500.00 ) print( "\n" ) }