Bank Account Demo¶
Here is an example of a class that manages a Bank. It is far from complete, but should give you an idea how all this works.
This is the banker.py driver program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | # banker.py
from BankManager import *
def main():
    print("Bank Manager v1.0")
    bank = BankManager("Silly Savings and Loan")
    account_num = bank.open_account("savings", 500)
    print("Created account:", account_num)
    account_num = bank.open_account("checking", 1500)
    print("Created account:", account_num)
    account_num = bank.open_account("pirate", 200)
    bank.deposit(account_num, 1250)
    bank.show_assets()
main()
 | 
Here is the BankManager.py file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42  | # BankManager.py
from BankAccount import *
class BankManager(object):
    def __init__(self, bank_name):
        self.name = bank_name
        print("Creating bank:",bank_name)
        self.accounts = []
    def open_account(self,account_type, initial_deposit):
        account_number = None
        account_type = account_type.lower()
        if account_type in ["checking", "savings"]:
            account = BankAccount(account_type)
            account.deposit(initial_deposit)
            self.accounts.append(account)
            account_number = account.number
        return account_number
    def show_assets(self):
        cash = 0
        print("Current status:")
        for account in self.accounts:
            print("\tAccount number:", account.number)
            print("\t\tAccount type:", account.account_type)
            print("\t\tcurrent balance:", account.balance)
            cash += account.balance
        print("Cash on hand:", cash)
    def deposit(self, account_number, amount):
        this_account = None
        for account in self.accounts:
            if account.number == account_number:
                this_account = account
                break
        if this_account:
            this_account.deposit(amount)
        else:
            print("Illegal account number - ignored")
 | 
And the BankAccount.py file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  | # BankAccount.py - manage accounts
class BankAccount(object):
    ACCOUNT_NUMBER = 1000
    def __init__(self, account_type):
        self.account_type = account_type
        self.number = BankAccount.ACCOUNT_NUMBER
        BankAccount.ACCOUNT_NUMBER += 1
        self.balance = 0
    def deposit(self, amount):
        self.balance += amount
 |