Solved

constructor problem

Posted on 2003-10-24
37
203 Views
Last Modified: 2010-05-18
[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
0
Comment
Question by:redsar
  • 19
  • 15
  • 2
  • +1
37 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9613119
Probably something wrrong with your print routine. Of course the account added in Another has no name, so name will be null
0
 

Author Comment

by:redsar
ID: 9613135
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);
    }
0
 
LVL 5

Expert Comment

by:Calron
ID: 9613244
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));
       
0
 

Author Comment

by:redsar
ID: 9613260
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()
    }
0
 
LVL 5

Expert Comment

by:Calron
ID: 9613307
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]
0
 

Author Comment

by:redsar
ID: 9613326
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9613384
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
0
 

Author Comment

by:redsar
ID: 9613454
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..

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9613482
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?
0
 

Author Comment

by:redsar
ID: 9613517
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9613568
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.
0
 

Author Comment

by:redsar
ID: 9613649
can u give me some hints?
0
 

Author Comment

by:redsar
ID: 9613713
can i use like : use array list for the accounNo
public Record(String name, List accNumbers)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9613902
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.


0
 

Author Comment

by:redsar
ID: 9614395
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..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9614413
>>.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
0
 

Author Comment

by:redsar
ID: 9614463
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..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9614549
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
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:redsar
ID: 9614819
i don;t know how this work
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9614895
You should have a class (Bank?) that holds a collection of Account objects.
0
 

Author Comment

by:redsar
ID: 9615098
>> 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
0
 

Expert Comment

by:blackwednesday
ID: 9615573
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)
0
 

Author Comment

by:redsar
ID: 9616318
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,
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 45 total points
ID: 9616678
>>You don't necessarily need a new class.

True, but it would probably be better.

>>private  ArrayList addAccount  ;

should better be called

private  ArrayList accounts;

I get the feeling that you have not analysed the relationships between entities in this program before you have started coding. That's one of the main reasons you're finding it difficult to code, and incidentally one of the main reasons why large software projects fail so frequently ;-)
0
 

Author Comment

by:redsar
ID: 9616798
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9616900
>>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?
0
 

Author Comment

by:redsar
ID: 9616976
  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?

0
 

Author Comment

by:redsar
ID: 9617183
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9617784
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?
0
 

Author Comment

by:redsar
ID: 9618984
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...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9619224
:-)

Hint - use a StringBuffer and iterate the accounts, appending as you go
0
 

Author Comment

by:redsar
ID: 9619390
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9619404
>>
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");
      }
}      
   
0
 

Author Comment

by:redsar
ID: 9619419
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 {
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9619432
Have you changed the class CustomerInfo to perform the functionality we just discussed?
0
 

Author Comment

by:redsar
ID: 9619505
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9619572
>>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
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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

19 Experts available now in Live!

Get 1:1 Help Now