Пример #1
0
from functools import reduce
from operator import add

from banking.domain import Account, CheckingAccount

accounts = [  # heterogeneous list
    Account("tr1", 10000),
    CheckingAccount("tr2", 20000, 1000),
    Account("tr3", 30000),
    CheckingAccount("tr4", 40000, 10000),
    Account("tr5", 50000)
]
total_balance = 0
for acc in accounts:  # external loop
    if isinstance(acc, CheckingAccount):
        total_balance += acc.balance
print(f"Total Balance in Checking Account's: {total_balance}")

x = 42
y = False
name = "jack"
acc = Account("tr6", 600000)

if_checking_account = lambda acc: isinstance(acc, CheckingAccount)
checking_accounts = list(filter(if_checking_account, accounts))
for acc in checking_accounts:
    print(acc)

balances = list(map(lambda a: a.balance, filter(if_checking_account,
                                                accounts)))
for bal in balances:
Пример #2
0
def a_checking_account():  # (1)
    """
    creates a CheckingAccount object with iban "tr1", balance 1000, and overdraft 500
    """
    return CheckingAccount("tr1", 1000, 500)
Пример #3
0
from random import random

from banking.domain import Account, CheckingAccount

account = None
if random() < 0.5:
    print("Head")
    account = Account("tr1", 10000)
else:
    print("Tail")
    account = CheckingAccount("tr2", 20000, 5000)

account.withdraw(10)
print(account)
Пример #4
0
from banking.domain import CheckingAccount, InsufficientBalance

try:
    acc1 = CheckingAccount("tr1", 10000, 2500)
    acc1.withdraw(10000)
    print(acc1)
    acc1.withdraw(2500)
    print(acc1)
    acc1.withdraw(1)
except ValueError as err:
    print(err)
except InsufficientBalance as err:
    print(f"{err.message}: deficit= {err.deficit}")

    acc1 = Account(
        "tr1",
        1000)  # __init__(self, iban, balance) ==> Account(acc1,"tr1", 1000)
    print(str(acc1))
    acc1.withdraw(500)  # def withdraw(self, amount) ==> withdraw(acc1, 500)
    print(str(acc1))  # ==> __str__(self)
    acc1.deposit(750)
    print(acc1)
    acc1.withdraw(1000000)
except ValueError as err:
    print(err)
except InsufficientBalance as err:
    print(err)

try:
    acc2 = CheckingAccount("tr2", 1000, 500)
    acc2.withdraw(1500)
    print(acc2)
except ValueError as err:
    print(err)
except InsufficientBalance as err:
    print(err)

# Alt + Enter
orcl = Stock("orcl", 111.34)
print(orcl)
orcl.price = 109.76  # setter method
print(orcl)

customers = [
    Customer("1", "Jack Bauer", "*****@*****.**"),
Пример #6
0
from banking.domain import Account, CheckingAccount, Customer

# heterogeneous list
accounts = []
accounts.append(Account("tr1", 1000))
accounts.append(CheckingAccount("tr2", 2000, 500))
accounts.append(Account("tr3", 3000))
accounts.append(CheckingAccount("tr4", 4000, 2500))
for acc in accounts:
    acc.withdraw(500)  # polymorphic call
    acc.deposit(250)  # polymorphic call
    print(str(acc))


def account_cost(accounts):  # generic
    for acc in accounts:
        if isinstance(acc, (CheckingAccount, Account)):
            acc.withdraw(50)  # polymorphic method


def get_total_balance(accounts):  # generic
    total = 0
    for acc in accounts:
        total = total + acc.balance  # polymorphic call
    return total


def get_customers_total_balance(customers):  # generic
    total = 0
    for customer in customers:
        for account in customer.get_all_accounts():