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

Hannah Gray- Week 3 IC - Iteration 4 #8

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
43 changes: 43 additions & 0 deletions lib/park.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions lib/passenger.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions lib/vehicle.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file removed spec/.keep
Empty file.
61 changes: 61 additions & 0 deletions spec/park_spec.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions spec/passenger_spec.rb
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions spec/vehicle_spec.rb
Original file line number Diff line number Diff line change
@@ -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