aspose file tools*
The moose likes Beginning Java and the fly likes constructor with array type. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "constructor with array type." Watch "constructor with array type." New topic
Author

constructor with array type.

brian yuen
Greenhorn

Joined: Oct 20, 2003
Posts: 20

how can i make the accNo in class Record can store two accNo?
[ October 24, 2003: Message edited by: brian yuen ]
[ October 24, 2003: Message edited by: brian yuen ]
[ October 24, 2003: Message edited by: brian yuen ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944


how can i make the accNo in class Record can store two accNo?

try it like this

then it should work
[ October 24, 2003: Message edited by: Oliver Refle ]
[ October 24, 2003: Message edited by: Oliver Refle ]
brian yuen
Greenhorn

Joined: Oct 20, 2003
Posts: 20
i still can't work it out
here is what i want
://take the cusomter name, and account Id
addRec.add(new Account(name, Id);
output like
brian, 123
//take another account no
addRec.add(new Account(Id));
output then like
brian, 123, 321
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Hi Brian
Just to be clear - are you trying to allow the 'Record' class to hold references to more than one account? (By the way, you have the name of the class as 'Record', but your constructors are for a class called 'Account' - the names should tie up).
If so, you need to have a member in the Record class of an appropriate data structure - I would suggest using an ArrayList (see API docs for details). Rather than passing in the name and account number to the Record constructor, pass in the name (String) and an object of type List (holding the accounts). So, your constructor's signature will now look like:

You could then add an 'addAccount(String newAccount)' method, which adds the 'newAccount' to the ArrayList, if you need to add further accounts to the customer's record later on (i.e. after construction).
You might also want to take a minute to think about how you are naming your classes/methods/variables - perhaps a better name for the 'Record/Account' class would be 'Customer', or 'AccountHolder', etc.? But mostly, you need to have a clear idea of what that class is for - does it represent a customer, or an account? Right now you seem to be mixing the 2.
Hope this helps
Michael


"One good thing about music - when it hits, you feel no pain" <P>Bob Marley
brian yuen
Greenhorn

Joined: Oct 20, 2003
Posts: 20
thanks for all your suggestion.
but the problem is that,
the construtor take 3 args,
1. If the customer choose open account, it's like
take a customerId, customerName, accountNo as args
if (customerId not in the record)
add customerId, customerName, accountId to Record
the output would look like
1, Brian, 12343434

2. when a cusomter choose open a account (a customer can has two account)
take a customerId, customerName, accountNo as args
if (customerName IS IN the record)
add accountNo under its cusotmerId( i just need to acc accountNo to Record)
1,Brian, 12343434, 11112222(so the cusomter now has two account)
so i thought i need 2 constructor, what's wrong withthe design
[ October 24, 2003: Message edited by: brian yuen ]
[ October 24, 2003: Message edited by: brian yuen ]
Michael Fitzmaurice
Ranch Hand

Joined: Aug 22, 2001
Posts: 168
Brian
I think you need to draw the distinction between creating instances of your class and creating new accounts for an existing customer. If you have a 'Customer' class, each instance can represent 1 existing customer. Now, each customer may have 1 or more accounts, so you need to store all of a customer's accounts in a dynamic data structure like an ArrayList (this would be a member of the 'Customer' class). Don't use a String or String array to hold a customers account(s). If you use a String, you limit the customer to one account. If you use a String array, you will have to constantly resize and remap it whenever a customer opens a new account. Another benefit is that you can also create instances of a customer with no accounts if need be, plus you would only need 1 constructor.
You may also store all customers in another ArrayList, but don't confuse storing customers with storing account details for one customer. If somebody is opening a new account, you have 2 scenarios - either
1 - they already have an account, in which case there should be an existing Customer object for that client, and you would then call a method on the object (e.g. addAccount, or something like that) which would add a new String to the ArrayList member of that object
or
2 - they have no existing account, in which case you will need to construct a new Customer object for that new client
I think maybe what you are getting at is that when somebody wants to open an account you need to check if they already have one. If so, that means a search through all Customer objects to see if one matches. If there is no match, construct a new Customer and add it to the container for customers.
Perhaps you could post your instructions here, so that I can be sure what it is you are trying to do?
Michael
brian yuen
Greenhorn

Joined: Oct 20, 2003
Posts: 20
Thanks very much,

that should be working....i think ,
[ October 24, 2003: Message edited by: brian yuen ]
[ October 24, 2003: Message edited by: brian yuen ]
 
wood burning stoves
 
subject: constructor with array type.