Skip to content

Instantly share code, notes, and snippets.

@leoallen85
Created November 19, 2015 16:00
Show Gist options
  • Save leoallen85/acc6b25949b5cc4a2ea1 to your computer and use it in GitHub Desktop.
Save leoallen85/acc6b25949b5cc4a2ea1 to your computer and use it in GitHub Desktop.
require "./lib/journey_log"
class Oystercard
MAXIMUM_BALANCE = 90
MINIMUM_BALANCE = 1
attr_reader :balance, :journey, :journey_log
def initialize(journey_log: JourneyLog.new)
@balance = 0
@journey_log = journey_log
end
def top_up(amount_received)
raise "Over limit of £#{MAXIMUM_BALANCE}" if above_maximum_balance?(amount_received)
self.balance += amount_received
end
def touch_in(journey)
raise "Too low funds" if below_minimum_balance?
journey_log.start_journey(journey)
end
def touch_out(exit_station)
journey_log.end_journey(exit_station)
deduct(current_journey.fare)
end
private
attr_writer :balance, :journey_log, :current_journey
attr_reader :journey
def deduct(amount_deducted)
self.balance -= amount_deducted
end
def below_minimum_balance?
balance < MINIMUM_BALANCE
end
def above_maximum_balance?(amount_received)
balance + amount_received > MAXIMUM_BALANCE
end
def current_journey
journey_log.current_journey
end
end
class Oystercard
def initialize(balance: BalanceCalculator.new, log: JourneyLog.new)
@balance = balance
@journey_log = log
end
def touch_in(station)
balance.verify
journey_log.start_journey(station)
end
def top_up(balance)
balance.top_up(balance)
end
def touch_out(station)
journey = journey_log.end_journey(station)
balance.adjust(journey)
end
end
class JourneyLog
def initialize
@journeys = []
end
def current_journey
@journeys.current
end
def start_journey(station)
already_in_journey?
{start_journey: station, end_journey: station}
end
def already_in_journey?
previous_journey_finished?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment