This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes check if ID already exists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "check if ID already exists" Watch "check if ID already exists" New topic
Author

check if ID already exists

Yani Abela
Greenhorn

Joined: Jan 30, 2012
Posts: 17
Hi there, I am doing a java project. the aim is to have a DVD Rental System. I have some classes, 3 of which are Client, DvdShop & Runner. Of course, in the Client class, I have the Get & Set methods.
Now, my problem is that I want to add a client but I don't want to add clients with the same id. In other words, if the user is adding a client and enters an ID that already exists for another client, an error message is shown.
Here is what I've done so far;

This is in the DvdShop class;

public int AddClient (Client tmpClient, int IDtoEnter)
{
for(int i=0 ; i<cList.size(); i++)
{
tmpClient = (Client)cList.get(i);
if(tmpClient.GetId() != IDtoEnter)
{
cList.add(tmpClient);
}
}

return -1;
}

WHILE THIS IS IN THE RUNNER CLASS ;

case 1:

//Adding a new Client

Client newClient = new Client();

//Ask user to input details of client being added

System.out.println("Please enter ID");
newClient.SetId(sc.nextInt());
System.out.println("Please enter name");
newClient.SetName(sc.next());
System.out.println("Please enter surname");
newClient.SetSurname(sc.next());
System.out.println("Please enter email");
newClient.SetEmail(sc.next());
System.out.println("Please enter address");
newClient.SetAddress(sc.next());
System.out.println("Please enter mobile no.");
newClient.SetMobile(sc.nextInt());


//Add to DvdShop
myDvdShop.AddClient(newClient);

break;

I don't know what am I doing wrong. Can somebody please help?? (code snippets are really appreciated. thanks )
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776

The above code calls a method named AddClient that takes a single argument as input. But the method you shown us below takes two arguments. Please check if the right method is called.

Also you have to read on method naming conventions. AddClient() and SetId() all should start with small letters.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
1) You are using the parameter "tmpClient" as a variable in the for loop. BIG no-no! In this case its a serious error, as you lose the reference to the new client, but even if it wasn't an error its bad style to change the value of a method parameter.

2) EDIT: you are inserting the new client evtl. multiple times (wherever the ID is not like an existing ID). Simple suggestion would be to use a boolean flag to indicate if you found the item, then after the loop check its value.

3) You may want to use a generic list for the clients, as that would allow you to use the easier-to-read foreach loop.

4) You may also want to look into common naming schemes for Java. Like, methods should start with a lower case character, same with variables and fields.

So, a suggestion would be to do something like:


Hope this helps.
Yani Abela
Greenhorn

Joined: Jan 30, 2012
Posts: 17
hi there, first of all thanks verymuch for your reply it was very interesting and helpful
I tried to do the code the way you wrote it but it is giving me the following error ;

incompatible types - found java.lang.Object but expected Client

the error is given in the underlined part - for(Client client : cList)
{

I can't find why the error is coming up. If this may help, I declared the array at the beginning of the class this way ; ArrayList cList = new ArrayList ();

Thanks.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Yep as I said, you need a generic list to use the foreach loop. There's an example how to do it in the code sample I posted above. Best google for "java generics" for more information, there are helpful tutorials which explain this quite well
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

D. Ogranos wrote:Yep as I said, you need a generic list to use the foreach loop.


Not strictly true.



But I do second your general point--parameterized collections are to be preferred.
Yani Abela
Greenhorn

Joined: Jan 30, 2012
Posts: 17
thanks verymuch It helped a lot thanks again
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Jeff Verdegan wrote:
Not strictly true.

But I do second your general point--parameterized collections are to be preferred.


I stand corrected
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

Why not use a Set to store your clients instead of a List? A Set guarantees uniqueness of its elements. If I understand the problem right, then your uniqueness constraint would simply be the clients ID. If so, make sure you override the equals() and hashCode() methods of your Client class to take that into consideration.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: check if ID already exists
 
Similar Threads
What is wrong in this code ?
Help me please I dont know it
Please help me with this scanner object problem
Loop through the list of objects to get attribute.
<identifier expected> error