diff --git a/lib/.gitkeep b/lib/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/lib/bike_club.rb b/lib/bike_club.rb index b83c6db..dec8db8 100644 --- a/lib/bike_club.rb +++ b/lib/bike_club.rb @@ -1,3 +1,25 @@ class BikeClub + attr_reader :name, :bikers + def initialize(name) + @name = name + @bikers = [] + end + + def add_biker(biker) + @bikers << biker + end + + def most_rides + most = @bikers[0] + + @bikers.each do |biker| + current_most_rides = most.rides.values.flatten.count + num_of_rides = biker.rides.values.flatten.count + + most = biker if num_of_rides > current_most_rides + end + + most + end end \ No newline at end of file diff --git a/lib/biker.rb b/lib/biker.rb index db234de..90565e9 100644 --- a/lib/biker.rb +++ b/lib/biker.rb @@ -1,3 +1,40 @@ +require './spec/spec_helper' + class Biker + attr_reader :name, :max_distance, :acceptable_terrain, :rides + + def initialize(name, max_distance) + @name = name + @max_distance = max_distance + @acceptable_terrain = [] + @rides = Hash.new { |hash, key| hash[key] = [] } + end + + def learn_terrain!(terrain) + @acceptable_terrain << terrain + end + + def log_ride(ride, time) + if @max_distance >= ride.total_distance && @acceptable_terrain.include?(ride.terrain) + @rides[ride] << time + end + end + + def personal_record(ride) + best_time = false + + if @rides[ride].empty? + return best_time + else + @rides[ride].each do |time| + if best_time == false + best_time = time + else + best_time = time if time < best_time + end + end + end + best_time + end end \ No newline at end of file diff --git a/lib/ride.rb b/lib/ride.rb index 1be20f0..4c689dc 100644 --- a/lib/ride.rb +++ b/lib/ride.rb @@ -1,3 +1,22 @@ class Ride + attr_reader :name, :distance, :terrain + def initialize(ride_info) + @name = ride_info[:name] + @distance = ride_info[:distance] + @loop = ride_info[:loop] + @terrain = ride_info[:terrain] + end + + def loop? + @loop + end + + def total_distance + if @loop + @distance + else + @distance * 2.0 + end + end end \ No newline at end of file diff --git a/spec/.gitkeep b/spec/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/spec/bike_club_spec.rb b/spec/bike_club_spec.rb new file mode 100644 index 0000000..ff1fd14 --- /dev/null +++ b/spec/bike_club_spec.rb @@ -0,0 +1,64 @@ +require_relative 'spec_helper' + +RSpec.describe BikeClub do + describe '#initialize' do + it 'exists' do + bike_club = BikeClub.new("Hell's Angels") + expect(bike_club).to be_an_instance_of(BikeClub) + end + + it 'has attributes' do + bike_club = BikeClub.new("Hell's Angels") + expect(bike_club.name).to eq("Hell's Angels") + end + + it 'can have different attributes' do + bike_club2 = BikeClub.new("Rolling Thunder") + expect(bike_club2.name).to eq("Rolling Thunder") + end + + it 'has a default attribute' do + bike_club = BikeClub.new("Hell's Angels") + bike_club2 = BikeClub.new("Rolling Thunder") + + expect(bike_club.bikers).to eq([]) + expect(bike_club2.bikers).to eq([]) + end + end + + describe '#add_biker' do + it 'can add a biker to its bikers list' do + bike_club = BikeClub.new("Hell's Angels") + biker = Biker.new("Kenny", 30) + biker2 = Biker.new("Athena", 15) + + expect(bike_club.bikers).to eq([]) + bike_club.add_biker(biker) + bike_club.add_biker(biker2) + expect(bike_club.bikers).to eq([biker, biker2]) + end + end + + describe '#most_rides' do + it 'returns the biker with the most rides' do + bike_club = BikeClub.new("Hell's Angels") + biker = Biker.new("Kenny", 30) + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + bike_club.add_biker(biker) + bike_club.add_biker(biker2) + + biker.learn_terrain!(:hills) + biker.learn_terrain!(:gravel) + biker2.learn_terrain!(:gravel) + + biker.log_ride(ride1, 91.2) + biker.log_ride(ride2, 60.6) + biker2.log_ride(ride2, 59.7) + + expect(bike_club.most_rides).to eq(biker) + end + end +end \ No newline at end of file diff --git a/spec/biker_spec.rb b/spec/biker_spec.rb new file mode 100644 index 0000000..240e451 --- /dev/null +++ b/spec/biker_spec.rb @@ -0,0 +1,114 @@ +require_relative 'spec_helper' + +RSpec.describe Biker do + describe '#initialize' do + it 'exists' do + biker = Biker.new("Kenny", 30) + expect(biker).to be_an_instance_of(Biker) + end + + it 'has attributes' do + biker = Biker.new("Kenny", 30) + + expect(biker.name).to eq("Kenny") + expect(biker.max_distance).to eq(30) + expect(biker.rides).to eq({}) + expect(biker.acceptable_terrain).to eq([]) + end + + it 'can have some different attributes' do + biker2 = Biker.new("Athena", 15) + + expect(biker2.name).to eq("Athena") + expect(biker2.max_distance).to eq(15) + expect(biker2.rides).to eq({}) + expect(biker2.acceptable_terrain).to eq([]) + end + end + + describe '#learn_terrain!' do + it 'adds a new terrain to the bikers acceptable terrains' do + biker = Biker.new("Kenny", 30) + + expect(biker.acceptable_terrain).to eq([]) + biker.learn_terrain!(:gravel) + expect(biker.acceptable_terrain).to eq([:gravel]) + biker.learn_terrain!(:hills) + expect(biker.acceptable_terrain).to eq([:gravel, :hills]) + end + end + + describe '#log_ride' do + context 'each unique ride is a key, with all the times for each ride being the values' do + it 'saves a bikers ride and time in the rides attribute' do + biker = Biker.new("Kenny", 30) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker.learn_terrain!(:gravel) + biker.learn_terrain!(:hills) + + biker.log_ride(ride1, 92.5) + biker.log_ride(ride1, 91.1) + biker.log_ride(ride2, 60.9) + biker.log_ride(ride2, 61.6) + + expect(biker.rides.keys).to contain_exactly(ride1, ride2) + expect(biker.rides[ride1]).to contain_exactly(92.5, 91.1) + expect(biker.rides[ride2]).to contain_exactly(60.9, 61.6) + end + + it 'will not log if rides terrain is not accepted by biker' do + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker2.log_ride(ride1, 97.0) + biker2.log_ride(ride2, 67.0) + + expect(biker2.rides).to eq({}) + end + + it 'will not log if rides total distance is higher than bikers max_distance' do + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker2.learn_terrain!(:gravel) + biker2.learn_terrain!(:hills) + + biker2.log_ride(ride1, 95.0) + biker2.log_ride(ride2, 65.0) + + expect(biker2.rides).to eq({ride2 => [65.0]}) + end + end + end + + describe '#personal_record' do + context 'returns false if biker has no saved time for a ride' do + it 'returns a bikers best ride time for a specific ride' do + biker = Biker.new("Kenny", 30) + biker2 = Biker.new("Athena", 15) + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + biker.learn_terrain!(:gravel) + biker.learn_terrain!(:hills) + biker2.learn_terrain!(:gravel) + biker2.learn_terrain!(:hills) + + biker.log_ride(ride1, 92.5) + biker.log_ride(ride1, 91.1) + biker.log_ride(ride2, 60.9) + biker.log_ride(ride2, 61.6) + biker2.log_ride(ride2, 65.0) + + expect(biker.personal_record(ride1)).to eq(91.1) + expect(biker.personal_record(ride2)).to eq(60.9) + expect(biker2.personal_record(ride1)).to be false + expect(biker2.personal_record(ride2)).to eq(65.0) + end + end + end +end \ No newline at end of file diff --git a/spec/ride_spec.rb b/spec/ride_spec.rb new file mode 100644 index 0000000..0b59496 --- /dev/null +++ b/spec/ride_spec.rb @@ -0,0 +1,54 @@ +require_relative 'spec_helper' + +RSpec.describe Ride do + describe '#initialize' do + it 'exists' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + expect(ride1).to be_an_instance_of(Ride) + end + + it 'has attributes' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + + expect(ride1.name).to eq("Walnut Creek Trail") + expect(ride1.distance).to eq(10.7) + expect(ride1.terrain).to eq(:hills) + end + + it 'can have different attributes' do + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + expect(ride2.name).to eq("Town Lake") + expect(ride2.distance).to eq(14.9) + expect(ride2.terrain).to eq(:gravel) + end + end + + describe '#loop?' do + it 'returns the value of the Rides loop attribute' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + expect(ride1.loop?).to be false + expect(ride2.loop?).to be true + end + end + + describe '#total_distance' do + it 'returns the base distance attribute if loop is true' do + ride2 = Ride.new({name: "Town Lake", distance: 14.9, loop: true, terrain: :gravel}) + + expect(ride2.distance).to eq(14.9) + expect(ride2.loop?).to be true + expect(ride2.total_distance).to eq(14.9) + end + + it 'returns distance times two if loop is false' do + ride1 = Ride.new({name: "Walnut Creek Trail", distance: 10.7, loop: false, terrain: :hills}) + + expect(ride1.distance).to eq(10.7) + expect(ride1.loop?).to be false + expect(ride1.total_distance).to eq(21.4) + end + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..addb0ec --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,6 @@ +# require 'simplecov';SimpleCov.start +require 'pry' +require 'rspec' +require './lib/ride' +require './lib/biker' +require './lib/bike_club' \ No newline at end of file