File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird: about the primary key of a record in Data file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "URLyBird: about the primary key of a record in Data file." Watch "URLyBird: about the primary key of a record in Data file." New topic
Author

URLyBird: about the primary key of a record in Data file.

Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
Hi guys,

I am coding my SCJD project URLyBird, here are the all data fileds in the database file.

The database that URLyBird uses contains the following fields:
Field descriptive name Database field name Field length Detailed description
Hotel Name name 64 The name of the hotel this vacancy record relates to
City location 64 The location of this hotel
Maximum occupancy of this room size 4 The maximum number of people permitted in this room, not including infants
Is the room smoking or non-smoking smoking 1 Flag indicating if smoking is permitted. Valid values are "Y" indicating a smoking room, and "N" indicating a non-smoking room
Price per night rate 8 Charge per night for the room. This field includes the currency symbol
Date available date 10 The single night to which this record relates, format is yyyy/mm/dd.
Customer holding this record owner 8 The id value (an 8 digit number) of the customer who has booked this. Note that for this application, you should assume that customers and CSRs know their customer ids. The system you are writing does not interact with these numbers, rather it simply records them. If this field is all blanks, the record is available for sale.

what's the primary key? it seems i can't identify a unique room,(need a room number?) because, if the size of the room is 2, then there are two records in the database file.
---------------------------------------------------
hotel1 | NY | 2 | N | 100| 2008/08/08 | 12323232 |
---------------------------------------------------
hotel1 | NY | 2 | N | 100| 2008/08/08 | 12323231 |

Thank you!
Ronggen


SCJP 1.4,<br />SCJD Java 2,<br />...
Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
because i have a value object Room, so i want to identify a unique room, then the customers can book it.
Vinicius Florentino
Greenhorn

Joined: Feb 26, 2008
Posts: 16
Can you insert in Room object the field: recNo? You can know the recNo reading the database.
Simon Hogg
Greenhorn

Joined: Jan 29, 2008
Posts: 7
Ask yourself a quick question; if there are two records containing the same data should it matter which one is updated?

If you have two records with essentially the same room data:

---------------------------------------------------
hotel1 | NY | 2 | N | 100| 2008/08/08 | |
---------------------------------------------------
hotel1 | NY | 2 | N | 100| 2008/08/08 | |

You can assume that the hotel has two rooms and they are capable of being reserved by two different clients.

As far as your program is concerned the only difference between the two rooms is the position in the data file. It doesn't contain any information about the room number, whether there's a balcony or if it overlooks the beach or the carpark. The hotel deals with that side of things, the program doesn't have to.

Once you've booked one (it doesn't matter which) you have two unique rooms their difference coming from the booking customer ID.

Provided your data view in the client is a separate from the order of the records in the data file, it doesn't really matter which record you choose to update.

If you have two customers booking the same room at the same time you will need to make sure that the second doesn't overwrite the first (e.g. find all of the matching records and loop through them until you get a lock on one).


I'm going off on a tangent now, but there is another edge case to consider:
Say you have I identical rooms (where I > 1) of which J are booked (where J < I) and K are not (where K = I - K).
If you have L customers who had booked one of these rooms call up to cancel at the same time that M call up to book one. What happens if M > K but M < K + L?

In the best case, enough cancellations will go through that all of the new bookings succeed (and we may even have some left over) before we run out of rooms.
In the worst case all of the booking requests will go through before any of the cancellations. In the latter case you will have a number of users see that their book request failed because there weren't enough rooms, although the refreshed data (if you are refreshing ;) may well show that there are plenty of rooms available. In this case, the users can just resubmit the book request and the customers have a booked room and are happy.

If there weren't enough cancellations some unlucky customers are still going to be left without a room
[ March 10, 2008: Message edited by: Simon Hogg ]
Vinicius Florentino
Greenhorn

