Tuesday

How many travelers are there?

The travel industry is a pretty complex (and weird) one. I've been working in travel for over 20 years now, much of it through the lens of online travel. Now that I've spent some time in web development in this space, I'm beginning to better appreciate all the nuanced logic that goes into many of the things I've personally taken for granted.

For example, I've always just sort of understood that "nonstop" and "direct" aren't the same things (Nonstop literally means the airplane is not scheduled to make any intermediate stops between your origin and your destination. This is distinctly different than direct flights, which ARE scheduled to stop en route to your destination, but there is no change in flight number), but for a programmer to figure out this distinction programmatically can be a real challenge. I'm not going to talk about this stuff here though, instead I'm going to talk through how to figure out a traveler count for display on the front end.

In this case, my task was to add a list of traveler types based on the user's search criteria. Something like:
"2 Adults, 1 child".

Definitions

To start out, we need to identify the types of travelers that exist in the world of travel. We're dealing with 2 sort of base types:

  • Adults
  • Children
But in each there are variants, based primarily on age. For example, an adult means what exactly? To the airline industry it means a person that is 12 years old or older. But UNDER like 63 years old, when that adult is then called a Senior. For travel products this is often very important because pricing is dependent upon traveler type. I won't go into the labyrinth of crazy that is a typical airline fare filing which goes quite a lot deeper, but for now let's just say this:
  • Senior (63+)
  • Adult (12-62)
  • Child (<12)
Oh yeah, there's also Infants, for when a child is typically under 2 years old (for the airlines again, there's also the complexity of infants that sit in your lap, and infants that have their own seat)...So, it really turns out looking like something like this:
  • Senior (63+)
  • Adult (12-62)
  • Child (2-12)
  • Infant (<2) - Note: I'm combining both infants-in-lap, and infants-in-seat here

Essentially, assuming we have each travelers age, we can make the above definitions in whatever language we're using. In my case, that was true, because we have a Java object someplace that defines that for me. Also, the above rules vary a bit for Hotels and Cars (e.g. you have to be 18 to rent a car), etc, but for our example, we'll use what I've outlined above.

Labels

We say "2 Adults" or "3 Children" or "1 Senior"...in other words, our labels change based on if we're talking plural or singular. So, our labels are going to be like this:
SingularPlural
AdultAdults
SeniorSeniors
ChildChildren
InfantInfants

Logic

From here it's pretty straightforward:
  1. We map our passenger type counts
  2. We define an Array of travelers
  3. We figure out how many of each traveler type there is, and based on the traveler type count, we add to our Array the count and appropriate label (either singular or plural)
  4. We output our Array as a string, concatenating each count with a comma

We map our passenger type counts
What I mean by this is we need to get number of Adults, number of Seniors, number of Children, and number of Infants from Java. Something like:
private int getNumAdults() {
  return getSearchData().getAdults();
}
Repeat this for each traveler type. This gives us a nice set of variables to work with.
We define an Array of travelers
Here we want to define a container (an Array) to collect all of our traveler info. It'll be a String type, and we'll be collecting all our traveler information in here. In addition, because we have TWO types of infants (infants in seat or in lap), we'll just add those together because we're just summarizing traveler types here. As such, we'll have an integer that is the sum of getNumInfantsInLap() + getNumInfantsInSeat(), which we'll call "infants"
We figure out how many of each traveler type there is, and based on the traveler type count, we add to our Array the count and appropriate label (either singular or plural
This is where our logic lives. I won't output the full Java code we used here, but essentially we're just doing this (I'm going to use "travelerType" as a proxy for each of my types of travelers):
IF <travelerType> is greater than 0 
   (IF <travelerType> = 1, 
    THEN add "1 " + <singularTravelerTypeLabel>" 
    ELSE add N + <pluralTravelerTypeLabel>")
ELSE do nothing.
Hopefully that reads in a sensible way to you. But what we're trying to do is check the values for our traveler types and only add stuff to our Array when there IS something to add (i.e. we don't want to say "0 Adults"), and add the appropriate label based on singular vs. plural counts. Whew!
We output our Array as a string, concatenating each count with a comma
Here we basically transform our Array items into a comma separated string. Java has something nice for us:
StringUtils.join(travelers, ", ")

Closing

So what? Well frankly, I'm not particularly sure why I thought this would make a nice blog post. Only that it's something that continually comes up for me while I implement new UI features, and I thought I'd collect my thoughts and findings somewhere I could reference later. It also occurred to me that it may be neat for other sort of non-developers like me to walk through something like this with me. Particular programming language syntax aside, the above illustrates that even something seemingly very simple, like telling the user how many travelers they've searched for, is in reality a pretty complex set of considerations and logic we have to define before we can show that information accurately to the user.

No comments:

Post a Comment