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..????
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..????