Skip to content

Instantly share code, notes, and snippets.

@stephenrichards
Last active February 12, 2019 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenrichards/7f2aec262e9e6884d15a5b5720494fc6 to your computer and use it in GitHub Desktop.
Save stephenrichards/7f2aec262e9e6884d15a5b5720494fc6 to your computer and use it in GitHub Desktop.
# Below are two classes -FareCalculator and OysterCard calculator.
# Currently, the fare calculator simply looks up the fare based on the starting zone and ending zone
# and returns the result.
#
# For this exercise, add procs to the ZONE_TO_ZONE_FARES so that if the journey is from zone 3 to 2 and the oyster card has a
# season ticket, then the fare is 45 and the fare for zone 1 to 1 in they have an incomplete journey on the card, the maximum fare is charged
class FareCalculator
MAXIMUM_FARE = 666
ZONE_TO_ZONE_FARES = {
1 => {
1 => 110,
2 => 120,
3 => 130
},
2 => {
1 => 120,
2 => 220,
3 => 230
},
3 => {
1 => 130,
2 => 230,
3 => 330
}
}
def self.calculate(oyster, starting_zone, ending_zone)
ZONE_TO_ZONE_FARES[starting_zone][ending_zone]
end
end
class OysterCard
def initialize(season_ticket: false, incomplete_journey: false)
@season_ticket = season_ticket
@incomplete_journey = incomplete_journey
end
def season_ticket?
@season_ticket
end
def incomplete_journey?
@incomplete_journey
end
def calculate_fare(starting_zone, ending_zone)
FareCalculator.calculate(self, starting_zone, ending_zone)
end
end
######### Expected results ###########################
o1 = OysterCard.new
puts o1.calculate_fare(3, 2) # => 230
o2 = OysterCard.new(season_ticket: true)
puts o2.calculate_fare(3, 2) # => 45
o3 = OysterCard.new(incomplete_journey: true)
puts o3.calculate_fare(3, 2) # => 666
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment