Solved

Multiple classes

Posted on 2004-10-23
177 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
Question by:Cyart
    14 Comments
     
    LVL 86

    Expert Comment

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

    Assisted Solution

    by:CEHJ
    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:
    > 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
     

    Author Comment

    by:Cyart
    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
    I can see what I have been doing wrong, I will need to look into collections.
    0
     
    LVL 30

    Expert Comment

    by: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();
    }
    0
     

    Author Comment

    by:Cyart
    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
    >>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
    > 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
    GrandSchtroumpf,

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

    Expert Comment

    by:GrandSchtroumpf
    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
    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
    8-)
    0
     
    LVL 30

    Expert Comment

    by:GrandSchtroumpf
    <:°)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    either24  challenge 19 58
    Understanding byte in Java 2 28
    Eclipse MARs unable to start with exit code=13 2 32
    Problem to setup 18 55
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    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.

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now