Link to home
Start Free TrialLog in
Avatar of redsar
redsar

asked on

constructor problem

[code]
public class Record1
{
   private String[] accountNo;
   private String name;
   public Record1(String  name, String[] accountNo)
   {
      this.name = name;
      this.accountNo  = accountNo;
      printrecord();
    }
    public Record1(String[] accountNo)
    {
       this.accountNo =accountNo;
    }
}
import java.util.*;
public class test
{
    static ArrayList addRecord = new ArrayList();
    static String[] acc = new String[2];
    public static void main(String args[])
    {
         acc[0] = "123";
         addRecord.add(new Record1("brian", acc));
         Another();

    }
    public static void Another()
    {
         acc[1] = "123456";
         addRecord.add(new Record1(acc));
    }
 }
[/code]

i don't why the acc[1] is not added to the ArrayList,
when I print it, it's
123nullBrian
thanks a lot
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Probably something wrrong with your print routine. Of course the account added in Another has no name, so name will be null
Avatar of redsar
redsar

ASKER

the print is in the class Reocrd1
like this
 public String getAccNo()
    {
     String s = accountNo[0] + accountNo[1];
     return s;
    }
    public String getName()
    {
      return name;
    }
    public void printrecord()
    {
      String s = getAccNo() + getName();
      System.out.println(s);
    }
Your problem is the following:

acc[0] = "123";      // here acc[1] is still null as only the call to Another will fill something in
addRecord.add(new Record1("brian", acc));  // in creating the new Record your printrecord method is called, and here acc[1] is still null
Another();   // here we set acc[1] and add a new record, only this time no printrecord() is called

so this should do the trick:

acc[0] = "123";
 Another();
 addRecord.add(new Record1("brian", acc));
       
Avatar of redsar

ASKER

public Record1(String  name, String[] accountNo)
   {
      this.name = name;
      this.accountNo  = accountNo;
      printrecord();//this should be removed, cos i never add acc[1] to the call printrecord()
    }
I don't quite understand that as you do add acc[1] to printrecord:

public void printrecord()
    {
      String s = getAccNo() + getName();
      System.out.println(s);
    }

to get the account number you call getAccNo();

public String getAccNo()
    {
     String s = accountNo[0] + accountNo[1];
     return s;
    }

Here you return a String that concatenates acc[0] and acc[1]
Avatar of redsar

ASKER

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
You should override toString in Account:

public String toString() {
    String accName = (name == null)? "" : name;
    return accName + ", " + accountNo[0];
}

But

a. It doesnt make sense to have a null name. You should use a special method to add a new account
b. The method I gave above also needs to loop through all accounts and concatenate each one to the string
Avatar of redsar

ASKER

sorry, i stil dont understand how to do it..
the scenario is like:
if (newcustomer)
add name, accountId to Record,
if (existCusomter)
ask them the name
   if (name is in the file)
      add the accountId,
   else
      error..

Stop. Before you try to print things out as you want, take on board that this is *not* a legitimate constructor and should be removed from your code:


    public Record1(String[] accountNo)
    {
       this.accountNo =accountNo;
    }

Why should an account be create with no name, i.e. belonging to nobody?
Avatar of redsar

ASKER

no,
this is the story
//
if the operation == Addaccount,
//take customer Id and account id as args
//if (search the file , the customer exist)
//that mean the file has already taken record of this customer
//then add the accountId , to their record, //say 12345
so the record, from like

format :customerId, name, acccountid
123, brian, 121212,
123, brian, 121212, 12345
That's nothing to do with a constructor. A constructor creates a new instance of a class. You need a method to add another account number to an existing account.
Avatar of redsar

ASKER

can u give me some hints?
Avatar of redsar

ASKER

can i use like : use array list for the accounNo
public Record(String name, List accNumbers)
You need something like

public void addAccounts(String name, List accNumbers)

and yes, using a List is a good idea. In fact the Account class itself ought to include such a List for account numbers.


Avatar of redsar

ASKER

but again the problem is
I don't need to add their name if they are alreday existing customer
so I thought I need 2 constructors..
>>.so I thought I need 2 constructors..

No. As I've already said, constructors are there to create a new instance. Why would you want that?

You don't add their name, you find the instance by name
Avatar of redsar

ASKER

do u mean something like
public void addAccounts(String name, List accNumbers)
{
     //if (name is existing in record)
     add accNumbers to record // what should i do in order to do this
     else  
     // error
}

do I need to run this even for new customer ? no....probably..
You need to have a collection in which to store these account - maybe a list. Then you can do

int index = accounts.indexOf(name);
if (index > -1) {
    Account acc = accounts.get(index);
    acc.getAccounts().addAll(accNumbers);
}

>>do I need to run this even for new customer ? no....probably..

No - you'd use a constructor
Avatar of redsar

ASKER

i don;t know how this work
You should have a class (Bank?) that holds a collection of Account objects.
Avatar of redsar

ASKER

>> holds a collection of Account objects.
I don;t really understand,,sorry i 'm new to java...i guess, i should have a class called Bank
and class called Account, sorry
You don't necessarily need a new class.  You could have an ArrayList in your test class that holds the accounts (a bank variable, not a whole class).

Otherwise, you could write a Customer class, where you have an ArrayList instance variable that stores all the accounts owned by that customer.  Then you could store your Customer objects in your test class, and when you go to add an Account to a customer, do this:

if (Customer exists)
  addAccount();
else
  addNewCustomer(new Account)
Avatar of redsar

ASKER

