// BankPolymorphic.swift Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2014-10-09 File created. // 2016-11-07 Last modification. /* 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 */ import Foundation class BankAccount { var account_owner : String var account_number : Int var account_balance : Double init( _ given_account_owner : String, _ given_account_number : Int, _ initial_balance : Double ) { account_owner = given_account_owner account_number = given_account_number account_balance = initial_balance } func show_account_data() { print( "\nB A N K A C C O U N T D A T A : " ) print( " Account owner : " + account_owner ) print( String( format: " Account number: %d", account_number ) ) print( String( format: " Current balance: %.2f\n", account_balance ) ) } func deposit_money( _ amount_to_deposit : Double ) { print( String( format: "\n\nTRANSACTION FOR ACCOUNT OF %@ (Account number %d)", account_owner, account_number ) ) print( String( format: "\n Amount deposited: %.2f", amount_to_deposit )) print( String( format: " Old account balance: %.2f", account_balance )) account_balance = account_balance + amount_to_deposit print( String( format: " New balance: %.2f", account_balance )) } func withdraw_money( _ amount_to_withdraw : Double ) { print( String( format: "\nTRANSACTION FOR ACCOUNT OF %@ (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", 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", account_balance )) } } func transfer_money_to( _ receiving_account : BankAccount, _ amount_to_transfer : Double ) { print( String( format: "\n\nTRANSACTION FOR ACCOUNT OF %@ (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 %@ (Account no. %d).", amount_to_transfer, receiving_account.account_owner, receiving_account.account_number ) ) print( String( format: " Balance before transfer: %.2f", account_balance ), terminator: "" ) account_balance = account_balance - amount_to_transfer print( String( format: " New balance: %.2f", account_balance )) } else { print( "\n -- Not enough money for transfer.\n" ) } } } class AccountWithCredit : BankAccount { var credit_limit : Double = 0.0 init( _ given_account_owner : String, _ given_account_number : Int, _ initial_balance : Double, _ given_credit_limit : Double ) { super.init( given_account_owner, given_account_number, initial_balance ) credit_limit = given_credit_limit } // We do not need a convenience initializer in this class when // the new data field credit_limit has a default value. override func withdraw_money( _ amount_to_withdraw : Double ) { print( String( format: "\nTRANSACTION FOR ACCOUNT OF %@ (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", amount_to_withdraw ) ) } else { print( String( format: "\n Amount withdrawn %.2f", amount_to_withdraw )) print( String( format: " Old account balance: %.2f", account_balance ), terminator: "" ) account_balance = account_balance - amount_to_withdraw print( String( format: " New balance: %.2f", account_balance )) } } } class RestrictedAccount : BankAccount { var maximum_amount_to_withdraw : Double = 3000.0 init( _ given_account_owner : String, _ given_account_number : Int, _ initial_balance : Double, _ given_withdrawal_limit : Double ) { super.init( given_account_owner, given_account_number, initial_balance ) maximum_amount_to_withdraw = given_withdrawal_limit } override func withdraw_money( _ amount_to_withdraw : Double ) { if amount_to_withdraw > maximum_amount_to_withdraw { print( String( format: "\nTRANSACTION FOR ACCOUNT OF %@ (Account number %d)", account_owner, account_number ) ) print( "\n -- Transaction not completed: " ) print( String( format: " Cannot withdraw %.2f. Withrawal limit %.2f", 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 ) } } } // Here begins the 'main' program. 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" )