From 375a17c5db1fde536e34640022a1555cf3b82ee2 Mon Sep 17 00:00:00 2001 From: Jackson Bickler Date: Mon, 4 Nov 2024 11:53:34 -0700 Subject: [PATCH] bike_club_challenge --- lib/biker.rb | 38 ++++++++++++++++++++++++++ lib/ride.rb | 20 ++++++++++++++ spec/biker_spec.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++ spec/ride_spec.rb | 20 ++++++++++++++ spec/spec_helper.rb | 2 ++ 5 files changed, 145 insertions(+) create mode 100644 spec/biker_spec.rb create mode 100644 spec/ride_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/lib/biker.rb b/lib/biker.rb index db234de..32e3a25 100644 --- a/lib/biker.rb +++ b/lib/biker.rb @@ -1,3 +1,41 @@ +require 'spec_helper' + class Biker + attr_reader :name, :max_distance, :rides, :acceptable_terrain + + def initialize(name, max_distance) + @name = name + @max_distance = max_distance + @rides = {} + @acceptable_terrain = [] + end + + def learn_terrain!(terrain) + @acceptable_terrain << terrain + end + + def log_ride(ride, distance) #i cant do it. no matter what i do the tests fail. if i add the last condtion (if they know the terrain but cant do the distance) it breaks the whole thing + #i understand that the condition right above it is overiding it but no matter what order i put it in it doesnt work. i dont know how to have all three conditions and make it work + if !@acceptable_terrain.include?(ride.terrain) + return "#{@name} dosent know this terrain" + elsif ride.total_distance > @max_distance + return "#{@name} can't bike this distance" + elsif @acceptable_terrain.include?(ride.terrain) && ride.total_distance <= @max_distance + return "#{@name} knows the terrain but cannot bike the distance" + else + if @rides.key?(ride) + @rides[ride] << distance + else + @rides[ride] = [distance] + end + end + end + def personal_record(ride) + if @rides.key?(ride) + @rides[ride].min + else + false + end + end end \ No newline at end of file diff --git a/lib/ride.rb b/lib/ride.rb index 1be20f0..d55eb29 100644 --- a/lib/ride.rb +++ b/lib/ride.rb @@ -1,3 +1,23 @@ class Ride + attr_reader :name, :distance, :loop, :terrain, :total_distance + + def initialize(info_hash) + @name = info_hash[:name] + @distance = info_hash[:distance] + @loop = info_hash[:loop] + @terrain = info_hash[:terrain] + end + + def loop? + @loop + end + + def total_distance + if @loop == false + @distance * 2 + else + @distance + 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..46d1e2a --- /dev/null +++ b/spec/biker_spec.rb @@ -0,0 +1,65 @@ +require 'spec_helper' + +RSpec.describe Biker do + before(:each) 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}) + end + + describe 'intiailize' do + it "exists and has attributes" do + 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 + end + + describe "learn_terrain!" do + it "adds a terrain to the acceptable terrain array" do + @biker.learn_terrain!(:gravel) + @biker.learn_terrain!(:hills) + @biker2.learn_terrain!(:gravel) + @biker2.learn_terrain!(:hills) + expect(@biker.acceptable_terrain).to eq([:gravel, :hills]) + + end + end + + describe "log_ride" do + it "logs rides into the rides hash" do + @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(@biker2.log_ride(@ride1, 97.0)).to eq("Athena dosent know this terrain") + expect(@biker2.rides).to eq({}) + @biker2.learn_terrain!(:gravel) + @biker2.learn_terrain!(:hills) + expect(@biker2.log_ride(@ride1, 95.0)).to eq("Athena can't bike this distance") + expect(@biker2.log_ride(@ride2, 65.0)).to eq("Athena knows the terrain but cannot bike the distance") + expect(@biker.rides).to include(@ride1) + expect(@biker.rides[@ride1]).to eq([92.5, 91.1]) + end + end + + describe "personal_record" do + it "displays the bikers personal record dpedning on the specific ride" do + @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.personal_record(@ride1)).to eq(91.1) + expect(@biker.personal_record(@ride2)).to eq(60.9) + end + end + + describe + +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..8d29f49 --- /dev/null +++ b/spec/ride_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +RSpec.describe Ride do + before(:each) 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}) + end + + describe 'initialize' do + it "exsits with attributes" do + expect(@ride1.name).to eq("Walnut Creek Trail") + expect(@ride1.distance).to eq(10.7) + expect(@ride1.loop?).to eq(false) + expect(@ride1.terrain).to eq(:hills) + expect(@ride1.total_distance).to eq(21.4) + expect(@ride2.loop?).to eq(true) + expect(@ride2.total_distance).to eq(14.9) + 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..236940e --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,2 @@ +require './lib/ride' +require './lib/biker' \ No newline at end of file