aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to identify that a room is booked? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to identify that a room is booked?" Watch "How to identify that a room is booked?" New topic
Author

How to identify that a room is booked?

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

In the data file, if a room is booked it will just have a string in the owner field that is completely full of whitespaces. But in the code, how did you guys signify that a room was booked?

There are only three options that I can think of:

1. A room is booked if the value is the empty string?
2. A room is booked if the value is a string with only whitespaces?
3. A room is booked if the value is null?

Which option did you guys go with and why?


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

I don't know how you got to this analysis, but in my application a room is booked if the customer id field contains an 8 digit number. If it contains all spaces, empty string, null,... it's an available room


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:in my application a room is booked if the customer id field contains an 8 digit number. If it contains all spaces, empty string, null,... it's an available room


In your client code you are receiving a transfer object where the owner is a String. So in theory it could be null, empty string, string with whiitespaces etc. So in your client code do you have something like this?:

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

No, because that wouldn't compile

I do not perform the actual 8 digit number check client-side, if the string is not empty (= null, empty or all whitespaces) the room is booked.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Everyone knows pseudo code doesn't compile - I think I may have left out some semi-colons!

Roel De Nijs wrote:(= null, empty or all whitespaces) the room is booked.


I don't get this - are you saying that if the value of the owner in your transfer object is either null, the empty string, or all white spaces then it is booked?

To avoid confusion a code example may be better., What is the actual check you have in your code? Something like:

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

Sean Keane wrote:are you saying that if the value of the owner in your transfer object is either null, the empty string, or all white spaces then it is booked?

No, I was just defining what I consider as "empty" and when the owner is not "empty", the room is booked

In code something like:
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Grrr. Definitely need some sleep. Apologies, my last post had a typo's & errors! I actually had the example you typed in my mind, but then wrote down the negative of it!

Roel De Nijs wrote:
In code something like:


This is what I was thinking. That you had more than one condition to check in order to verify a room is booked.

I'm not knocking your approach - it works . I was playing around with a few approaches myself and that was one of them. But the thing I didn't like about it is that you need to check two conditions in order to verify a room is booked. As someone using that code I wouldn't find that a nice approach - I simply want to have one condition to check.

But to implement a single condition approach you need to do something like saying "null implies the room is not booked, otherwise the room is booked". The difficulty I had with this approach is it means I need to convert between the empty string and null at some point (and vice-versa) which is not particularly nice either.

Like all things, it's probably going to be a trade off. I'm just throwing ideas around at the moment. Seeing what others have done. Seeing how happy I am with an approach etc.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

Sean Keane wrote:But the thing I didn't like about it is that you need to check two conditions in order to verify a room is booked.

These checks are of course in a utility method, so I don't have to type these multiple checks every time I want to know if a room is booked (and if something would change about the logic I just have to make changes in 1 place)
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:
Sean Keane wrote:But the thing I didn't like about it is that you need to check two conditions in order to verify a room is booked.

These checks are of course in a utility method, so I don't have to type these multiple checks every time I want to know if a room is booked (and if something would change about the logic I just have to make changes in 1 place)


Yes. But the issue still remains the same. If I receive your transfer object, then there are two conditions I must check in order to verify if a room is booked or not. The key point is the necessity to have to check two conditions.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

No, you just have to call that method, you don't have to do the 2 checks yourself. Just calling Utils.isBooked(theRoom) will reveal if the room is available or booked.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:No, you just have to call that method, you don't have to do the 2 checks yourself. Just calling Utils.isBooked(theRoom) will reveal if the room is available or booked.


Yes, but you still have to do two checks when looking at the transfer object returned from the server. The only difference is that you have moved those two checks into another utility method - there are still two checks .

It's a nicer solution if there is only one check that is required to be carried out on the transfer object to determine if a room is booked. It means there is only one state that relates to the room being booked, rather than two states i.e.

The null value is commonly used to indicate that something is not set. So this is a nice solution.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

But then you always have to make sure (in the setCustomerId for example) that when you pass in an empty String ("") or one containing nothing but spaces (" ") these values are converted to null. And so you are adding a bit of logic to the transfer object, which put the 2 checks in different places.

Maybe you could do something like:

You have 1 check and you can keep the transfer object dumb.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

The Pattern.matches is one solution - thinking outside the box . But the thing I don't like about that is that it's not that intuitive as saying "null implies it is not booked". I think most developers would be well accustomed to API's that use null to indicate something is not set or does not exist.

If I go with the null option, then for the transfer object the easy bit is controlling what the client code can set. That is the benefit of having getters and setters. I can simply throw an IllegalArgumentException if the argument in the setter is a non-null value. Then when the transfer object reaches the business service I can check if it is a valid string i.e. it won't be null, it will be a string containing some value.

The bit I don't like is the other direction - from the datafile to the client code. The code that reads my data file will give me an array with an empty string for a record that is not booked. When I am converting this String[] array into an transfer object in my business service I will have to go and convert the empty string into a null.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

Sean Keane wrote:I can simply throw an IllegalArgumentException if the argument in the setter is a non-null value.

And if I get an exception when I pass a non-null value, how can I set a valid customer id? Or will it just be an extra parameter in the bookRoom-method?
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:
Sean Keane wrote:I can simply throw an IllegalArgumentException if the argument in the setter is a non-null value.

And if I get an exception when I pass a non-null value, how can I set a valid customer id? Or will it just be an extra parameter in the bookRoom-method?


If you pass a non-null value, then the transfer object wouldn't throw an IllegalArgumentException. The validation of the transfer object would be on a the server side - a combination of being carried out in the business class and the data class. In the business class, as you mentioned, we could check the business rule - that the customer id is an 8 digit number. Then in the data class all other fields could be validated.

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5599
    
  15

I thought you would throw the exception from inside the setter, but you perform the validation server-side, so everything ok
Alexandra Zhang
Greenhorn

Joined: Jun 08, 2007
Posts: 16
Hi for the original poster.

My understanding based on the database schema is if the room available date is within 2 days it should be empty or available for booking. You should not change the data file, either the format or the schema. Am I right?
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Hi Alexandra. This discussion was about how to identify in your transfer object (if you have one) that a room is not booked.

Correct me if I am wrong, but I think what you are asking is something different? So probably worth a separate thread so as not to confuse.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to identify that a room is booked?