Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I just wanted to say that you guys RULE, a million thank you's to whoever created, and/or manages this site. KEEP UP THE GOOD WORK..."

Geography

Where in the world do Tek-Tips members come from?
davidb09 (Programmer)
21 Aug 09 19:31
I am writing an basic accounts package which records money coming in/out of three accounts.
I have a basic 'Transaction' interface (java abstract class). Then depending on the account, I have 3 classes which implement the 'Transaction' interface: AccountOne, AccountTwo, AccountThree (not real account names)
Each transaction will be an instance of one of the account classes. I currently store all 'Transactions' in one single java 'ArrayList'.
To get a total of money going through an account type within a given month, I am currently iterating through the ArrayList, and using 'instanceOf' to determine the transaction type, to give a total for a specific account type.

Having read about the negatives of 'instanceOf', is there a better way to do this using e.g. polymorphism...
I cannot get my head around the problem...I'm hoping someone can suggest another way..????
jmeckley (Programmer)
22 Aug 09 10:12
Transaction would have a public member like GetTotal(). each concrete implementation would implement GetTotal();

Then cycling through the array you call GetTotal(); something like this (i'm a C# guy, so my syntax may be wrong.)

CODE

int total = 0;
ArrayList transactions = GetAllTransactions();
foreach(object transaction in transactions)
{
   total += ((Transaction)transaction).GetTotal();
}
return total;
Get total will have all the components necessary to calculate the total of the transaction.

Jason Meckley
Programmer
Specialty Bakers, Inc.

FAQ855-7190: Database Connection Management

stevexff (Programmer)
24 Aug 09 4:06
This doesn't sound right to me. Although it seems reasonable for accounts to implement the Transaction interface, this fails the is-a test. A transaction isn't an account, it's something you can do to an account. (It should probably be a class in its own right, so an account can list its transactions, and a transaction always has two accounts to enforce double-entry book-keeping, although this may be overkill for what you need). So your arraylist isn't a list of transactions at all, it's a list of accounts. Maybe it's just the nomenclature - if we called it the TransactableAccount interface it might be clearer?

But realistically, will there ever be a type of account that doesn't support transactions? Maybe you can move the interface methods up to an abstract base class for Account? It makes more sense to a casual reader of your code for Account to support getTotal() than it does for it to be treated as a Transaction.

To paraphrase Ward Cunningham, you know you are reading good code when each class and method does pretty much what you'd expect it to...

Steve

"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)

jmeckley (Programmer)
24 Aug 09 8:28
I agree with Steve. While your original question was not about how to structure your objects. It would make more sense to separate the concepts of Account and Transaction.

Jason Meckley
Programmer
Specialty Bakers, Inc.

FAQ855-7190: Database Connection Management

davidb09 (Programmer)
24 Aug 09 12:45
I have changed the structure of the program as you say. This also makes it easier to get totals for individual accounts as each account now has its own array of transactions.

Thanks both for the help!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close