class customer
{
   private String customerId, name;
   private  ArrayList addAccount  ;
    public customer(Stiring customeId, String name, ArrayList addAccount)
   {  
      this.customerId = customerId;
      this.name = name;
      this.addAccount = new ArrayList();
      this.addAccount.add(new Account(addAccount));
    }
}
??????????????
>>Then you could store your Customer objects in your test class, and when you go to add an Account to a customer,
ASKER CERTIFIED 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
Avatar of redsar

ASKER

Thanks very much,
import java.util.*;
public class CustomerInfo
{
   public static ArrayList accounts;
   private String name;
   public CustomerInfo(String  surname, String accountsNo)
   {
      this.surname = surname;
      this.accounts = new ArrayList();
      this.accounts.add(accountsNo);
    }

    public String getName()
    {
       return surname;
    }

    public void print()
    {
       String s = getName() + accounts;
       System.out.println(s);
    }
    public void add(String accountsNo)
    {
      this.accNo.add(accountsNo);
    }
 }
import java.util.*;
public class test
{
    static ArrayList addRecord = new ArrayList();
     public static void main(String args[])
    {
        String accoutNo = "123";
        CustomerInfo s = new Record1("brian",accountNo);
        String another = "1122";
        s.add(another);
        s.print();
    }
 }
i come up with something like this, but i don't know  whether it is working or not
>>but i don't know  whether it is working or not

Then you must compile and test it ;-)

The test class doesn't need a List to test one CustomerInfo does it?
Avatar of redsar

ASKER

  static ArrayList addRecord = new ArrayList(); //this one should remove
it works, but the outpu is like
brian[123, 1122]
how can i delete the brackets.should i change every item in the arrayList to String then print it out?

Avatar of redsar

ASKER

i can use somthing like this to convert
   public void print()
    {
       String s = this.accNo.get(0) + "," + this.accNo.get(1);
       s = getName() + "," + s;
       System.out.println(s);
    }
but any better idea?
This should really be a toString method,which all well-written classes should have.

>>String s = this.accNo.get(0) + "," + this.accNo.get(1);

How do you know there are exactly two accounts?
Avatar of redsar

ASKER

yes, that should be toString
and i could use a if statement
//if (accounts.size() ==1)
  print the name and account.get(0);
if (account.size() == 2)
 print both account...
:-)

Hint - use a StringBuffer and iterate the accounts, appending as you go
Avatar of redsar

ASKER

text file format: customer Id, name, accountNo
for example
123, Brian, 123
i use to following coed to get that
// read the file

  static ArrayList TempCusInfo = new ArrayList();  
StringTokenizer st = new StringTokenizer(line, "," );  
String CusIdInFile = st.nextToken();  
String surnameInRec = st.nextToken();  
String accNoInRec = st.nextToken();  
TempCusInfo.add(new TestRecord(CusIdInFile, surname,accNoInRec);
class TestRecord{    
private cusIdInFile, surname;    
private ArrayList accounts;  
 public TestRecord(String CusIdInFile, String surname, String accNo)    {      
   this.cusIdInFile = CusIdInFile;      
   this.surname = surname;    
   this.accounts = new ArrayList();      
   this.accounts.add(accNo);    
 }    
public void addAccount(String accountNo)
{      
    this.accounts.add(accountNo);  
}  
public String getCustomerId()  
{      
    return customerId;  
}
}

my problem is each customer can have 2 account, let say,i add an account to Brian

//take a customer Id, accountNo(the user wanna add) from user
for (int i = 0; i < TempCusInfo.size(); i++)
{TestRecord s = (TestRecord)TempCusInfo.get(i);
if (s.getCustomerId().equals(customerId))        
  s.addAccount(accountNo);

then i print all the information in the TempCusInfo arrayList
the output would like
123, Brian, 123, 456
OK, fine,
but the problem occur, when the user wanna add another account even if they have two account, I should show error message,but run this code

 String accNoInRec = st.nextToken();  
TempCusInfo.add(new TestRecord(CusIdInFile, surname,accNoInRec);

the this.accounts.size() still remain 1, cos i only read one account No (the first one)
i can't check it
>>
but the problem occur, when the user wanna add another account even if they have two account
>>

Just do something like

public void addAccount(String account) {
      if(accounts.size() < 2) {
            accounts.add(account);
      }
      else {
            throw new RuntimeException("User can only have up to two accounts");
      }
}      
   
Avatar of redsar

ASKER

the prblem is here
String accNoInRec = st.nextToken();  
TempCusInfo.add(new TestRecord(CusIdInFile, surname,accNoInRec);
for example the text file is
1,Brian, 123, 456
i just read the first three element :"1", "Brian", "123" from the text add pass it to arrayList, so the accouts.size()
will be still 1
when i use these code....the account.size still 1
public void addAccount(String account) {
     if(accounts.size() < 2) {
          accounts.add(account);
     }
     else {
Have you changed the class CustomerInfo to perform the functionality we just discussed?
Avatar of redsar

ASKER

THANKS VERy MUCH!!!
really...
althought I guess the problem is I forgot the the size of ArrayList will not be destory unless I terminate the program....
Um...what if i have to deal with EXIT() like System.exit(1).in some stage, and load the application again, so the size of arrayList will be destoryed ...
SHould I save that in a text file to avoid this problem , anyway, thanks very mxuh
>>SHould I save that in a text file to avoid this problem

Well persistence is a whole other ball game ;-)

Until then you should just watch out for setting the sizes of lists appropriately