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)
WHILE THIS IS IN THE RUNNER CLASS ;
//Adding a new Client
Client newClient = new Client();
//Ask user to input details of client being added
System.out.println("Please enter ID");
System.out.println("Please enter name");
System.out.println("Please enter surname");
System.out.println("Please enter email");
System.out.println("Please enter address");
System.out.println("Please enter mobile no.");
//Add to DvdShop
I don't know what am I doing wrong. Can somebody please help?? (code snippets are really appreciated. thanks )
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.
Joined: Jan 30, 2012
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 ();
Joined: Feb 02, 2009
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
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.