Joined: Feb 26, 2008
Posts: 16
What room will be updated? The selected room in JTable. If click in the first one and the recNo equals 45, you will book the room with the recNo = 45. If doesnt contains any other information than "hotel1 | NY | 2 | N | 100| 2008/08/08 | |" to identify the correct room, I dont know what can do, need something to know the correct Room, like recNo.

(where K = I - K) the correct is: K = I - J, right?

So, whats the problem if the "cancel thread" run after the "book thread"? You dont need to resolve this problem. Its bad, but dont worry with that, the worst case its a fatality.

=]
Simon Hogg
Greenhorn

Joined: Jan 29, 2008
Posts: 7
I just thought that since we can't tell the difference from the data itself, why bother. It's a bit like eating peas. You don't number the peas and eat them in order, you just start eating them

If you use recNo as part of the room data, then yes, you would. And as it happens if the first record that gets booked is on line 45
However two users told to book a room for different customers, when faced with two identical-looking rooms in the UI will likely both pick the first one. If they both click on book at the same time, if the record number is important then one will be told that the room has already been booked, even when there's a perfectly good alternative room available. So the user would have to select the other record and book that one.
If you ignore the record number then you can do a little magic and have two rooms booked and the user would never notice.
It's not a big deal and I don't think it makes that much difference in the end. Just something else to think about when writing up the choices

Yeah, I meant k = I - J. Part of the way through I started renaming things and got a bit muddled up

Oh there wasn't a problem with it, I was just pointing out that in a rather contrived scenario that there were times when not caring about the record numbers for otherwise identical records might make it more flexible and usable.

For me I just prefer it if the client doesn't know anything about how the data is stored.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Howdy, Ronggen.

what's the primary key?

Well, you don't have a primary key. You'll have to use the position of the record in the database to uniquely identify it, and use this to update or delete the record (in your GUI, you won't be deleting or inserting records, just updating the "owner" field of a particular record).

Also, I don't think it is correct to add the "record number" attribute to the Room class. This looks more as a parameter that should be kept only in the database.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
Hi all,

Thank you for your smart reply!

i'm clearer now, I will add the filed recNo to the Room class, exactly, i think the accurate name of the class should be RoomRecord.

but i also have a question about how to judge some customers booked the same room; from the data file, nothing we can do; is it not included in our program?

Thank you!
Ronggen
Ronggen Liu
Ranch Hand

Joined: Jul 29, 2007
Posts: 40
exactly, it seems don't need the value object Room, just use a Map to cache the records in the data file.right?

Thank you!
Ronggen
Noe Cruchet
Greenhorn

Joined: Mar 26, 2008
Posts: 5
hi all,
I have also the project URLyBird and now I am confused by create method in DBMain interface.

I agree with Roberto Perillo
Well, you don't have a primary key. You'll have to use the position of the record in the database to uniquely identify it, and use this to update or delete the record (in your GUI, you won't be deleting or inserting records, just updating the "owner" field of a particular record).


According to the assignement, GUI permet user to so "select" or "update". But why , in DBMain interface, are there create and delete method?

thanks a lot
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Howdy, Noe.

But why , in DBMain interface, are there create and delete method?


Well, I think their idea was to see how you deal with all this, you know... depending on how you developed the class that implements the DBMain interface, you'll have a lot of concerns... for example, when I load the class that implements this interface, I store all records in a Map structure, and when the application closes, I save these records back to the physical .db file, and during that, I check if new records came in and if the records that exist in the .db file were deleted. This whole thing is more tricky than it looks like, there are many things hidden there... but in a nutshell, provide these functionalities (deleting and creating records) and say that you're ready for "future maintenances"

Have a great day/evening!
Noe Cruchet
Greenhorn

Joined: Mar 26, 2008
Posts: 5
Thanks Roberto.
It is really very helpful!!


No�
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URLyBird: about the primary key of a record in Data file.
 
Similar Threads
URLyBird - Maybe my database file come messed !! Helpp me
NX:[URLyBird]How to use the specified Data File Format?
URLyBird 1.1.3 Schema description section Data File Format
URLyBird Data File Format
How to start with Data File - URLyBird 1.3.3