2

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
3
  • How are you persisting the data? Are you using some sort of ORM? Commented Sep 18, 2011 at 17:54
  • I'm persisting the data, currently using pickle. I know there are issues with pickle, but I need something very lightweight and am not worried about security issues. Commented Sep 18, 2011 at 18:18
  • Why are there close votes on this question? Commented Sep 18, 2011 at 18:22

1 Answer 1

2

1) Does this seem a sensible way of organizing classes?

Yes, it seems so. Remember that LineItem "ispartof" Account, and Account itself is part of Journal. There are no inheritance relationships around, you just have a collection of Accounts in Journal, and a collection of LineItem's in Account.

The only issue I see is about naming. You say that the class is a LineItem, and later that a LineItem is a Transaction. Maybe you should give it a moment of thought. Why didn't you name it Transaction? Would you need yo clarify it if the class was named Transaction?

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.

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?

Pay attention to the fact that you have used the word Transaction again. You must store the date attribute in the LineItem's objects. I think that's a sensible approach. If you don't want to change the lookUp methods constantly, in order to track of all the possible attributes you can search for, consider creating an Options class in which you can store the search parameters, storing all the attributes you can need now and in the future.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

It does help. start_date and end_date are set at runtime (and may not be set or may be set more than once - sorry if this was not clear). Iterating through each lineitem instance to store the values seems inefficient. I'm editing the question to show the date lookup method and will think more about your other suggestions
Do you mean: iterating through each LineItem instance to check the values seems inefficient? If that's the problem, then the list of LineItem's should be clearly be sorted by their date field. You can also maintain different lists of references in order to have it sorted by multiple fields. But, in any case, maybe a database manager should come handy, as Roseman suggested.
Sorry for the misunderstanding about start_date and so on... I have edited my answer to reflect it. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.