1

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;

        }
   }
2
  • can you post an example of your .dat files? How can you connect an account to a customer? Commented Oct 1, 2012 at 2:16
  • 1
    In addition to the accounts and customers (I would use a List<Account> and List<Customer> instead of arrays), you should build a Map<Integer, Account> mapping account numbers to Account objects. (Or whatever type besides Integer is appropriate for an account number.) The Customer can have a List<Integer> accountNumbers for all its accounts, and you can look up the account objects as needed, or actually populate a List<Account> accounts in the Customer. Commented Oct 1, 2012 at 2:34

2 Answers 2

2

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;
         }
    }
Sign up to request clarification or add additional context in comments.

10 Comments

another problem is that each customer can have multiple accounts. Would this method still work if that were the case?
sure, you could make the accounts variable inside our new CustomerAccounts class an array, then you can stick as many accounts in there as you'd like
How would this work in the context of the following code? public class MonthlyReport() { public static void main(String args[]){ Account a = new Account(accountNumber, accountType, balance, openingDates, aprAdjustment, feeAdjustment); accounts[i]=a; Customer c = new Customer(customerID, firstName, lastName, mailingAddress, emailAddress, flag, accountNum); customers[i]= c; } } Also, sorry I don't know how to format code in a comment, so I put it in the original question.
What you'd want to do is create an array of this new object while you're reading in the .dat files instead of seperate Customer and Account objects. Does that make sense at all?
So you mean create an array of each object? Currently I am using to object arrays, but I am not entirely sure how I would do it as a single array of objects. Also, like I said I updated my original question to have an example of what I have.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.