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

Mark Kendall #7

Open
wants to merge 15 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
109 changes: 109 additions & 0 deletions lib/park.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require 'pry'

class Park
attr_reader :name, :price

def initialize(name, price)
@name = name
@price = price.to_i #Just to be sure it's an integer

@vehicles_in_park = []
@revenue = 0 #Assume e.g. this tracks annual revenue, starts at zero for year
end

def add_vehicle(incoming_vehicle)
#This method assumes that by 'adding' the vehicle, it is entering the park and being charged, and so the passegers pay the entrance fee
#Alternate: don't use instance var @revenue, just deduce from # vehicles in park (but risky - vehicles could leave, so need history)
@revenue += calculate_entrance_fee(incoming_vehicle)
@vehicles_in_park << incoming_vehicle
end

def passengers()
passenger_list = []
@vehicles_in_park.each do |vehicle|
passenger_list << vehicle.passengers
end
#Since this is a 2D array (vehicles) of arrays (passengers), need to flatten it to 1D:
passenger_list.flatten
end

def revenue()
#Trivial var return, due to implementation of add_vehicle()
@revenue
end

def vehicles()
#To be compliant with instructions' naming scheme
@vehicles_in_park
end

def patron_names()
#List all patrons' names in the park, sorted alphabetically
#NOTE: could write a separate method to just sort patrons as a full array, since 3 methods kidna rely on this...
#Written at bottom of class
passengers.sort_by do |passenger|
passenger.name
end.map do |passenger_sorted|
passenger_sorted.name
end
end

def adults()
#Final all adult passengers, then sort by their names, then map to an array with just their names
# passengers.find_all do |passenger|
# passenger.adult?()
# end.sort_by do |adult|
# adult.name
# end.map do |adult_sorted|
# adult_sorted.name
# end

#Refactored after turning in:
sorted_passengers().find_all do |passenger_sorted|
passenger_sorted.adult?()
end.map do |adult_sorted|
adult_sorted.name
end
end

def minors()
#Same song and dance as for adults() - it is actually the negated set of adults (in another implementation this could save computational time)
# passengers.find_all do |passenger|
# !passenger.adult?()
# end.sort_by do |minor|
# minor.name
# end.map do |minor_sorted|
# minor_sorted.name
# end

sorted_passengers().find_all do |passenger_sorted|
!passenger_sorted.adult?()
end.map do |minor_sorted|
minor_sorted.name
end
end

private

#Anything defined below here will be private. NEET!

def sorted_passengers()
#Built after I turned in.
passengers().sort_by do |passenger|
passenger.name
end
end

#Could move entrance_fees() here!
def calculate_entrance_fee(vehicle)
# vehicle.passengers.length * @price

#Ah crap, I accidentally charged the same fee for all people (not just adults)
#Here is the modified version:
vehicle.num_adults() * @price
end

#Some other time: might want to provide a printing service as well to list various queries on the terminal.
#If so, make this a separate class maybe? Can call methods from e.g. Park class wherever, then format and print accordingly (probably needs its own tests too, ugh)

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_data)
@name = passenger_data["name"]
@age = passenger_data["age"]

@is_driver = false
end

def adult?()
@age >= 18
end

def driver?()
@is_driver
end

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

def initialize(year, make, model)
@year = year
@make = make
@model = model

@is_speeding = false
@passengers = []
end

def speeding?()
@is_speeding
end

def speed()
#Wait a minute, aren't we all law-abiding citizens?
@is_speeding = true
end

def add_passenger(passenger)
@passengers << passenger
end

def num_adults()
@passengers.count { |passenger| passenger.adult?()}
end

end
122 changes: 122 additions & 0 deletions spec/park_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
require './lib/vehicle.rb'
require './lib/passenger.rb'
require './lib/park.rb'

RSpec.describe Park do
before(:each) do
@yellowstone = Park.new("Yellowstone", 20)

@vehicle1 = Vehicle.new("2001", "Honda", "Civic")
@vehicle2 = Vehicle.new("2016", "Subaru", "Forester")
@vehicle3 = Vehicle.new("2022", "Telsa", "S3")

@charlie = Passenger.new({"name" => "Charlie", "age" => 18})
@taylor = Passenger.new({"name" => "Taylor", "age" => 12})
@jude = Passenger.new({"name" => "Jude", "age" => 20})
@finneas = Passenger.new({"name" => "Finneas", "age" => 30})
@simone = Passenger.new({"name" => "Simone", "age" => 34})
@abigail = Passenger.new({"name" => "Abigail", "age" => 14})
end

it 'can initialize' do
expect(@yellowstone).to be_a(Park)
expect(@yellowstone.name).to eq("Yellowstone")
expect(@yellowstone.price).to eq(20)
expect(@yellowstone.vehicles).to eq([])
end

it 'can add vehicles entering / in the park and list them' do
@yellowstone.add_vehicle(@vehicle2)
@yellowstone.add_vehicle(@vehicle1)

expect(@yellowstone.vehicles()).to eq([@vehicle2, @vehicle1])
end

it 'can list all passengers in the park' do
@vehicle1.add_passenger(@charlie)
@vehicle1.add_passenger(@jude)
@vehicle2.add_passenger(@simone)
@vehicle2.add_passenger(@taylor)
@vehicle3.add_passenger(@finneas)

@yellowstone.add_vehicle(@vehicle1)
@yellowstone.add_vehicle(@vehicle2)
@yellowstone.add_vehicle(@vehicle3)

