Welcome to Dream.In.Code
Become a Java Expert!

Join 150,157 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,385 people online right now. Registration is fast and FREE... Join Now!




how do i create a unique id when i create a customer object

 
Reply to this topicStart new topic

how do i create a unique id when i create a customer object, please help im soo stuck..

coffeebeanz
23 Aug, 2008 - 01:03 AM
Post #1

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 2

so far this is what ive done, but I dont know how to generate the unique id


public class Customer{

private int id;
private String name;
private int childSeats;
private int adultSeats;
private int numFlights;
private double cost;

public Customer(){

} THIS IS WHAT IM STUCK ON

public int getAdultSeats(){
return adultSeats;
}
public void setAdultSeats(int adultSeats){
this.adultSeats = adultSeats;
}
public int getChildSeats(){
return childSeats;
}
public void setChildSeats(int childSeats){
this.childSeats=childSeats;
}
public double getCost(){
return cost;
}
public void setCost(double cost){
this.cost = cost;
}
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getNumFlights(){
return numFlights;
}
public void setNumFlights(int numFlights){
this.numFlights=numFlights;
}
public String toString()
{
final String TAB = " ";
String retValue ="";
retValue = "Customer ("
+super.toString()+ TAB
+ "id=" + this.id + TAB
+ "name =" + this.name + TAB
+ "childSeats="+ this.childSeats + TAB
+ "adultSeats="+ this.adultSeats + TAB
+ "numFlights=" +this.numFlights + TAB
+ "cost=" +this.cost + TAB
+ ")";
return retValue;

}
}
User is offlineProfile CardPM
+Quote Post

baavgai
RE: How Do I Create A Unique Id When I Create A Customer Object
23 Aug, 2008 - 02:34 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,282



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Nice class. Try this:
java

public class Customer {
private static int lastId=0;

public Customer() {
this.id = ++Customer.lastId;
}
}


This requires an understanding of static. When java sees the class for the first time, lastId will be zero. The value exists in the class, not in each of the objects, so in a sense it's global. When you create an instance, that "global" value is incremented, used, and maintained.

For a quick check, try something like
java

System.out.println(new Customer());
System.out.println(new Customer());
System.out.println(new Customer());


Hope this helps.

User is offlineProfile CardPM
+Quote Post

coffeebeanz
RE: How Do I Create A Unique Id When I Create A Customer Object
23 Aug, 2008 - 11:29 PM
Post #3

New D.I.C Head
*

Joined: 23 Aug, 2008
Posts: 2

Thank you so much baavgai. That worked im so happy. How do you stop the package name and all that from printing out? do you know. But thanks for the time taken to reply. you've made my day.

Take care
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:40AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month