/    Sign up×
Community /Pin to ProfileBookmark

quick question

hey guys, ok im writing a program with 2 class’. how do i keep track of objects being made from the driver class,

example?tghis would be created in the driver class)

Customer c1 = new Customer( “dante” , “Hicks” );
Customer c2 = new Customer( “Han”, “Solo” );

i need to be able to keep track of them as the are created from the other class, because i need to set an id for each object. if anyone understands my problem and can help me out i owuld appreciate it.

thnks.
-keko-

to post a comment
Java

6 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliMar 06.2005 — there are few options for you.

1. Create an array of Customer and keep every newly created Customer

object in that array. e.g

Customer[] customers = new Customer[10];

customers[0]= new Customer( "dante" , "Hicks" );

customers[1] = new Customer( "Han", "Solo" );

  • 2. add every customer object that you create in a java.util.List .e.

    java.util.List customerList = new java.util.ArrayList();

    Customer c1 = new Customer( "dante" , "Hicks" );

    customerList.add(c1);


    Customer c2 = new Customer( "Han", "Solo" );

    customerList.add(c2);


  • 3. You can create another class that will chache your obects and you

    can use Hashtable and assign a key to evey object you store in that

    etc,


  • Hope this helps
    Copy linkTweet thisAlerts:
    @keko2005authorMar 06.2005 — ok, im not sure if i follow, and i know it might be alot but this is the code im using, i dont hting that im supposed to use and array, i iwll have to ask my prof. he never specified that we have to use and array.

    driver class
    ------------------------------------------------------------------


    public class Project2 {

    public static void main( String args[ ] ) {

    Customer c1 = new Customer( "hey " , "Hicks" );
    Customer c2 = new Customer( "Han", "Solo" );



    System.out.println( "Amount of customers: " + Customer.getCount( ) );
    System.out.println( c1.getId() + ": " + c1.getFirst() + ", " + c1.getLast() );
    System.out.println( c2.getId() + ": " + c2.getFirst() + ", " + c2.getLast() );





    }

    }


    Customer class
    ---------------------------------------------------------------------


    public class Customer {

    private String first;//stores customers first name
    private String last;//stores customers last name
    private int id = 0;//stores customers id #
    private static int count=0;//customer count

    public Customer() {//default constructor

    first = "Default";
    last = "Customer";
    id++;
    count++;

    }//end default

    public Customer(String f, String l){//parameter constructor

    if(f.equals("") || l.equals("")){

    first = "Default";
    last = "Customer";
    id++;
    count++;

    System.err.println("Default values used for costomer");

    }
    else{


    first = f;
    last = l;
    id++;
    count++;
    }
    }//end constructor




    public static int getCount(){//accessor for customer count

    return count;

    }//end access


    public int getId(){//accessor for customer id

    return id;

    }//ens access

    public String getFirst(){//

    return first;

    }

    public String getLast(){//

    return last;

    }


    public void setFirst(String f){//mutator for customers first name

    if(f.equals(" ")){

    first = "default";

    }
    else{

    first =f;

    }
    }//end mutator

    public void setLast(String l){//mutator for customers last name

    if(l.equals("")){

    last = "Customer";

    }
    else{

    last = l;

    }

    }//end mutator


    public String toString(){

    return id + ": " + last + ", " + first;

    }




    }

    these are the 2 classes. thats the driver our prof. gave use to test out class. everything comes out right so far, except the id, it comes out 1, and 1, instead of 1,2,3 etc. any suggestions?
    Copy linkTweet thisAlerts:
    @Khalid_AliMar 06.2005 — ok I get it. For that purpose you will have to increment the id your self everytime you create an object or decalre id as static.(decaring a static variable like that is not a good practice at all so I would question that why your prof will ask you guys to implement such solution)
    Copy linkTweet thisAlerts:
    @keko2005authorMar 06.2005 — thnks, i think we are both not really understanding each other, cause im not supposed to increment the id manually, its supposed to be automatic, so maybe im wrong on wha i have to do, but i dop have the pdf, and it doesnt say to use arrays or anything, just says i have to get the id, by keeping track of the objects created. but thnks anyway, i appreciate it.

    -keko-
    Copy linkTweet thisAlerts:
    @Khalid_AliMar 06.2005 — ?

    sure
    Copy linkTweet thisAlerts:
    @ray326Mar 07.2005 — You need to keep your Customers in a CustomerList as Khalid suggested. Then you could use the current size of the CustomerList as the ID of the next Customer added. That gives you automatic ID management.
    ×

    Success!

    Help @keko2005 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...