DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Posts
    2

    Too many methods problem

    Hi,

    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?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    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.
    }
    Add a new variable

    private ArrayList accountListeners=new ArrayList();

    to Account.

    Then add three methods to Account:
    Code:
    public void addAccountListener (AccountListener al) {
      accountListeners.add(al);
    }
    public void broadcastChange (AccountEntry ae) {
      for (int i=0; i<accountListeners.size(); i++) {
        (AccountListener) al=(AccountListener)accountListeners.get(i);
        ai.accountChanged(ae);
      }  
    }
    public void broadcastSelection (AccountEntry ae) {
      for (int i=0; i<accountListeners.size(); i++) {
        (AccountListener) al=(AccountListener)accountListeners.get(i);
        ai.accountSelected(ae);
      }  
    }
    To sum it up then; when a new Account is opened in Ledger you do like:

    account.addAccountListener(this);
    account.addAccountListener(this.mainProgram);

    (I assume the ledger has a pointer to a Main_program instance).

    Then you place calls to the broadcast methods inside Account wherever it is
    required.

    mkay ?
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    2

    Thanks

    Thanks for the reply I'll try this.

    Cheers
    BLP

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links