Design a train route fare calculator for one linear route.
Stops are added to the route in travel order. Travelling across each segment between two neighboring stops costs 1.
On weekends, the fare for a journey cannot exceed the configured weekend fare cap.
1.startStop and endStop.weekendFareCap.0.calculateFare.TrainRouteFareCalculator
TrainRouteFareCalculator(int weekendFareCap)
weekendFareCap is the maximum fare charged for one weekend journey.void addStop(String stopName)
stopName to the end of the route.addStop calls determines the order of the stops.int calculateFare(String startStop, String endStop, boolean isWeekend)
startStop is the stop where the journey begins.endStop is the stop where the journey ends.isWeekend is true for a weekend journey and false for a weekday journey.1 ≤ weekendFareCap ≤ 100,0001 ≤ stopName.length() ≤ 100100,000 stops are added.100,000 fare calculations are requested.startStop and endStop passed to calculateFare has already been added.null. TrainRouteFareCalculator calculator = new TrainRouteFareCalculator(weekendFareCap = 3);
calculator.addStop(stopName = "Cedar");
calculator.addStop(stopName = "Lake");
calculator.addStop(stopName = "Museum");
calculator.addStop(stopName = "Market");
calculator.addStop(stopName = "Airport");
calculator.addStop(stopName = "Harbor");
calculator.calculateFare( startStop = "Cedar", endStop = "Harbor", isWeekend = false )
Output: 5
The journey crosses five segments. Because it is a weekday journey, the weekend cap does not apply.
Using the same calculator and route:
calculator.calculateFare( startStop = "Cedar", endStop = "Harbor", isWeekend = true )
Output: 3
The normal fare is 5, but weekend fares are capped at 3.
Using the same calculator and route:
calculator.calculateFare( startStop = "Market", endStop = "Lake", isWeekend = true )
Output: 2
The stops are two segments apart. The normal fare is already below the weekend cap.
Using the same calculator and route:
calculator.calculateFare( startStop = "Museum", endStop = "Museum", isWeekend = false )
Output: 0
No segment is travelled when both stops are the same.