Accessing a single object from an array of objects that is in another class

Okay so I have a driver class called MonthlyReport that reads in data from 2 different .dat files, one has information regarding accounts and another has information regarding customers. In my driver class I create 2 arrays of objects that store the information on accounts and customers respectively. The problem is that once I have sent the customer data to the customer class, I am not sure how to specify which account belongs to each customer and cannot modify the information in their account based on who they are.

So basically, I want to access the corresponding account object to its customer but I am not sure how to access the object that is created in MonthlyReport from the Customer class. It is more of a conceptual problem and I do not necessarily need code so much as I need a design suggestion. I can add some of my code up if it helps to answer the question. Thanks in advance.

  public class MonthlyReport() {
       public static void main(String args[]){

             //reading in account data here

             Account a = new Account(accountNumber, accountType, balance, openingDates, aprAdjustment, feeAdjustment);
             accounts[i]=a;

             //reading in customer data here

             Customer c = new Customer(customerID, firstName, lastName, mailingAddress, emailAddress, flag, accountNum);
             customers[i]= c;

        }
   }

Upvotes: 1

Views: 749

Answers (2)

trashgod
trashgod

Reputation: 205775

Another problem is that each Customer can have multiple accounts.

Assuming a one-to-many relation beween Customer and Account details, MonthlyReport should hold a reference to a Map<Customer, List<Account>> data.

Addendum: As you read the customer file and accumulate Customer instances in your Map, the List<Account> for each will initially be empty. At the same time, add each Customer to a Map<AccountNumber, Customer> index. Later, as you read the account file, you can use the index to find the Customer for each new account record, which should then be added to that customer's List<Account>.

Upvotes: 1

Andrew Walters
Andrew Walters

Reputation: 4803

You could write a custom class which contains both of the arrays datatypes and combine the information from your .dat files into an array of this new type

public class CustomerAccounts
{
    public Account[] Account {get; set;}
    public Customer Customer {get; set;}
}


public class MonthlyReport() {
   public static void main(String args[]){

         CustomerAccounts[] allData = new CustomerAccounts[totalCustomers];

         //Read in customers
         for (i = 0; i < totalCustomers; i++)
         {
               Customer c = new Customer(customerID, firstName, lastName, mailingAddress, emailAddress, flag, accountNum);
               CustomerAccounts customerAccount = new CustomerAccounts()'
               customerAccount.customer = c;
               allData [i] = customerAccount;

         }

         for (i = 0; i < totalAccounts; i++)
         {
              Account a = new Account(accountNumber, accountType, balance, openingDates, aprAdjustment, feeAdjustment);

              //Look up customer account belongs to to get index in all data array
              int index = lookupIndex();

              CustomerAccounts customerAccount = allData [index];
              int numberOfAccounts = customerAccount.accounts.count;
              allData [index].accounts[numberOfAccounts] = a;
         }
    }

Upvotes: 2

Related Questions