Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rides #3

Merged
merged 13 commits into from
Jan 21, 2025
29 changes: 29 additions & 0 deletions lib/ride.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# Create a ride for the carnival
class Ride
attr_reader :name, :min_height, :admission_fee, :excitement, :total_revenue, :rider_log

def initialize(ride_params)
@name = ride_params[:name]
@min_height = ride_params[:min_height]
@admission_fee = ride_params[:admission_fee]
@excitement = ride_params[:excitement]
@total_revenue = 0
@rider_log = {}
end

def board_rider(visitor)
return unless visitor.tall_enough?(@min_height) &&
visitor.spending_money >= @admission_fee &&
visitor.preferences.include?(@excitement)

if @rider_log[visitor]
@rider_log[visitor] += 1
else
@rider_log[visitor] = 1
end
@total_revenue += @admission_fee
visitor.spend_money(@admission_fee)
end
end
4 changes: 4 additions & 0 deletions lib/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ def add_preference(pref)
def tall_enough?(minimum_height)
@height > minimum_height
end

def spend_money(amount)
@spending_money -= amount
end
end
93 changes: 93 additions & 0 deletions spec/ride_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# frozen_string_literal: true

require_relative 'spec_helper'

RSpec.describe Ride do
subject(:first_ride) do
described_class.new({ name: 'Carousel', min_height: 24, admission_fee: 1, excitement: :gentle })
end

let(:second_ride) do
described_class.new({ name: 'Ferris Wheel', min_height: 36, admission_fee: 5, excitement: :gentle })
end
let(:third_ride) do
described_class.new({ name: 'Roller Coaster', min_height: 54, admission_fee: 2, excitement: :thrilling })
end

let(:first_visitor) { Visitor.new('Bruce', 54, '$10') }
let(:second_visitor) { Visitor.new('Tucker', 36, '$5') }
let(:third_visitor) { Visitor.new('Penny', 64, '$15') }

describe '#initialize' do
it { is_expected.to be_instance_of described_class }

it 'has a name' do
expect(first_ride.name).to eq('Carousel')
end

it 'has a minimum height' do
expect(first_ride.min_height).to eq(24)
end

it 'has an admission fee' do
expect(first_ride.admission_fee).to eq(1)
end

it 'has an excitement level' do
expect(first_ride.excitement).to eq(:gentle)
end

it 'has no revenue' do
expect(first_ride.total_revenue).to eq(0)
end

it 'has empty rider log' do
expect(first_ride.rider_log).to eq({})
end
end

describe '#board_rider' do
context 'when rider is not eligible to ride' do
it 'rejects rider who is not tall enough' do
second_visitor.add_preference(:thrilling)

expect(third_ride.board_rider(second_visitor)).to be_nil
end

it 'rejects rider who cannot afford ride' do
first_visitor.add_preference(:gentle)
first_visitor.spend_money(10)

expect(first_ride.board_rider(first_visitor)).to be_nil
end

it 'rejects rider who does not have matching preference' do
first_visitor.add_preference(:thrilling)

expect(first_ride.board_rider(first_visitor)).to be_nil
end
end

context 'when rider is eligible to ride' do
it 'can board rider' do
first_visitor.add_preference(:gentle)

expect(first_ride.board_rider(first_visitor)).not_to be_nil
end
end
end

describe '#rider_log' do
before do
first_visitor.add_preference(:gentle)
second_visitor.add_preference(:gentle)
first_ride.board_rider(first_visitor)
first_ride.board_rider(second_visitor)
first_ride.board_rider(first_visitor)
end

it 'returns hash of riders with amount of times ridden' do
expect(first_ride.rider_log).to eq({ first_visitor => 2, second_visitor => 1 })
end
end
end
6 changes: 6 additions & 0 deletions spec/visitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@
it { is_expected.to be true }
end
end

describe '#spend_money' do
it 'can spend money' do
expect(first_visitor.spend_money(2)).to eq(8)
end
end
end