Preloader
Others
  • Estimated reading time: 6 Minutes

How to Build a Zone-Based Transit Fare Calculator: Dubai Metro Example

How to Build a Zone-Based Transit Fare Calculator: Dubai Metro Example

Zone-based fares look simple from the passenger's side, one price for a short hop and another for a long one, and turn out to be a neat little graph problem from the developer's side. The fare is not a function of distance or of how many stations you pass. It is a function of how many fare zones the journey passes through, and that depends on the route the train actually takes. This article walks through the design of a working calculator for the Dubai Metro, from the data model to the edge cases that break naive arithmetic, in a way you can implement in any language in an afternoon.

The fare model

The rules come straight from how Dubai Metro zones work: seven numbered zones of which five contain stations, three fare tiers, Gold Class doubling each one, and two quirks that catch out anyone who tries to do the maths in their head. The first quirk is that Dubai Creek is the boundary between zones 5 and 6, so a one-stop hop across the creek is a two-zone fare. The second is that the Route 2020 branch runs from zone 2 through a single-station zone 3 (Jumeirah Golf Estates) into zone 1, so two neighbouring communities on that branch can be a three-zone journey. A calculator that subtracts zone numbers gets both cases wrong. One that counts the distinct zones along the real path gets both right.

Card or ticket Inside 1 zone 2 zones 3 or more zones
Silver Nol (standard) AED 3.00 AED 5.00 AED 7.50
Gold Class AED 6.00 AED 10.00 AED 15.00
Red Ticket (paper, single trip) AED 4.00 AED 6.00 AED 8.50

Step 1: describe the network as data, not as rules

Two structures are enough. The first is each line as an ordered list of station names: the Red Line from Centrepoint to Life Pharmacy, the Route 2020 branch from National Paints (where it leaves the main line) to Expo 2020, and the Green Line from e& to Creek. The second is each zone as a list of the stations inside it. Nothing else needs to be hand-written. Adjacency falls out of the list order automatically, and the two interchange stations, Union and BurJuman, appear in both the Red and Green lists and merge into single nodes because the graph is keyed by name. Keep the branch as its own list that starts at the junction station and the fork takes care of itself.

Zone Stations Where it is Examples
1 4 The far south-west end of the Red Line and the end of the Route 2020 branch Danube, Life Pharmacy, Dubai Investment Park, Expo 2020
2 14 The southern Red Line from Business Bay's neighbour down to Energy, plus the first three branch stations Mall of the Emirates, Sobha Realty, DMCC, Ibn Battuta, Al Furjan
3 1 A one-station pocket on the Route 2020 branch Jumeirah Golf Estates
5 20 Everything north of the creek: Deira, the airport and the northern Green Line Airport Terminal 3, Union, Al Rigga, Gold Souq, Al Nahda
6 14 The central band: Bur Dubai, Sheikh Zayed Road and Business Bay BurJuman, Burj Khalifa/Dubai Mall, Business Bay, Al Ghubaiba, Creek

Zones 4 and 7 exist in the transport system but contain no metro stations; they matter for buses, not for this calculator.

Step 2: derive the lookups once

From those two lists, build two derived structures at load time. The first is a station-to-zone map, one entry per station, made by walking each zone's list. The second is an adjacency list, made by walking each line's list and recording every consecutive pair as neighbours of each other. The whole network is 53 stations, so both structures are tiny, and because they are derived rather than typed, a station rename means changing one name in one list.

Step 3: find the route, then count the zones

The calculation has three parts, and the order matters.

  1. Find the path. A breadth-first search from the origin station returns the route with the fewest stations. On this network that is also the route a passenger takes, because the only forks are the two interchanges and the Route 2020 junction.
  2. Count the distinct zones along that path. Walk the route, look up each station's zone, and count how many different zones appear. This is the number the operator charges on, and it is why the creek hop and the Jumeirah Golf Estates pocket come out right without special cases: the path itself passes through the zones, so the count includes them.
  3. Clamp to the tier. One zone is tier one, two zones is tier two, and anything from three upwards is tier three. Look the tier up in the fare table for the chosen card type.

Run that against the operator's own published examples and every one comes back right.

