diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..529d84d Binary files /dev/null and b/.DS_Store differ diff --git a/lib/park.rb b/lib/park.rb index e69de29..c33b870 100644 --- a/lib/park.rb +++ b/lib/park.rb @@ -0,0 +1,43 @@ +# lib/park.rb + +# The Park class represents a national park with vehicles, passengers, and revenue tracking. +class Park + attr_reader :name, :price, :vehicles + + # Initializes a Park with its name, admission price, and a list of vehicles. + def initialize(name, price) + @name = name # The name of the park (e.g., Yellowstone). + @price = price # The admission price per adult. + @vehicles = [] # A list of vehicles that entered the park. + end + + # Adds a vehicle to the park. + def add_vehicle(vehicle) + @vehicles << vehicle + end + + # Collects all passengers from all vehicles in the park. + def passengers + @vehicles.flat_map(&:passengers) # Combines all passengers from all vehicles. + end + + # Calculates the total revenue generated by charging the admission price per adult. + def revenue + 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 e69de29..e7a83de 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -0,0 +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"] # 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 e69de29..44ee15b 100644 --- a/lib/vehicle.rb +++ b/lib/vehicle.rb @@ -0,0 +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 # 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?) # Use `adult?` to count passengers who are adults. + end +end diff --git a/spec/.keep b/spec/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/spec/park_spec.rb b/spec/park_spec.rb new file mode 100644 index 0000000..8977f78 --- /dev/null +++ b/spec/park_spec.rb @@ -0,0 +1,61 @@ +require_relative '../lib/park' +require_relative '../lib/vehicle' +require_relative '../lib/passenger' + +RSpec.describe Park do + before(:each) do + # Create a park and 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 + # Check the park's attributes. + expect(@park.name).to eq("Yellowstone") + expect(@park.price).to eq(50) + 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.revenue).to eq(100) # Charlie and Taylor generate revenue. + end + + it 'can list patron names alphabetically' do + # Add vehicles and list passenger names alphabetically. + @park.add_vehicle(@vehicle1) + @park.add_vehicle(@vehicle2) + expect(@park.patron_names).to eq(["Charlie", "Jude", "Taylor"]) + end + + 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 + + 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 new file mode 100644 index 0000000..46d8b42 --- /dev/null +++ b/spec/passenger_spec.rb @@ -0,0 +1,23 @@ +require_relative '../lib/passenger' + +RSpec.describe Passenger do + it 'exists and has attributes' do + # Create a passenger named Charlie who is 18 years old. + charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + + # 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 + + it 'can update driver status' do + # Create a passenger and set them as a driver. + charlie = Passenger.new({"name" => "Charlie", "age" => 18}) + charlie.drive + + # 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 new file mode 100644 index 0000000..0243379 --- /dev/null +++ b/spec/vehicle_spec.rb @@ -0,0 +1,32 @@ +require_relative '../lib/vehicle' +require_relative '../lib/passenger' + +RSpec.describe Vehicle do + before(:each) do + # 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}) + end + + it 'exists and has attributes' do + # 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([]) # Vehicle starts with no passengers. + end + + it 'can add passengers' do + # Add a passenger to the vehicle. + @vehicle.add_passenger(@charlie) + expect(@vehicle.passengers).to include(@charlie) + end + + it 'can count the number of adults' do + # Add passengers and count adults. + @vehicle.add_passenger(@charlie) + @vehicle.add_passenger(@jude) + expect(@vehicle.num_adults).to eq(1) # Only Charlie is an adult. + end +end