I am trying to make a small general ledger program which will consist of the following classes:
- Main_Program - displays a text based menu for recording different transactions
- Ledger - class that holds a List/HashMap of Account objects
- Account - class that holds the String AccountName and int AccounNo along with a List/HashMap that holds AccountEntry objects
- AccountEntry - Object that stores the date particulars and amount of the transaction along with an int AccountEntryID.
The problem is that if I want to display an amount from an AccountEntry I have to use the following method calls:
1. Main_Program - Call method in Ledger called getAccountEntryAmount(AccountEntryID)
2. Ledger - Call method in Account called getAccountEntryAmount(AccountEntryID)
3. Account - Call method in AccountEntry called getAccountEntryAmount(AccountEntryID)
All these methods to get one AccountEntryAmount value. If I were to have many Ledger type lists such as customer lists etc the Main_Program will have methods corresponding to all the lowest level class' methods.
Is there a shortcut to access a variable (eg AccountEntryAmount) that is so deeply nested i.e main_Program-->Ledger-->Account-->AccountEntry-->Amount, so as to avoid so many identical methods calls?
03-24-2005, 06:12 PM
sjalle
So the Main program gets a ledger, and that ledger contains the accounts
with the entries that they all want to read, right ?
Then Main and ledger and all other interested parties need (at least) a method like:
Code:
public void accountEntrySelected(AccountEntry acc) {
.
textFieldForValue.setText(Integer.toString(acc.getValue()));
}
that any present Account object would call when an entry was selected or
changed.
The good way to do this is to make an interface
Code:
public interface AccountListener {
public void accountEntrySelected(AccountEntry acc);
public void accountEntryChanged(AccountEnrty acc);
}
Then you define the Main_Program and the Ledger as implementations of this
interface like
Code:
public class Main_Program implements AccountListener {
.
.
public void accountSelected (AccountEntry) {
textFieldForValue.setText("selected:"+Integer.toString(acc.getValue()));
getValue()));
}
public void accountEntryChanged(AccountEnrty acc) {
textFieldForChangedValue.setText("changed:"+Integer.toString(acc.
}