Link to home
Start Free TrialLog in
Avatar of Cyart
CyartFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Multiple classes

I have produced a program to replicate a bank account. The classes are as follows bank account, customer, and a TestClass which implements main. This program worked fine with input dialogs and message dialogs prompting the user. From main I declared an array of bank accounts and from the bank account class I declared a reference to the customer class.

Within main
Bank[] newBank = new Bank[100] ;
newBank [noOfAccounts] = new Bank () ;

Within bank
Customer newCustomer = new Customer () ;

However, I have been up for a challenge so I have decided to convert the program to Contentpanes, whilst still using the same procedure for creating bank accounts and customers. However, when I try and display the account information I now have different interest rates for the bank accounts which is as expected, but all the customer names are the same, but they all change to the last enetered customer name. To get around this I thought that I could just declare a customer array along with the bank account array, which I feel would probably get arround the problem.

The question I have is is this the correct way of doing things i.e create the customer and bank from main or should the bank account be used to create the customer. Like I said with input boxes and message boxes it all worked fine.

Any info would be much appreciated
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Bank has 0..n Customer. Customer has 0..n Account
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cyart

ASKER

Thanks,

I see what you mean, one question though why did it work previous with JOptionPane but now it will not work with ContentPanes?
Avatar of Cyart

ASKER

I can see what I have been doing wrong, I will need to look into collections.
Avatar of GrandSchtroumpf
GrandSchtroumpf

One more thing:

> Customer[] myCustomers = new Customer[numCustomer];
this line just declares an array, it does not initialize the content of the array.

if you want to have an array full of customers you need to do something like this:

Customer[] myCustomers = new Customer[numCustomer];
for (cnt = 0; cnt < numCustomer; cnt++) {
  myCustomers[cnt] = new Customer();
}
Avatar of Cyart

ASKER

Yes,

I have been doing that anyway, How do you deal with a customer that has multiple accounts, do myou use a container? if so what type?
>>do myou use a container? if so what type?

Yes - we tend to call them 'collections' in Java. I would use a Set (see my link above)
> do you use a container?
Or in my example with arrays:

myAccounts[0] = new Account(myCustomers[2]);
myAccounts[1] = new Account(myCustomers[2]);

means that customer 2 has accounts 0 and 1.
note that the index of the account does not need to match the index of the customer.
Avatar of Cyart

ASKER

GrandSchtroumpf,

Thanks for the info, what then gets placed in the constructors arguments of the account class.
your Account class should look like this:

public class Account {

    // This account's Customer
    private Customer myCustomer;

    // Data access to Customer
    public Customer getCustomer() {
        return myCustomer;
    }

    // Constructor with Customer argument
    public Account(Customer aCustomer) {
        this.myCustomer = aCustomer;
    }

}

Remember that this is only valid for "one customer per account" relationship.
I'm not sure i would define a relationship in that direction. At least not until a circular relationship becomes vital
8-)