Journey Zones along the path Zones crossed Silver Nol Gold Class
Union to Al Rigga 5 1 AED 3.00 AED 6.00
Airport Terminal 3 to Burj Khalifa/Dubai Mall 5, 6 2 AED 5.00 AED 10.00
BurJuman to Mall of the Emirates 6, 2 2 AED 5.00 AED 10.00
Airport Terminal 3 to Sobha Realty 5, 6, 2 3 AED 7.50 AED 15.00
Dubai Investment Park to Al Furjan 1, 3, 2 3 AED 7.50 AED 15.00
Creek to Expo 2020 6, 2, 3, 1 4 (tier 3) AED 7.50 AED 15.00

The last two rows are the ones a subtract-the-zone-numbers approach gets wrong. Dubai Investment Park and Al Furjan are two stops apart, yet the path passes through the zone 3 pocket, so it is a three-zone fare. Creek to Expo 2020 passes through four zones and simply lands in the top tier.

Step 4: a form that still works if the script fails

The user-facing part is a plain form: two drop-downs for the origin and destination stations, a third for the card type, a button, and an output area. Populate the two station drop-downs from the same station list the calculator uses, so the data lives in one place and a rename changes the form as well as the logic. On submit, run the three steps above and write the result as a sentence, for example the number of zones crossed, the number of stops, and the fare to two decimal places. Then make sure the fare tier table sits on the page above the form as ordinary content. If the script fails to load, the page degrades to a lookup table rather than to nothing, which is the progressive-enhancement bargain and costs nothing to keep.

Two production notes. Interchanges are free on this network and happen inside the paid area, so the model needs no transfer penalty; if you port this to a system that charges per line, add a cost to any step that changes line. And station names change on this network (well over a dozen renames since it opened), so keep names in one data file and reference them by a stable identifier in anything you store.

The same pattern, a different rule: date-based tiers

The structure above (inputs, a tiered rule, a rendered breakdown) is the whole design, and swapping the rule makes it do something else entirely. The same pattern powers this UAE gratuity calculator, which applies 21 days of basic pay per year of service for the first five years and 30 days a year after that. The tiers there are years rather than zones, the daily wage is the basic salary divided by 30, and the law caps the total at two years of pay.

Length of service Rate Worked example: AED 10,000 basic, 7.5 years
Under 1 year No gratuity Not applicable
First 5 years 21 days of basic pay per year 5 years times 21 days times AED 333.33 a day = AED 35,000
Beyond 5 years 30 days of basic pay per year 2.5 years times 30 days times AED 333.33 a day = about AED 25,000
Cap Two years of pay AED 240,000, not reached; total about AED 60,000

Notice what changed and what did not. The rule changed completely; the form, the validation and the render step are the same as the fare calculator's. When you find yourself building a third calculator, that is the moment to pull the skeleton into a small helper that takes a rule and a renderer, and stop there. It does not need a framework.

Where to take it

  • Add a daily cap or a travel pass: compute per-journey fares as above, then apply a running total per calendar day.
  • Precompute an all-pairs zone matrix at build time if you are serving the calculator from a static page; 53 stations means 2,809 entries, small enough to inline.
  • Show the route, not just the price. Passengers trust a fare more when they can see the stations it was computed from.

Fares and zone assignments as published by Dubai's RTA and checked in September 2026.

Related articles
What Actually Separates a Good Website From a Forgettable One
15 Sep, 2026
  • Estimated reading time: 5 Minutes
Why AI Model Choice Matters for Anime Art
15 Sep, 2026
  • Estimated reading time: 5 Minutes
When There's No Storefront to Point a Map To
15 Sep, 2026
  • Estimated reading time: 4 Minutes
A Practical Workflow for Turning Product Assets into AI Video Demos
15 Sep, 2026
  • Estimated reading time: 7 Minutes
All-in-One DTF Bundle: What a Complete Production Setup Should Solve
15 Sep, 2026
  • Estimated reading time: 4 Minutes
Top 8 Container Monitoring Tools in 2026
15 Sep, 2026
  • Estimated reading time: 7 Minutes
Weekly trending
What Actually Separates a Good Website From a Forgettable One
15 Sep, 2026
  • Estimated reading time: 5 Minutes
Why AI Model Choice Matters for Anime Art
15 Sep, 2026
  • Estimated reading time: 5 Minutes
When There's No Storefront to Point a Map To
15 Sep, 2026
  • Estimated reading time: 4 Minutes
A Practical Workflow for Turning Product Assets into AI Video Demos
15 Sep, 2026
  • Estimated reading time: 7 Minutes
All-in-One DTF Bundle: What a Complete Production Setup Should Solve
15 Sep, 2026
  • Estimated reading time: 4 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.