Main Content

Rooms available tonight - how to code an algorithm from first principles

Archive - Originally posted on "The Horse's Mouth" - 2013-08-19 06:58:30 - Graham Ellis

Delegates on Well House Consultants courses usually book 6 to 8 weeks ahead. Business guests at Well House Manor are usually booked a week or two ahead, and the leisure guests we have with us have normally given us good notice too. Telephone calls enquiring about rooms for the same night, mid-evening and later, are usually frustrating for us and for the enquirer, since we provide a full service - nice rooms and a place to stay and facilities, rather than just a bed for the night with a shared bathroom and loo. And that's typically what the late bookers really want, at a price to match the facilities.

However, there's another market out there - for the people who are booking a last minute staycation, or touring the area and booking during the day, and they're an excellent fit to our product.

Our hotel web site - until now - has shown availability for future dates and suggested people phone us up if they want to book and checkin on the same day. It's highlighted today's date in grey so that we can instantly know the date, and it's avoided late bookings through the automated system which we might not notice or might not have noticed in the past. But with our extended team, and our ever-heavier use of online booking, I've changed the system to offer rooms for the same night up to 8 p.m, and indeed to flag availability on the front of the Well House Manor web site for the current day. Examples screen captures here show yesterday's daytime display (we have 2 rooms available tonight), and the display as it was later in the evening.

A sort of change of topic ... One of the questions that comes up for newcomers to programming is "how do you decide to work it out and do it THAT way? And this requirement to add in a status for today's rooms is a good point from which to provide a simple answer.

Firstly, I look at the inputs and outputs required from the new logic - in this case variables that I will use in filling in the existing page template. And the outputs are:
  $ton - the message to display
  $tonk - the background colour behind the message
  $bklinkstart - the code to link (or not link) to the booking engine
  $bklinkend - matching code to end that link
The inputs are
  $tonight - rooms free tonight
  $tonlink - a link to a page to book for tonight
  the system's date() function to tell me the current hour number

Then I start separating out the cases. I have chosen to go for a default, offering the visitor a red panel and saying "full" unless there are conditions which mean otherwise, which I identify with if statements and then override my already-defaulted output variables in the conditional block. Let me show you the actual code - in PHP


  <?php
  
  # set up defaults
  
  $ton = "Sorry - Full Tonight<br /><br />Please book for future nights by selecting your arrival date below.";
  $tonk = "CC4400";
  $bklinkstart = "";
  $bklinkend = "";
  
  # Just one or more than one room - change colour and message
  
  if ($tonight == 1) {
    $ton = "Just 1 Room available tonight<br>[click here] to book";
    $tonk = "999900";
  }
  if ($tonight > 1) {
    $ton = "Rooms available tonight<br>[click here] to book";
    $tonk = "00CC00";
  }
  
  # If rooms available - add in a link
  
  if ($tonight > 0) {
    $bklinkstart = "<a href=https://lightning.he.net/~wellho/hotel/reservation.php?date_checkin1=$tonlink style=\"text-decoration:none;color: white;\">";
    $bklinkend = "</a>";
  }
  
  # If it's late in the day, supress the work done above and
  # put up a welcome for future nights
  
  if (date("H") > 19) {
    $ton = "Now closed for bookings<br>to stay tonight<br /><br />Please book for future nights by selecting your arrival date below.";
    $tonk = "000088";
    $bklinkstart = "";
    $bklinkend = "";
  }
  ?>


This is very much demonstration code in a spike solution form. I should have used a nested if to save a test and add robustness if rooms are available, for example, and I should probably call a function to set up links and use variables to hold colours.

Having written the code Testing is vital. The more possible conditions you have on your input data, the more tests are needed and you should test
• What happens is a value is LESS THAN a limit
• What happens is a value is GREATER THAN a limit, not forgetting
• TEST AT THE LIMIT too.
So I was running this code during the afternoon, late in the evening .. and also between 7 and 9 pm to make sure it flipped over properly. And on top of that, I created dummy settings (literally forced in different variables) to see what happened when availability dropped. We want to avoid any embarrassing overbookings at 'all costs' after all; we are fortunate that the site traffic is not high, and I could (and did) take the risk of it giving incorrect results for a few seconds while I did the tests - but on a busy site, that's absolutely something that could not be done and you would work with a copy of the script, or possibly a whole test environment which could be followed by quality assurance before it goes live (if you write your own testing schedule, you are likely to miss out the same condition in writing the tests as you did in forming the code!)

The example above is in PHP ... same thing applies to Python, which I'm training on in a couple of hours!