• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

UB create method

 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic