# BankBetter.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-22 File created. # 2022-12-18 Converted to Python 3. # The BankAccount class in this program has a constructor, i.e., # a method that it invoked automatically when a BankAccount # object is created. In Python, the constructor method has # the name __init__(). The term 'constructor' is not frequently # used in Python documentation, but I guess it is not a big # mistake to call the __init__() method a constructor. class BankAccount : def __init__( self, given_account_owner, given_account_number, initial_balance ) : self.account_owner = given_account_owner self.account_number = given_account_number self.account_balance = initial_balance def show_account_data( self ) : print( "\n\nB A N K A C C O U N T D A T A : " ) print( " Account owner : %s" % self.account_owner ) print( " Account number: %d" % self.account_number ) print( " Current balance: %.2f" % self.account_balance ) def deposit_money( self, amount_to_deposit ) : print( "\n\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)" % \ ( self.account_owner, self.account_number ), end="" ) print( "\n Amount deposited: %.2f\n Old account balance: %.2f" % \ ( amount_to_deposit, self.account_balance ), end="" ) self.account_balance = self.account_balance + amount_to_deposit print( " New balance: %.2f" % self.account_balance, end="" ) def withdraw_money( self, amount_to_withdraw ) : print( "\n\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)" % \ ( self.account_owner, self.account_number ), end="" ) if self.account_balance < amount_to_withdraw : print( "\n -- Transaction not completed: Not enough money" \ " to withdraw %.2f" % amount_to_withdraw, end="" ) else : print( "\n Amount withdrawn: %.2f\n Old account balance: %.2f" % \ ( amount_to_withdraw, self.account_balance ), end="" ) self.account_balance = self.account_balance - amount_to_withdraw print( " New balance: %.2f" % self.account_balance, end="" ) def transfer_money_to( self, receiving_account, amount_to_transfer ) : print( "\n\nTRANSACTION FOR ACCOUNT OF %s (Account number %d)" % \ ( self.account_owner, self.account_number ), end="" ) if self.account_balance >= amount_to_transfer : receiving_account.account_balance = \ receiving_account.account_balance + amount_to_transfer print( "\n %.2f was transferred to %s (Account no. %d)." % \ ( amount_to_transfer, receiving_account.account_owner, receiving_account.account_number ), end="" ) print( "\n Balance before transfer: %.2f" % self.account_balance, end="" ) self.account_balance = self.account_balance - amount_to_transfer, print( " New balance: %.2f" % self.account_balance, end="" ) else : print( "\n -- Not enough money for transfer.", end="" ) # The main program begins. jazz_player_account = BankAccount( "Louis Armstrong", 121212, 0 ) moon_walker_account = BankAccount( "Neil Armstrong", 191919, 7777.77 ) jazz_player_account.deposit_money( 3333.33 ) jazz_player_account.withdraw_money( 4444.44 ) moon_walker_account.transfer_money_to( jazz_player_account, 2222.22 ) moon_walker_account.show_account_data() jazz_player_account.show_account_data()