""" Ex 7.2 from "A primer on... Modify the Account-class provided in the book, to include a new attribute that counts the number of transactions for the account. Also write a test method for testing that the class works. """ class Account: def __init__(self, name, account_number, initial_amount): self.name = name self.number = account_number self.balance = initial_amount self.transactions = 1 def deposit(self, amount): self.transactions += 1 self.balance += amount def withdraw(self, amount): self.transactions += 1 self.balance -= amount def dump(self): s = f"{self.name}, {self.number}, {self.balance}, {self.transactions}" print(s) """ Recipe for test functions for a class: 1. Create an instance of the class (!) 2. Call one or more methods 3. If the methods return a value, compare this with the expected output. If they don't return a value, but simply modify the instance's attributes, compare these values with the expected. """ def test_Account(): a1 = Account("John Olsson", 19371554951, 20000) a1.withdraw(1000) a1.deposit(50) assert a1.balance == 19050 assert a1.transactions == 3 test_Account() a1 = Account("John Olsson", 19371554951, 20000) a2 = Account("Liz Olsson", 19371564761, 20000) a1.deposit(1000) a1.withdraw(4000) a2.withdraw(10500) a1.withdraw(3500) print("a1’s balance:", a1.balance) a1.dump() a2.dump()