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

Passengers & Vehicles - Chase Phillips #4

Open
wants to merge 4 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
63 changes: 63 additions & 0 deletions lib/park.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
class Park
attr_reader :name, :price, :vehicles

def initialize(name, price)
@name = name
@price = price
@vehicles = []
end

def add_vehicle(vehicle)
@vehicles << vehicle
end

def passengers
all_passengers = []
@vehicles.each do |vehicle|
vehicle.passengers.each do |passenger|
all_passengers << passenger
end
end
all_passengers
end

def revenue
adults = passengers.select { |passenger| passenger.adult? }
adults.count * @price
end

def vehicles_in_park
@vehicles
end

def patron_names
names = []
passengers.each do |passenger|
names << passenger.name
end
names.sort
end


def minors
minors = []
passengers.each do |passenger|
if passenger.age < 18
minors << passenger.name
end
end
minors.sort
end


def adults
adults = []
passengers.each do |passenger|
if passenger.age >= 18
adults << passenger.name
end
end
adults.sort
end

end
22 changes: 22 additions & 0 deletions lib/passenger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Passenger
attr_reader :name, :age

def initialize(passenger_attributes)
@name = passenger_attributes["name"]
@age = passenger_attributes["age"]
@driver = false

end

def adult?
@age >= 18
end

def driver?
@driver
end

def drive
@driver = true
end
end
31 changes: 31 additions & 0 deletions lib/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Vehicle
attr_reader :year, :make, :model, :passengers


def initialize(year, make, model)
@year = year
@make = make
@model = model
@speeding = false
@passengers = []


end

def speeding?
@speeding
end

def speed
@speeding = true
end

def add_passenger(passenger)
@passengers << passenger
end

def num_adults
@passengers.count { |passenger|
passenger.age >= 18 }
end
end
153 changes: 153 additions & 0 deletions spec/park_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
require './lib/passenger'
require './lib/vehicle'
require './lib/park'

RSpec.describe Park do
it 'can add a vehicle to the park' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")

park.add_vehicle(vehicle1)

expect(park.vehicles).to include(vehicle1)

end

it 'can list all vehicles that entered the park' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")
vehicle2 = Vehicle.new("1985", "Toyota", "Tacoma")

park.add_vehicle(vehicle1)
park.add_vehicle(vehicle2)


expect(park.vehicles_in_park).to eq([vehicle1, vehicle2])

end

it 'lists all passengers in the park' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")
vehicle2 = Vehicle.new("1985", "Toyota", "Tacoma")
passenger1 = Passenger.new({"name" => "Charlie", "age" => 18})
passenger2 = Passenger.new({"name" => "Jude", "age" => 20})
passenger3 = Passenger.new({"name" => "Taylor", "age" => 12})
passenger4 = Passenger.new({"name" => "Gabe", "age" => 98})

vehicle1.add_passenger(passenger1)
vehicle1.add_passenger(passenger2)
vehicle1.add_passenger(passenger3)
vehicle2.add_passenger(passenger4)

park.add_vehicle(vehicle1)
park.add_vehicle(vehicle2)

expect(park.passengers.map(&:name)).to include("Charlie", "Jude", "Taylor", "Gabe")

end

it 'calculates revenue generated by adult passengers' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")
vehicle2 = Vehicle.new("1985", "Toyota", "Tacoma")
passenger1 = Passenger.new({"name" => "Charlie", "age" => 18})
passenger2 = Passenger.new({"name" => "Jude", "age" => 20})
passenger3 = Passenger.new({"name" => "Taylor", "age" => 12})
passenger4 = Passenger.new({"name" => "Gabe", "age" => 98})

vehicle1.add_passenger(passenger1)
vehicle1.add_passenger(passenger2)
vehicle1.add_passenger(passenger3)
vehicle2.add_passenger(passenger4)

park.add_vehicle(vehicle1)
park.add_vehicle(vehicle2)

expect(park.revenue).to eq(60)

end

it 'counts passengers in the park' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")
vehicle2 = Vehicle.new("1985", "Toyota", "Tacoma")
passenger1 = Passenger.new({"name" => "Charlie", "age" => 18})
passenger2 = Passenger.new({"name" => "Jude", "age" => 20})
passenger3 = Passenger.new({"name" => "Taylor", "age" => 12})
passenger4 = Passenger.new({"name" => "Gabe", "age" => 98})

