#Ex 7.2 from "A primer on..."
# add an attribute to a class
class Account2:
def __init__(self, name, account_number, initial_amount):
self.name = name
self.no = account_number
self.balance = initial_amount
self.transactions = 1
def deposit(self, amount):
self.balance += amount
self.transactions += 1
def withdraw(self, amount):
self.balance -= amount
self.transactions += 1
def dump(self):
s = f"{self.name}, {self.no}, balance: {self.balance}, transactions: {self.transactions}"
print(s)
"""
Simple way to test a class:
1. Create an instance with given attributes
2. Call some methods that change these attributes
3. Check that the attributes' values are as expected.
"""
def test_Account2():
a = Account2('js',1234,0)
a.deposit(100)
a.withdraw(50)
expected = (50,3) #(balance,transactions)
success = (a.balance, a.transactions) == expected
assert success
test_Account2()
a1 = Account2('Joakim',12345,100)
a1.deposit(50)
a1.withdraw(10)
a1.dump()
"""
Terminal> python Account2.py
Joakim, 12345, balance: 140, transactions: 3
"""