2012-10-01 5 views
1

좋아요. 두 개의 다른 .dat 파일에서 데이터를 읽는 MonthlyReport라는 드라이버 클래스가 있습니다. 하나는 계정에 관한 정보이고 다른 하나에는 고객에 관한 정보가 있습니다. 내 드라이버 클래스에서 나는 계정과 고객에 대한 정보를 각각 저장하는 2 개의 객체 배열을 생성한다. 문제는 일단 고객 데이터를 고객 클래스에 보내면 각 고객에게 속한 계정을 지정하는 방법을 알지 못하고 자신이 누구인지에 따라 계정의 정보를 수정할 수 없다는 것입니다.다른 클래스에있는 객체 배열에서 단일 객체에 액세스합니다.

기본적으로 해당 고객에게 해당 계정 개체에 액세스하려고하지만 Customer 클래스의 MonthlyReport에서 만들어진 개체에 액세스하는 방법을 모르겠습니다. 그것은 개념적인 문제에 가깝고 설계 제안이 필요한만큼 코드를 꼭 필요로하지 않습니다. 질문에 대답하는 데 도움이된다면 제 코드를 추가 할 수 있습니다. 미리 감사드립니다.

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; 

     } 
    } 
+0

.dat 파일의 예를 게시 할 수 있습니까? 계정을 고객에게 어떻게 연결할 수 있습니까? –

+1

계정과 고객 외에도 배열 대신 목록과 목록 을 사용하는 경우 Map 계정 개체를 계정 개체에 매핑해야합니다. (또는 정수 이외의 유형은 계좌 번호에 적합합니다.) 고객은 모든 계좌에 대해 계좌 번호 목록을 보유 할 수 있으며 필요에 따라 계좌 개체를 조회하거나 실제로 계좌 번호 을 채울 수 있습니다. –

답변

2

당신은 또 다른 문제는 각 점이다 배열 데이터 유형을 모두 포함하는 사용자 정의 클래스를 작성하고이 새로운 형태의

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

또 다른 문제는 각 고객이 여러 계정을 가질 수 있다는 것입니다. 이 경우에도이 방법을 사용할 수 있습니까? –

+0

확실히, 당신은 우리의 새로운 CustomerAccounts 클래스 내에서 accounts 변수를 배열로 만들 수 있습니다. 그러면 원하는만큼 많은 계정을 유지할 수 있습니다 –

+0

다음 코드의 컨텍스트에서 어떻게 작동합니까? 'public class MonthlyReport() { public static void main (문자열 args []) { 계정 a = 새 계정 (accountNumber, accountType, balance, openingDates, aprAdjustment, feeAdjustment); \t \t accounts [i] = a; 고객 c = 새 고객 (고객 ID, 이름, 성, mailingAddress, emailAddress, 플래그, accountNum); \t \t \t 고객 [i] = c; } } 또한 죄송합니다. 코멘트에 코드의 형식을 지정하는 방법을 모르므로 원본 질문에 넣습니다. –

1

의 배열로 .dat 파일의 정보를 결합 할 수 있습니다 Customer은 여러 개의 계정을 가질 수 있습니다.

CustomerAccount 세부 사항 beween 일대 다 관계를 가정하면, MonthlyReportMap<Customer, List<Account>> data에 대한 참조를 유지해야한다.

추가 정보 : 고객 파일을 읽고 MapCustomer 인스턴스를 축적하면 각 파일에 대한 List<Account>은 처음에는 비어 있습니다. 동시에 CustomerMap<AccountNumber, Customer> index에 각각 추가하십시오. 나중에 계정 파일을 읽을 때 index을 사용하여 각 계정 기록에 대해 Customer을 찾은 다음 해당 고객의 List<Account>에 추가해야합니다.

관련 문제