expect(@yellowstone.passengers()).to be_a(Array)
expect(@yellowstone.passengers().length).to eq(5)
expect(@yellowstone.passengers()).to eq([@charlie, @jude, @simone, @taylor, @finneas])
end

it 'can calculate revenue generated based on vehicle and passenger entries' do
@vehicle1.add_passenger(@charlie)
@vehicle1.add_passenger(@jude)
@vehicle2.add_passenger(@simone)
@vehicle2.add_passenger(@taylor)
@vehicle3.add_passenger(@finneas)

@yellowstone.add_vehicle(@vehicle1)
@yellowstone.add_vehicle(@vehicle2)
expect(@yellowstone.revenue()).to eq(60) #Used to be 80, now 60 (only adults are charged)

@yellowstone.add_vehicle(@vehicle3)
expect(@yellowstone.revenue()).to eq(80) #Was 100, now 80
end

xit 'helper method calculate_entrance_fee() for generating revenue works correctly' do
#NOTE: this is no longer needed / legal, since I made the method private
@vehicle1.add_passenger(@charlie)
@vehicle1.add_passenger(@simone)
@vehicle1.add_passenger(@taylor)

expect(@yellowstone.calculate_entrance_fee(@vehicle1)).to eq(60)
end

it 'can generate alphabetized list of all park patrons' do
@vehicle1.add_passenger(@charlie)
@vehicle1.add_passenger(@jude)
@vehicle2.add_passenger(@simone)
@vehicle2.add_passenger(@taylor)
@vehicle3.add_passenger(@finneas)
@vehicle3.add_passenger(@abigail)
@yellowstone.add_vehicle(@vehicle1)
@yellowstone.add_vehicle(@vehicle2)
@yellowstone.add_vehicle(@vehicle3)

expect(@yellowstone.patron_names()).to eq(["Abigail", "Charlie", "Finneas", "Jude", "Simone", "Taylor"])
end

it 'can generate alphabetized list of all adult patrons' do
@vehicle1.add_passenger(@charlie)
@vehicle1.add_passenger(@jude)
@vehicle2.add_passenger(@simone)
@vehicle2.add_passenger(@taylor)
@vehicle3.add_passenger(@finneas)
@vehicle3.add_passenger(@abigail)
@yellowstone.add_vehicle(@vehicle1)
@yellowstone.add_vehicle(@vehicle2)
@yellowstone.add_vehicle(@vehicle3)

expect(@yellowstone.adults().length).to eq(4)
expect(@yellowstone.adults()).to eq(["Charlie", "Finneas", "Jude", "Simone"])
end

it 'can generate alphabetized list of all minor patrons' do
@vehicle1.add_passenger(@charlie)
@vehicle1.add_passenger(@jude)
@vehicle2.add_passenger(@simone)
@vehicle2.add_passenger(@taylor)
@vehicle3.add_passenger(@finneas)
@vehicle3.add_passenger(@abigail)
@yellowstone.add_vehicle(@vehicle1)
@yellowstone.add_vehicle(@vehicle2)
@yellowstone.add_vehicle(@vehicle3)

expect(@yellowstone.minors().length).to eq(2)
expect(@yellowstone.minors()).to eq(["Abigail", "Taylor"])
end

#NOTE: no test(s) for sorted_passengers(), since the method is private and basically can't be accessed outside the class.
#Felt confident in it since basically directly pulling already existing code to simply make more compact. Works!

end
31 changes: 31 additions & 0 deletions spec/passenger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require './lib/passenger.rb'

RSpec.describe Passenger do
before(:each) do
@charlie = Passenger.new({"name" => "Charlie", "age" => 18})
@taylor = Passenger.new({"name" => "Taylor", "age" => 12})
end

it 'can initialize' do
expect(@charlie).to be_a(Passenger)
expect(@taylor).to be_a(Passenger)
expect(@charlie.name).to eq("Charlie")
expect(@charlie.age).to eq(18)

end

it 'can test if passenger is an adult' do
expect(@charlie.adult?()).to eq(true)
expect(@taylor.adult?()).to eq(false)
end

it 'can test if passenger is a driver by default' do
expect(@charlie.driver?()).to eq(false)
end

it 'can drive a vehicle and change is_driver status' do
@charlie.drive()
expect(@charlie.driver?()).to eq(true)
end

end
48 changes: 48 additions & 0 deletions spec/vehicle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require './lib/vehicle.rb'
require './lib/passenger.rb'

RSpec.describe Vehicle do
before(:each) do
@vehicle = Vehicle.new("2001", "Honda", "Civic")

@charlie = Passenger.new({"name" => "Charlie", "age" => 18})
@taylor = Passenger.new({"name" => "Taylor", "age" => 12})
@jude = Passenger.new({"name" => "Jude", "age" => 20})
end

it 'can initialize' do
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 'check if speeding by default' do
expect(@vehicle.speeding?()).to eq(false)
end

it 'can speed and update instance var accordingly' do
@vehicle.speed()
expect(@vehicle.speeding?()).to eq(true)
end

it 'can add passengers to the current vehicle' do
@vehicle.add_passenger(@charlie)
@vehicle.add_passenger(@jude)
@vehicle.add_passenger(@taylor)

expect(@vehicle.passengers).to eq([@charlie, @jude, @taylor])
end

it 'can determine the number of adults in the vehicle' do
@vehicle.add_passenger(@charlie)
@vehicle.add_passenger(@taylor)

expect(@vehicle.num_adults()).to eq(1)

@vehicle.add_passenger(@jude)
expect(@vehicle.num_adults()).to eq(2)
end

end