简体   繁体   English

从另一个类中的对象数组中访问单个对象

[英]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. 好的,所以我有一个名为MonthlyReport的驱动程序类,它从2个不同的.dat文件中读取数据,一个包含有关帐户的信息,另一个包含有关客户的信息。 In my driver class I create 2 arrays of objects that store the information on accounts and customers respectively. 在我的驱动程序类中,我创建了2个对象数组,分别存储帐户和客户的信息。 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. 所以基本上,我想访问其客户的相应帐户对象,但我不知道如何从Customer类访问MonthlyReport中创建的对象。 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;

        }
   }

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 您可以编写一个包含两个数组数据类型的自定义类,并将.dat文件中的信息合并到这个新类型的数组中

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;
         }
    }

Another problem is that each Customer can have multiple accounts. 另一个问题是每个Customer可以拥有多个帐户。

Assuming a one-to-many relation beween Customer and Account details, MonthlyReport should hold a reference to a Map<Customer, List<Account>> data . 假设CustomerAccount详细信息之间存在一对多关系, MonthlyReport应保留对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. 附录:当您阅读客户文件并在Map累积Customer实例时,每个的List<Account>最初将为空。 At the same time, add each Customer to a Map<AccountNumber, Customer> index . 同时,将每个Customer添加到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> . 稍后,当您阅读帐户文件时,您可以使用index查找每个新帐户记录的Customer ,然后将其添加到该客户的List<Account>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM