wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Home contractor assignment, update()/create(), DuplicateKeyException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Home contractor assignment, update()/create(), DuplicateKeyException" Watch "NX: Home contractor assignment, update()/create(), DuplicateKeyException" New topic
Author

NX: Home contractor assignment, update()/create(), DuplicateKeyException

Werner Joerling
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Hi,
why the method void update(int recNo, String[] data) does not throw a DuplicateKeyException as int create(String[] data) does?
I assume, that structurally the argument data represents the same in both methods, namely the record values (name, location, specialties, size, rate, owner), and therefore the method update() has to prevent duplicate keys, too.
Werner
[ April 18, 2003: Message edited by: Werner Joerling ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Actually in update you want a key that matches one that is already used. Because you are updating, not inserting.
You need the key to match one that exists so you know which record to update.
So there is no such thing as a DuplicateKeyException in update.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Nick Lee
Ranch Hand

Joined: Dec 17, 2002
Posts: 45
Actually in update you want a key that matches one that is already used. Because you are updating, not inserting.
You need the key to match one that exists so you know which record to update.
So there is no such thing as a DuplicateKeyException in update.

Mark,I'm a new comer here and I'm confused if we should consider that the new coming record and one of the old record are totlly the same,at that time,we should throw a DuplicateException,am I right?


Nick Lee
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

No Nick, you are a little confused.
If I am adding a new record to the database, then the create() which adds the new record needs to check to see if that key already exists, if it does, that means you are trying to add a second record with mathcing keys, or in other words you are adding a duplicate key, hence the reason for throwing a DuplicateKeyException in the create() method.
Now if you are only changing an already existing record, you will call the update() method, and this method needs to be passed a key that already exists, so that it finds that record then updates it with the changed data. So you see in this case the record/key must already exist. So if you threw a DuplicateKeyException here, you would basically always be throwing this exception everytime you call this method, because you have to pass an already existing key to update that record.
Mark
Werner Joerling
Ranch Hand

Joined: Mar 23, 2003
Posts: 35
Because this application deals with booking records and not editing records, it only has to change the owner field from an empty to a non-empty value. In this special situation we really need not care about duplicate keys, and furthermore, need not answer the question which fields the key consists of.
The record to update is given by recNo, and data contains the all fields of the record. This generality is suited for fictive (but real world) enhancements that may sound like: "Make it possible to edit and save all fields of a single record." Then the question for the key gets important and IMHO update() has to throw DuplicateKeyException.
[ April 21, 2003: Message edited by: Werner Joerling ]
[ April 21, 2003: Message edited by: Werner Joerling ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"IMHO update() has to throw DuplicateKeyException."
Never, you will never have to change the Key, and therefore never throw a DuplicateKeyException.
Mark
Nick Lee
Ranch Hand

Joined: Dec 17, 2002
Posts: 45
hi Mark
Now,I ve understood what u mean,and thanks for ur instruction.
I ve another question on my system:
how can I produce a customer ID(8 digit number) in my gui? what is it format?
00001234 or 12345678?
Is it a random one or not?
thanks very much
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well a customer ID. Hmm, there could be many solutions. You could use the customers Social Security number. You could try using a random number, but that can't guarantee that two clients might get the same number. Same with Thread ID, there isn't a guarantee there either.
In my Beta exam with the contractor assignment, I wasn't required to have the application make the customer ID or name. It was just a JTextField that the user enters this information.
Hope that helps
Mark
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi Mark, and Nick Lee,
In B&S assignment, it is clearly mentioned in the Database schema about this customer ID. It says "Th id value(an 8 digit number) of the customer who has booked this. Note that for this application, you should assume that customer and customer service representatives know their customer ids. The system you are writing does not interact with these numbers, rather it simply records them."
So according to this, user can choose any 8 digit number. I personally feel 00001234 == 1234. It is upto the user, who enters the number.
Further comments are welcome.
GVRao
Nick Lee
Ranch Hand

Joined: Dec 17, 2002
Posts: 45
You could use the customers Social Security number.

hi,Mark
What do u mean with customers Social Security number? I haven't any info about that.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Nick,
Do you have the same requirement as Venkateswara Rao Gunturu? If so, you can ignore Mark's suggestion about using a Social Security Number - just use any 8 digit number since you do not have to do anything with it other than record it.
If your question was "what is a social security number" - it is an Americanism. Every American has a unique social security number which can identify them to govenernment departments and other organisations.
Some other countries have similar unique identifiers for each citizen,but not all. So you could not use the social security number in an application that might be used in any country other than America.
Even in America I suspect that this might cause problems - what happens if another company is doing the booking in the company's name? Do you ask the company secretary to provide her social security number?
Regards, Andrew
[ April 23, 2003: Message edited by: Andrew Monkhouse ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Yes, ignore my Social Security part. Basically the client ID is nto something that has to be figured out in code. The users already know the client's ID and will enter it themselves.
Mark
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
Let's back to the original question, In my assignment the create method takes String[] data as input param, we don't know which column is the key, how could we judge when to throw DuplicateKeyException in the implementation? Or just leave it there without doing anything?
I got this problem after finished create method, and surprisingly found no way to throw this exception.
In this database, we take recNo as the access key, whether reuse deleted record's space or not, we can avoid put two live record in to the some place, so no DuplicateKeyException .
Appreciate any comment.
Larry


The hill is not a hill.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

The first value can be passed to the find method which would return the Key.
Now the find method is implemented by you, so I can't tell you exactly what to do there, but it does throw a RecordNotFoundException, which is the result you want, because that means that this is not a DuplicateKey. Now if it returns some value, in my case it is the record number, I throw the DuplicateKeyException. So I actually have that it != -1, even though -1 is never sent back from the find method.
Mark
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
Thx, Mark.
I got your idea, it's really helpful,but not in the Hotel Booking assignment.
I think two record can be completely the same before they are booked by someone after when they get a unique customer id.
Let's take a look at the columns:
Hotel Name,City,Maximum occupancy of this room,
Is the room smoking or non-smoking,
Price per night,Date available,Customer holding this record;
Before book course, it's usual for the hotel which has two same rooms.
So when find() could get same return values, I think it doesn't mean we couldn't add another same room.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

If that is the case that there are two of the same hotel rooms, then there will be two records that look identical. The only difference is the Kye which is the record number.
Now my Spider senses tell me that Sun wouldn't do that to you, there must be something different in the data between those two rooms. Maybe room number or something, or maybe they made in the requirements that there can only be one room in a hotel of a type. Something that would make the find method be able to find one of those rooms and not all of the same.
What is the find method signature in the interface that Sun provided?
Mark
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27

I can't find any "unique like" ideas according to code above, neither in other part of the instruction doc.
Chan Huan
Greenhorn

Joined: Apr 29, 2003
Posts: 1
I got a same assignment. I think the confusion is what is key. through having analysed the instruction and business model, the key may be consist of name and location field.
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by Chan Huan:
I got a same assignment. I think the confusion is what is key. through having analysed the instruction and business model, the key may be consist of name and location field.


that cant be it Chan Huan, because I have several records that would cause problems if that was the case.
and think about it, a hotel has several rooms, which may be up for grabs at the same time, and there are no room numbers, else that and a name/location combo would definately work.
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
Chan, when read the instruction doc for the first time, I had the same view. But there aren't any specific instruction that mention it, so "may be" couldn't do any help. And I think there possibly two same rooms in a hotel, that is to say, with same hotel name and location.
Appreciate your comments.Keep in touch
Larry.
Larry Li
Greenhorn

Joined: Nov 25, 2002
Posts: 27
TQ, I'm not quite agree with it.
I think a hotel with two same room is possible.
Yes, a room number is the best identity, but without it, a room can still be booked, when a customer comes, the waiter takes him to one of these rooms randomly. Looks strange, but possible.
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by Larry Lee:
TQ, I'm not quite agree with it.
I think a hotel with two same room is possible.
Yes, a room number is the best identity, but without it, a room can still be booked, when a customer comes, the waiter takes him to one of these rooms randomly. Looks strange, but possible.


I think you misread what I wrote, I tried to get across that a hotel has several rooms, these may be identical, sleeps 5 people, smoking, etc, and without the room number there is no way to uniquely identify the room, not from our database anyway, so thats why we issue record numbers to the rooms, problem solved.
now without a room number AND a record number, how does the person taking reservations know which "random" room the waiter has taken the new guest to ?
and if he doesn't, nor care, then at least he'd have to know how many such rooms he has available, and decrement so as not to reserve a room the waiter you speak of has just taken the guest to.
we dont have that luxury out of the box like the FBN assignment which I believe has a very similar setup from a different angle, and while I have no doubt you can implement it, I dont think its a good approach, but thats just my opinion, its too much work, one too many workarounds, and more code than required to get the job done
Surya De
Greenhorn

Joined: Nov 06, 2005
Posts: 28
I am assuming this is the B&S assignment. Then am I to understand from a post in this thread that we are only booking in this application and nothing more...that we dont have to worry about supposing a company has 10 employees and 3 of them are hired out then the company can still be used by another customer because now there are 7 employees left to be hired out?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: Home contractor assignment, update()/create(), DuplicateKeyException
 
Similar Threads
No way to throw DuplicateKeyException
My design choices
URLyBird : Need Suggestions on create method and update method
B&S Data exceptions
update method not throws DuplicateKeyException!