I’m writing an expense tracking program. At the moment I have three class
Lineitem stores the attributes for individual transactions and has methods format a transaction for output (payee, amount date), etc. Each lineitem is one transaction.
Account is a collection of lineitems and has methods to add a lineitem, list its lineitems, determine the value of all of its lineitems, etc.
Journal is a collection of accounts and has methods to add a new account, list its accounts, format a list of accounts, etc.
1) Does this seem a sensible way of organizing classes?
2) I’m adding a method to only look at transactions between a run-time specified start_date and end_date. The best way might be to store start_date and end_date somewhere, then modify Account to change some methods to receive start_date and end_date parameters and then examine lineitem dates when totaling or listing its linetimes. Are there other approaches I should be considering?
EDIT: method in accounts to implement the date check. self.lineitems is a list of lineitems. start_date and end_date are set and runtime and may not be set or may be set more than once.
def get_lineitems(self, start_date, end_date):
if start_date and end_date:
for lineitem in self.lineitems:
if start_date <= lineitem.date <= end_date:
yield lineitem
else:
for lineitem in self.lineitems:
yield lineitem
Yes, it seems so. Remember that
LineItem“ispartof”Account, andAccountitself is part ofJournal. There are no inheritance relationships around, you just have a collection of Accounts inJournal, and a collection ofLineItem‘s inAccount.The only issue I see is about naming. You say that the class is a
LineItem, and later that aLineItemis aTransaction. Maybe you should give it a moment of thought. Why didn’t you name itTransaction? Would you need yo clarify it if the class was namedTransaction?Anyway, the needs of your application will change with time (will grow with time). Probably, you need to worry more about how will you adapt it to the future needs, instead of now, in which the requirements are clear. Your second question has to do with this.
Pay attention to the fact that you have used the word Transaction again. You must store the
dateattribute in theLineItem‘s objects. I think that’s a sensible approach. If you don’t want to change thelookUpmethods constantly, in order to track of all the possible attributes you can search for, consider creating anOptionsclass in which you can store the search parameters, storing all the attributes you can need now and in the future.Hope this helps.