""" Exercise 7.3 from "A primer on..." Change the internal data structure of the Account class and implement additional functionality. Main points: 1. Classes & protected attributes 2. Python standard Library - documentation """ from datetime import datetime class BankAccountP: def __init__(self, first_name, last_name, number, balance): self._first_name = first_name self._last_name = last_name self._number = number init_trans = {'time':datetime.now(), 'amount': balance} self._transactions = [init_trans] def deposit(self, amount): deposit = {'time':datetime.now(), 'amount': amount} self._transactions.append(deposit) def withdraw(self, amount): withdrawal = {'time':datetime.now(), 'amount': -amount} self._transactions.append(withdrawal) def get_balance(self): s = 0 for t in self._transactions: s += t['amount'] return s def print_info(self): first = self._first_name; last = self._last_name number = self._number; bal = self.get_balance() s = f'{first} {last}, {number}, balance: {bal}' print(s) def print_transactions(self): for t in self._transactions: """ t['time'] is a datetime object, which has a method strftime which can be used to format the output. See the online documentation for details. """ time = t['time'].strftime('%a %d %b %H:%M:%S') amount = t['amount'] print(f"Time: {time}, amount: {amount}") a1 = BankAccountP('John', 'Olsson', '19371554951', 20000) a2 = BankAccountP('Liz', 'Olsson', '19371564761', 20000) a1.deposit(1000) a1.withdraw(4000) a2.withdraw(10500) a1.withdraw(3500) print("a1's balance:", a1.get_balance()) a1.print_info() a1.print_transactions() """ Terminal> python Account3.py a1's balance: 13500 John Olsson, 19371554951, balance: 13500 Time: Mon 21 Oct 17:44:28, amount: 20000 Time: Mon 21 Oct 17:44:28, amount: 1000 Time: Mon 21 Oct 17:44:28, amount: -4000 Time: Mon 21 Oct 17:44:28, amount: -3500 """