• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

UB create method

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic