From 979e9bd5fab8e9610808afa47ba168f007602a06 Mon Sep 17 00:00:00 2001 From: Hannah Gray Date: Wed, 18 Dec 2024 10:12:13 -0700 Subject: [PATCH 1/5] Add Passenger class and RSpec tests --- .DS_Store | Bin 0 -> 8196 bytes lib/passenger.rb | 28 ++++++++++++++++++++++++++++ spec/passenger_spec.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 .DS_Store create mode 100644 spec/passenger_spec.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..90a62d73adff89e3d22feb819c1adcc8fb6e0b9b GIT binary patch literal 8196 zcmeHMyKWOf6ulFhWUUCvMUY&iScx`DY$rlQP&mdZC}^?>3P8r|wd@t^osA!HVOequ z6jW5Sd<0*RPoRLG;1A%;BkNt;p)d#`XC|6^X6HUUbLM(?uR}zvI`m3J1tPN01?IOQ zmNcOkeW7jWnH?a5Kk=p;a2Hqk1zR&=7%&VN1`Gp+0mH!m!2q7wqFL8G_m$R2!+>Gn zMlvAo4>r2MipHrzdFz0XB>*ssY1uHAFaJPWOaLnyrwTEH#*`~ixiVd1FkL$^wjxE{B-{D?d8eLugksPXrsnMelvvT!(VLU#(U)-~gj*F8$dwg6Ljf%VRL& HhB5F984Xl| literal 0 HcmV?d00001 diff --git a/lib/passenger.rb b/lib/passenger.rb index e69de29..8ef0e5a 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -0,0 +1,28 @@ +# lib/passenger.rb + +class Passenger + attr_reader :name, :age + + # Initialize a Passenger with a hash containing name and age + def initialize(details) + @name = details["name"] + @age = details["age"] + @driver = false # Default driver status is false + end + + # Returns true if the passenger is 18 or older + def adult? + @age >= 18 + end + + # Returns the current driver status + def driver? + @driver + end + + # Updates the driver status to true + def drive + @driver = true + end +end + diff --git a/spec/passenger_spec.rb b/spec/passenger_spec.rb new file mode 100644 index 0000000..b5c76e2 --- /dev/null +++ b/spec/passenger_spec.rb @@ -0,0 +1,32 @@ +# spec/passenger_spec.rb + +require_relative '../lib/passenger' + +RSpec.describe Passenger do + describe 'Initialization' do + it 'exists and has attributes' do + charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + + expect(charlie.name).to eq("Charlie") + expect(charlie.age).to eq(18) + end + end + + describe 'Methods' do + it 'can determine if a passenger is an adult' do + charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + taylor = Passenger.new({"name" => "Taylor", "age" => 12}) + + expect(charlie.adult?).to eq(true) + expect(taylor.adult?).to eq(false) + end + + it 'can check and update driver status' do + charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + + expect(charlie.driver?).to eq(false) + charlie.drive + expect(charlie.driver?).to eq(true) + end + end +end From 0fea9bda2faba12143078275e58c43dc1f433bc5 Mon Sep 17 00:00:00 2001 From: Hannah Gray Date: Wed, 18 Dec 2024 10:32:42 -0700 Subject: [PATCH 2/5] Complete Vehicle class and RSpec tests --- .DS_Store | Bin 8196 -> 6148 bytes .rspec | 1 + lib/vehicle.rb | 21 +++++++++++++++++++++ spec/.keep | 0 spec/vehicle_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 .rspec delete mode 100644 spec/.keep create mode 100644 spec/vehicle_spec.rb diff --git a/.DS_Store b/.DS_Store index 90a62d73adff89e3d22feb819c1adcc8fb6e0b9b..529d84d7caecdc96c582920686795d496d29586c 100644 GIT binary patch delta 104 zcmZp1XfcprU|?W$DortDU=RQ@Ie-{Mvv5r;6q~50$jG}fU^g=(?_?fw&zl? zOy*&kSuev-z>vsL41}o+c?{`5vIxkI2a4q}qyXtMphyNoCPOkq4v+K1|rQB!Zdg?2kUY2ot2Yr7@VA+TL5+hLq8kwPC<4_ zJ;aZ>`7SO{KXN4J&n}$Wc?{%FBK?MJAk4fJ;>@cjG7<_hkbKMd0pw}29ak1yl$VpA zmku<8aq}XUROXEh8jOO>ATxn!i5p0`g3{AwL5}atlletFC;Rhoa4 "Charlie", "age" => 18}) + @jude = Passenger.new({"name" => "Jude", "age" => 12}) + @taylor = Passenger.new({"name" => "Taylor", "age" => 25}) + end + + it 'exists and has attributes' do + # Verify the vehicle's attributes. + expect(@vehicle).to be_a(Vehicle) + expect(@vehicle.year).to eq("2001") + expect(@vehicle.make).to eq("Honda") + expect(@vehicle.model).to eq("Civic") + expect(@vehicle.passengers).to eq([]) + end + + it 'can add passengers' do + # Add passengers to the vehicle. + @vehicle.add_passenger(@charlie) + expect(@vehicle.passengers).to eq([@charlie]) + + @vehicle.add_passenger(@jude) + expect(@vehicle.passengers).to eq([@charlie, @jude]) + end + + it 'can count the number of adults' do + # Add passengers and count the number of adults. + @vehicle.add_passenger(@charlie) + @vehicle.add_passenger(@jude) + @vehicle.add_passenger(@taylor) + + expect(@vehicle.num_adults).to eq(2) # Charlie and Taylor are adults. + end +end From 35d3aaa548ca9f4d5b774776b9134c6efe0c6619 Mon Sep 17 00:00:00 2001 From: Hannah Gray <110919054+hanbanfan@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:35:06 -0700 Subject: [PATCH 3/5] Delete .rspec --- .rspec | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .rspec diff --git a/.rspec b/.rspec deleted file mode 100644 index 49d5710..0000000 --- a/.rspec +++ /dev/null @@ -1 +0,0 @@ ---format documentation From 5d02772c48cbe031b3754e3ace69b8830d7a18e7 Mon Sep 17 00:00:00 2001 From: Hannah Gray Date: Wed, 18 Dec 2024 10:52:40 -0700 Subject: [PATCH 4/5] Complete Iteration 3 with Park class and tests --- lib/park.rb | 29 ++++++++++++++++++++++++ lib/passenger.rb | 6 +---- lib/vehicle.rb | 15 ++++++------- spec/park_spec.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 spec/park_spec.rb diff --git a/lib/park.rb b/lib/park.rb index e69de29..b6c61d8 100644 --- a/lib/park.rb +++ b/lib/park.rb @@ -0,0 +1,29 @@ +# lib/park.rb + +# The Park class represents a national park with vehicles, passengers, and revenue tracking. +class Park + attr_reader :name, :price, :vehicles + + # Initialize a Park with a name, admission price, and an empty list of vehicles. + def initialize(name, price) + @name = name # The unique name of the park. + @price = price # The admission price per adult. + @vehicles = [] # A list to store vehicles that enter the park. + end + + # Add a vehicle to the park's vehicle list. + def add_vehicle(vehicle) + @vehicles << vehicle + end + + # List all passengers in all vehicles that entered the park. + def passengers + @vehicles.flat_map(&:passengers) + end + + # Calculate revenue generated by charging the admission price for each adult passenger. + def revenue + num_adults = passengers.count(&:adult?) # Count only adult passengers. + num_adults * @price # Multiply by the park's admission price. + end +end diff --git a/lib/passenger.rb b/lib/passenger.rb index 8ef0e5a..dcf9b71 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -3,24 +3,20 @@ class Passenger attr_reader :name, :age - # Initialize a Passenger with a hash containing name and age def initialize(details) @name = details["name"] @age = details["age"] - @driver = false # Default driver status is false + @driver = false end - # Returns true if the passenger is 18 or older def adult? @age >= 18 end - # Returns the current driver status def driver? @driver end - # Updates the driver status to true def drive @driver = true end diff --git a/lib/vehicle.rb b/lib/vehicle.rb index b4bcb39..f5cb53a 100644 --- a/lib/vehicle.rb +++ b/lib/vehicle.rb @@ -1,21 +1,20 @@ +# lib/vehicle.rb + class Vehicle attr_reader :year, :make, :model, :passengers - # Initialize a Vehicle with year, make, model, and an empty list of passengers. def initialize(year, make, model) - @year = year # The manufacturing year of the vehicle. - @make = make # The make (brand) of the vehicle, e.g., Honda. - @model = model # The specific model of the vehicle, e.g., Civic. - @passengers = [] # An array to hold passengers. + @year = year + @make = make + @model = model + @passengers = [] end - # Add a passenger to the vehicle's passenger list. def add_passenger(passenger) @passengers << passenger end - # Count the number of adult passengers in the vehicle. def num_adults - @passengers.count { |passenger| passenger.adult? } + @passengers.count(&:adult?) end end diff --git a/spec/park_spec.rb b/spec/park_spec.rb new file mode 100644 index 0000000..57cee5b --- /dev/null +++ b/spec/park_spec.rb @@ -0,0 +1,56 @@ +# spec/park_spec.rb + +require_relative '../lib/park' +require_relative '../lib/vehicle' +require_relative '../lib/passenger' + +RSpec.describe Park do + before(:each) do + # Create a park and associated test data. + @park = Park.new("Yellowstone", 50) + @vehicle1 = Vehicle.new("2001", "Honda", "Civic") + @vehicle2 = Vehicle.new("2010", "Toyota", "Corolla") + @charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + @jude = Passenger.new({"name" => "Jude", "age" => 12}) + @taylor = Passenger.new({"name" => "Taylor", "age" => 25}) + + # Add passengers to vehicles. + @vehicle1.add_passenger(@charlie) + @vehicle1.add_passenger(@jude) + @vehicle2.add_passenger(@taylor) + end + + it 'exists and has attributes' do + # Test park attributes. + expect(@park).to be_a(Park) + expect(@park.name).to eq("Yellowstone") + expect(@park.price).to eq(50) + expect(@park.vehicles).to eq([]) + end + + it 'can add vehicles' do + # Add vehicles to the park. + @park.add_vehicle(@vehicle1) + expect(@park.vehicles).to eq([@vehicle1]) + + @park.add_vehicle(@vehicle2) + expect(@park.vehicles).to eq([@vehicle1, @vehicle2]) + end + + it 'can list all passengers' do + # Add vehicles to the park and list all passengers. + @park.add_vehicle(@vehicle1) + @park.add_vehicle(@vehicle2) + + expect(@park.passengers).to eq([@charlie, @jude, @taylor]) + end + + it 'can calculate revenue generated' do + # Add vehicles to the park and calculate revenue from adult passengers. + @park.add_vehicle(@vehicle1) + @park.add_vehicle(@vehicle2) + + # Only Charlie and Taylor are adults, so 2 * $50 = $100. + expect(@park.revenue).to eq(100) + end +end From 8fbc0a7f4bd38401efc34bad7ef5dd9f84d4a92b Mon Sep 17 00:00:00 2001 From: Hannah Gray Date: Wed, 18 Dec 2024 11:30:55 -0700 Subject: [PATCH 5/5] Complete Iteration 4 with Park class and tests --- lib/park.rb | 32 +++++++++++++++++++++++--------- lib/passenger.rb | 12 ++++++++---- lib/vehicle.rb | 14 +++++++++----- spec/park_spec.rb | 35 ++++++++++++++++++++--------------- spec/passenger_spec.rb | 37 ++++++++++++++----------------------- spec/vehicle_spec.rb | 22 +++++++--------------- 6 files changed, 81 insertions(+), 71 deletions(-) diff --git a/lib/park.rb b/lib/park.rb index b6c61d8..c33b870 100644 --- a/lib/park.rb +++ b/lib/park.rb @@ -4,26 +4,40 @@ class Park attr_reader :name, :price, :vehicles - # Initialize a Park with a name, admission price, and an empty list of vehicles. + # Initializes a Park with its name, admission price, and a list of vehicles. def initialize(name, price) - @name = name # The unique name of the park. + @name = name # The name of the park (e.g., Yellowstone). @price = price # The admission price per adult. - @vehicles = [] # A list to store vehicles that enter the park. + @vehicles = [] # A list of vehicles that entered the park. end - # Add a vehicle to the park's vehicle list. + # Adds a vehicle to the park. def add_vehicle(vehicle) @vehicles << vehicle end - # List all passengers in all vehicles that entered the park. + # Collects all passengers from all vehicles in the park. def passengers - @vehicles.flat_map(&:passengers) + @vehicles.flat_map(&:passengers) # Combines all passengers from all vehicles. end - # Calculate revenue generated by charging the admission price for each adult passenger. + # Calculates the total revenue generated by charging the admission price per adult. def revenue - num_adults = passengers.count(&:adult?) # Count only adult passengers. - num_adults * @price # Multiply by the park's admission price. + passengers.count(&:adult?) * @price # Multiply the number of adults by the price. + end + + # Lists the names of all passengers, sorted alphabetically. + def patron_names + passengers.map(&:name).sort + end + + # Lists the names of all minors (under 18), sorted alphabetically. + def minors + passengers.select { |p| !p.adult? }.map(&:name).sort + end + + # Lists the names of all adults (18 and older), sorted alphabetically. + def adults + passengers.select(&:adult?).map(&:name).sort end end diff --git a/lib/passenger.rb b/lib/passenger.rb index dcf9b71..e7a83de 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -1,24 +1,28 @@ # lib/passenger.rb +# The Passenger class represents a person in a vehicle. class Passenger attr_reader :name, :age + # Initializes a Passenger with their name and age. def initialize(details) - @name = details["name"] - @age = details["age"] - @driver = false + @name = details["name"] # Passenger's name. + @age = details["age"] # Passenger's age. + @driver = false # Tracks if the passenger is a driver (default: false). end + # Determines if the passenger is an adult (18 or older). def adult? @age >= 18 end + # Checks if the passenger is currently a driver. def driver? @driver end + # Updates the passenger's status to a driver. def drive @driver = true end end - diff --git a/lib/vehicle.rb b/lib/vehicle.rb index f5cb53a..44ee15b 100644 --- a/lib/vehicle.rb +++ b/lib/vehicle.rb @@ -1,20 +1,24 @@ # lib/vehicle.rb +# The Vehicle class represents a car with passengers. class Vehicle attr_reader :year, :make, :model, :passengers + # Initializes a Vehicle with its year, make, and model. def initialize(year, make, model) - @year = year - @make = make - @model = model - @passengers = [] + @year = year # The year the vehicle was manufactured. + @make = make # The vehicle's make (e.g., Honda). + @model = model # The vehicle's model (e.g., Civic). + @passengers = [] # A list to store passengers in the vehicle. end + # Adds a passenger to the vehicle. def add_passenger(passenger) @passengers << passenger end + # Counts the number of adult passengers in the vehicle. def num_adults - @passengers.count(&:adult?) + @passengers.count(&:adult?) # Use `adult?` to count passengers who are adults. end end diff --git a/spec/park_spec.rb b/spec/park_spec.rb index 57cee5b..8977f78 100644 --- a/spec/park_spec.rb +++ b/spec/park_spec.rb @@ -1,12 +1,10 @@ -# spec/park_spec.rb - require_relative '../lib/park' require_relative '../lib/vehicle' require_relative '../lib/passenger' RSpec.describe Park do before(:each) do - # Create a park and associated test data. + # Create a park and test data. @park = Park.new("Yellowstone", 50) @vehicle1 = Vehicle.new("2001", "Honda", "Civic") @vehicle2 = Vehicle.new("2010", "Toyota", "Corolla") @@ -21,36 +19,43 @@ end it 'exists and has attributes' do - # Test park attributes. - expect(@park).to be_a(Park) + # Check the park's attributes. expect(@park.name).to eq("Yellowstone") expect(@park.price).to eq(50) - expect(@park.vehicles).to eq([]) + expect(@park.vehicles).to eq([]) # Park starts with no vehicles. end it 'can add vehicles' do # Add vehicles to the park. @park.add_vehicle(@vehicle1) expect(@park.vehicles).to eq([@vehicle1]) + end + it 'can calculate revenue' do + # Add vehicles and calculate revenue from adult passengers. + @park.add_vehicle(@vehicle1) @park.add_vehicle(@vehicle2) - expect(@park.vehicles).to eq([@vehicle1, @vehicle2]) + expect(@park.revenue).to eq(100) # Charlie and Taylor generate revenue. end - it 'can list all passengers' do - # Add vehicles to the park and list all passengers. + it 'can list patron names alphabetically' do + # Add vehicles and list passenger names alphabetically. @park.add_vehicle(@vehicle1) @park.add_vehicle(@vehicle2) - - expect(@park.passengers).to eq([@charlie, @jude, @taylor]) + expect(@park.patron_names).to eq(["Charlie", "Jude", "Taylor"]) end - it 'can calculate revenue generated' do - # Add vehicles to the park and calculate revenue from adult passengers. + it 'can list minors alphabetically' do + # Add vehicles and list minors alphabetically. @park.add_vehicle(@vehicle1) @park.add_vehicle(@vehicle2) + expect(@park.minors).to eq(["Jude"]) + end - # Only Charlie and Taylor are adults, so 2 * $50 = $100. - expect(@park.revenue).to eq(100) + it 'can list adults alphabetically' do + # Add vehicles and list adults alphabetically. + @park.add_vehicle(@vehicle1) + @park.add_vehicle(@vehicle2) + expect(@park.adults).to eq(["Charlie", "Taylor"]) end end diff --git a/spec/passenger_spec.rb b/spec/passenger_spec.rb index b5c76e2..46d8b42 100644 --- a/spec/passenger_spec.rb +++ b/spec/passenger_spec.rb @@ -1,32 +1,23 @@ -# spec/passenger_spec.rb - require_relative '../lib/passenger' RSpec.describe Passenger do - describe 'Initialization' do - it 'exists and has attributes' do - charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + it 'exists and has attributes' do + # Create a passenger named Charlie who is 18 years old. + charlie = Passenger.new({"name" => "Charlie", "age" => 18}) - expect(charlie.name).to eq("Charlie") - expect(charlie.age).to eq(18) - end + # Check the passenger's attributes. + expect(charlie.name).to eq("Charlie") + expect(charlie.age).to eq(18) + expect(charlie.adult?).to eq(true) # Charlie is an adult. + expect(charlie.driver?).to eq(false) # Charlie is not a driver by default. end - describe 'Methods' do - it 'can determine if a passenger is an adult' do - charlie = Passenger.new({"name" => "Charlie", "age" => 18}) - taylor = Passenger.new({"name" => "Taylor", "age" => 12}) - - expect(charlie.adult?).to eq(true) - expect(taylor.adult?).to eq(false) - end - - it 'can check and update driver status' do - charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + it 'can update driver status' do + # Create a passenger and set them as a driver. + charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + charlie.drive - expect(charlie.driver?).to eq(false) - charlie.drive - expect(charlie.driver?).to eq(true) - end + # Verify that the passenger is now a driver. + expect(charlie.driver?).to eq(true) end end diff --git a/spec/vehicle_spec.rb b/spec/vehicle_spec.rb index a1424d2..0243379 100644 --- a/spec/vehicle_spec.rb +++ b/spec/vehicle_spec.rb @@ -1,40 +1,32 @@ require_relative '../lib/vehicle' require_relative '../lib/passenger' - RSpec.describe Vehicle do before(:each) do - # Create a vehicle and some passengers for testing. + # Create a vehicle and passengers for testing. @vehicle = Vehicle.new("2001", "Honda", "Civic") @charlie = Passenger.new({"name" => "Charlie", "age" => 18}) @jude = Passenger.new({"name" => "Jude", "age" => 12}) - @taylor = Passenger.new({"name" => "Taylor", "age" => 25}) end it 'exists and has attributes' do - # Verify the vehicle's attributes. - expect(@vehicle).to be_a(Vehicle) + # Check the vehicle's attributes. expect(@vehicle.year).to eq("2001") expect(@vehicle.make).to eq("Honda") expect(@vehicle.model).to eq("Civic") - expect(@vehicle.passengers).to eq([]) + expect(@vehicle.passengers).to eq([]) # Vehicle starts with no passengers. end it 'can add passengers' do - # Add passengers to the vehicle. + # Add a passenger to the vehicle. @vehicle.add_passenger(@charlie) - expect(@vehicle.passengers).to eq([@charlie]) - - @vehicle.add_passenger(@jude) - expect(@vehicle.passengers).to eq([@charlie, @jude]) + expect(@vehicle.passengers).to include(@charlie) end it 'can count the number of adults' do - # Add passengers and count the number of adults. + # Add passengers and count adults. @vehicle.add_passenger(@charlie) @vehicle.add_passenger(@jude) - @vehicle.add_passenger(@taylor) - - expect(@vehicle.num_adults).to eq(2) # Charlie and Taylor are adults. + expect(@vehicle.num_adults).to eq(1) # Only Charlie is an adult. end end