Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multiple classes

Posted on 2004-10-23
14
Medium Priority
?
179 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:Cyart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12390253
Bank has 0..n Customer. Customer has 0..n Account
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 375 total points
ID: 12390273
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
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 375 total points
ID: 12390819
> 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Cyart
ID: 12392050
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
 

Author Comment

by:Cyart
ID: 12392198
I can see what I have been doing wrong, I will need to look into collections.
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12392434
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
 

Author Comment

by:Cyart
ID: 12392532
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12392767
>>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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12392862
> 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
 

Author Comment

by:Cyart
ID: 12421042
GrandSchtroumpf,

Thanks for the info, what then gets placed in the constructors arguments of the account class.
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12427782
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12427859
I'm not sure i would define a relationship in that direction. At least not until a circular relationship becomes vital
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12542773
8-)
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12573199
<:°)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question