vehicle1.add_passenger(passenger1)
vehicle1.add_passenger(passenger2)
vehicle1.add_passenger(passenger3)
vehicle2.add_passenger(passenger4)

park.add_vehicle(vehicle1)
park.add_vehicle(vehicle2)

expect(park.passengers.size).to eq(4)

end

it 'list all patron names alphabetically' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")
vehicle2 = Vehicle.new("1985", "Toyota", "Tacoma")
passenger1 = Passenger.new({"name" => "Charlie", "age" => 18})
passenger2 = Passenger.new({"name" => "Jude", "age" => 20})
passenger3 = Passenger.new({"name" => "Taylor", "age" => 12})
passenger4 = Passenger.new({"name" => "Gabe", "age" => 98})

vehicle1.add_passenger(passenger1)
vehicle1.add_passenger(passenger2)
vehicle1.add_passenger(passenger3)
vehicle2.add_passenger(passenger4)

park.add_vehicle(vehicle1)
park.add_vehicle(vehicle2)

expect(park.patron_names).to eq(["Charlie", "Gabe", "Jude", "Taylor"])

end

it 'lists all minor patron names alphabetically' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")
vehicle2 = Vehicle.new("1985", "Toyota", "Tacoma")
passenger1 = Passenger.new({"name" => "Charlie", "age" => 18})
passenger2 = Passenger.new({"name" => "Jude", "age" => 20})
passenger3 = Passenger.new({"name" => "Taylor", "age" => 12})
passenger4 = Passenger.new({"name" => "Gabe", "age" => 98})

vehicle1.add_passenger(passenger1)
vehicle1.add_passenger(passenger2)
vehicle1.add_passenger(passenger3)
vehicle2.add_passenger(passenger4)

park.add_vehicle(vehicle1)
park.add_vehicle(vehicle2)

expect(park.minors).to eq(["Taylor"])

end

it 'lists all adult patron names alphabetically' do
park = Park.new("Yellowstone", 20)
vehicle1 = Vehicle.new("2001", "Honda", "Civic")
vehicle2 = Vehicle.new("1985", "Toyota", "Tacoma")
passenger1 = Passenger.new({"name" => "Charlie", "age" => 18})
passenger2 = Passenger.new({"name" => "Jude", "age" => 20})
passenger3 = Passenger.new({"name" => "Taylor", "age" => 12})
passenger4 = Passenger.new({"name" => "Gabe", "age" => 98})

vehicle1.add_passenger(passenger1)
vehicle1.add_passenger(passenger2)
vehicle1.add_passenger(passenger3)
vehicle2.add_passenger(passenger4)

park.add_vehicle(vehicle1)
park.add_vehicle(vehicle2)

expect(park.adults).to eq(["Charlie", "Gabe", "Jude"])
end
end
52 changes: 52 additions & 0 deletions spec/passenger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require './lib/passenger'

RSpec.describe Passenger do

it 'returns a passenger exists' do
charlie = Passenger.new({"name" => "Charlie", "age" => 18})
taylor = Passenger.new({"name" => "Taylor", "age" => 12})

expect(charlie).to be_a(Passenger)
end

it 'retuns the name of the passenger' do
charlie = Passenger.new({"name" => "Charlie", "age" => 18})
taylor = Passenger.new({"name" => "Taylor", "age" => 12})

expect(charlie.name).to eq("Charlie")

end

it 'returns a passenger age' do
charlie = Passenger.new({"name" => "Charlie", "age" => 18})
taylor = Passenger.new({"name" => "Taylor", "age" => 12})

expect(charlie.age).to eq(18)


end

it 'determines whether passenger is or is not 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 'determines if a passenger is a driver' do
charlie = Passenger.new({"name" => "Charlie", "age" => 18})
taylor = Passenger.new({"name" => "Taylor", "age" => 12})

expect(charlie.driver?).to eq(false)

charlie.drive

expect(charlie.driver?).to eq(true)

end


end
Loading