I’m working on a script which emulates a basic bank account that can deposit and withdraw money like an ATM. I made a forum thread earlier which was helpful but since then I’ve added new features such as an interest calculator as well as a date and time register. Taking my script to the next level, what I am struggling with is assembling all the unique information for each transaction to generate into a “confirmation number”. This is the task I am working on now.
In greater detail, here is a snippet of the description from the exercise provided by the instructor for a non-credit as a hobby Udemy course:
So for example, the confirmation should look something like this:
D-140568-20190315145900-124
…where:
- “D” represents deposit
- ”140568” represents the account number (which is arbitrary)
- ”20190315145900” represents a concatenated local timestamp (Specific timezone: MST)
- “124” is the transaction ID. The instructor specifies it starts at zero and with each transaction, it should increment by 1.
That is the breakdown and structure of the confirmation number that I am trying to have my script generate for each transaction.
Here is my script:
from datetime import datetime
from pytz import timezone
class TimeZone:
def __init__(self, locality='US/Mountain'):
self.tz = datetime.now(timezone(locality))
self.readable_format = '%Y-%m-%d %H:%M:%S %Z%z'
print(f'The date and time: {self.tz.strftime(self.readable_format)}')
self.transaction_time_id_format = '%Y%m%d%H%M%S'
print(f'The date and time condensed: {self.tz.strftime(self.transaction_time_id_format)}')
class Account:
interest = 0.005 # Percent
def __init__(self, first_name, last_name, account_num=1400, starting_balance=0.00):
self.first_name = first_name
self.last_name = last_name
self.full_name = f'{first_name} {last_name}'
self.account_num = account_num
self.balance = starting_balance
self.transaction_id = 0
def deposit(self, amount):
self.balance += amount
print(f'D-{self.account_num}-{TimeZone(self.transaction_time_id_format)}-{self.transaction_id+1}') # problem line
def withdraw(self, amount):
if amount > self.balance:
raise ValueError('Transaction declined. Insufficient funds. Please deposit some more $$$ first.')
self.balance -= amount
def pay_interest(self):
monthly_rate = self.interest/12
monthly_sum = monthly_rate * self.balance
return monthly_sum + self.balance
def __repr__(self):
"""Return a string that represents the account."""
return f"{self.__class__.__name__}({self.last_name}, {self.first_name}, balance={self.balance})"
Here is me playing around with my classes in my REPL with output:
$ bpython
bpython version 0.22.1 on top of Python 3.10.2 /usr/bin/python
>>> import script
>>> time_instance = script.TimeZone()
The date and time: 2022-03-09 22:58:27 MST-0700
The date and time: 20220309225827
Above I am instantiating the TimeZone class. As you can see with the print statements, it’s working so far.
But when I attempt to instantiate the Account class and perform a deposit()
, I get a traceback directing my attention to line 27:
>>> BA = script.Account('Winston', 'Smith')
>>> BA.deposit(100)
Traceback (most recent call last):
File "<input>", line 1, in <module>
BA.deposit(100)
File "/home/<user>/dev/projects/python/2018-and-2020/Udemy-Fred-Baptiste-OOP/script.py", line 27, in deposit
print(f'D-{self.account_num}-{TimeZone(self.transaction_time_id_format)}-{se
lf.transaction_id+1}')
AttributeError: 'Account' object has no attribute 'transaction_time_id_format'
At line 27 I am trying to create the confirmation number using an f-string. The problem is with the way I am calling the TimeZone
class embedded within the Account
class within the deposit()
method.
So my first question is: How do I properly interpolate the transaction_time_id_format
call (with a ‘now’ timestamp for the MST time zone) from the TimeZone
class into the deposit()
method that exists in the other Account
class?
For my humble purposes here right now with this thread, I am first just trying to build a basic f-string with the matching format of the confirmation number embedded within the method. Although I realize that I am kind of missing the point and have mostly strayed away from the larger picture that the instructor is asking for.
What the instructor really wants is another completely separate method elsewhere in the script that is likely primarily responsible for generating confirmation numbers which can then print transaction details individually or in aggregate. That is probably the end game. But at this point it’s not clear to me what that next step might be in terms of writing an outside mechanism to be triggered during every deposit()
/ withdrawal()
/ pay_interest()
method call to generate confirmation numbers.
To assist with this next level challenge, any further hints, advice, guidance, and tips are welcome.