Forums Register Login

UB create method

+Pie Number of slices to send: Send
Hi all, in URLyBird I implemented my delete() method by updating the delete flag to 1. The delete flag in my data file is 1 byte (using raf.writeByte(1)). Now for create() method, it throws DuplicateKeyException (public int create(String[] data) throws DuplicateKeyException). I implemented mine like this:
1) find total number of records
2) loop to check if delete flag=1
3) if yes assign loop var as return val and break loop
4) update delete flag to 0 and write data

When to throw DuplicateKeyException? Currently in step 3 above, I also check the first field, if it is the same as the input's first field, I throw DKE. Does this look right?

I tested my methods like this. say I use record 5:
read(5)
delete(5)
newRecNum = create(data)) // got record number 5 correct
read(newRecNum) // same as read(5)
delete(newRecNum)
create(data) // throw DKE?
+Pie Number of slices to send: Send
 

When to throw DuplicateKeyException?


Never. There are no fields (or combination of fields) that can be taken as primary key.

Currently in step 3 above, I also check the first field, if it is the same as the input's first field, I throw DKE. Does this look right?


No (it is the Hotel Name field, right?). Other records can have the same value for this field, and it should be ok.
+Pie Number of slices to send: Send
Until now, most of the participants almost never throw the DuplicateKeyException including me. It is very likely that a person (maybe a rich person) books 2 or more rooms (perhaps for his big family) at the same time.


Jeffry Kristianto Yanuar (Java Instructor) SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2)
+Pie Number of slices to send: Send
Thanks. After thinking about it, if I do check the fields' contents that would make the record number not a primary key per se. And if PK is numeric, I can't have duplicate records, unless the content consists of the record number as part of its content, which is unlikely.

Now on to the searching
+Pie Number of slices to send: Send
Don't forget to document your decision while working at it. Doing so is the common practice when doing the assignment


Jeffry Kristianto Yanuar (Java Instructor) SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2)
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 841 times.
Similar Threads
Data class multithreaded load test (UB1.1.2)
[B&S] Create Method
nx: All of URLy Bird 1.1.3 read/write lock(2)
findByCriteria
Unneeded exception?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:32:12.