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
CyartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Bank has 0..n Customer. Customer has 0..n Account
0
CEHJCommented:
Bank should probably have a Set of Customer and methods to add and remove Customer, and similarly with Customer and Account. See
http://java.sun.com/docs/books/tutorial/collections/index.html
0
GrandSchtroumpfCommented:
> newBank [noOfAccounts] = new Bank () ;
does the "Bank" class represent an account?  why not calling it "Account" then?

As CEHJ said, try to model the "real world".
You first need to create your Customer instances (if you want to open an account, you first need to be a customer).
Then you can create the accounts and associate Customer instances with the Account instances.
If you only need one customer per account, you can just use the customer as parameter in the Account constructor, otherwise you can pass an array (or a collection) of Customer instances.

Example with arrays and one single Customer per Account:

Customer[] myCustomers = new Customer[numCustomer];  // or use collections as CEHJ suggested.
myCustomers[0] = new Customer();
myCustomers[1] = new Customer();
myCustomers[2] = new Customer();
...
myCustomers[numCustomer-1] = new Customer();

Account[] myAccounts = new Account[numAccount];  // or use collections as CEHJ suggested.
myAccounts[0] = new Account(myCustomers[2]);
myAccounts[1] = new Account(myCustomers[2]);
myAccounts[2] = new Account(myCustomers[1]);
...
myAccounts[numAccount-1] = new Account(myCustomers[0]);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

CyartAuthor Commented:
Thanks,

I see what you mean, one question though why did it work previous with JOptionPane but now it will not work with ContentPanes?
0
CyartAuthor Commented:
I can see what I have been doing wrong, I will need to look into collections.
0
GrandSchtroumpfCommented:
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();
}
0
CyartAuthor Commented:
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?
0
CEHJCommented:
>>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)
0
GrandSchtroumpfCommented:
> 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.
0
CyartAuthor Commented:
GrandSchtroumpf,

Thanks for the info, what then gets placed in the constructors arguments of the account class.
0
GrandSchtroumpfCommented:
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.
0
CEHJCommented:
I'm not sure i would define a relationship in that direction. At least not until a circular relationship becomes vital
0
CEHJCommented:
8-)
0
GrandSchtroumpfCommented:
<:°)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.