From 6fb6db8d64b0f0037ebf87ba8460861e8435b4e2 Mon Sep 17 00:00:00 2001 From: Coral Date: Wed, 18 Dec 2024 13:57:31 -0500 Subject: [PATCH] Finished Iteration 3 --- lib/park.rb | 33 +++++++++++++++++ lib/passenger.rb | 25 +++++++++++++ lib/vehicle.rb | 30 ++++++++++++++++ spec/park_spec.rb | 80 ++++++++++++++++++++++++++++++++++++++++++ spec/passenger_spec.rb | 47 +++++++++++++++++++++++++ spec/vehicle_spec.rb | 64 +++++++++++++++++++++++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 spec/park_spec.rb create mode 100644 spec/passenger_spec.rb create mode 100644 spec/vehicle_spec.rb diff --git a/lib/park.rb b/lib/park.rb index e69de29..e2a3d0a 100644 --- a/lib/park.rb +++ b/lib/park.rb @@ -0,0 +1,33 @@ +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 + passengers_array = [] + @vehicles.each do |vehicle| + vehicle.passengers.each do |passenger| + passengers_array << passenger + end + end + return passengers_array + end + + def revenue + total_revenue = 0 + passengers.each do |passenger| + if passenger.adult? == true + total_revenue += @price + end + end + return total_revenue + end +end \ No newline at end of file diff --git a/lib/passenger.rb b/lib/passenger.rb index e69de29..c18ecac 100644 --- a/lib/passenger.rb +++ b/lib/passenger.rb @@ -0,0 +1,25 @@ +class Passenger + attr_reader :name, :age, :driver + + def initialize(info) + @name = info[:name] + @age = info[:age] + @driver = false + end + + def adult? + if @age >= 18 + return true + else + return false + end + end + + def driver? + @driver + end + + def drive + @driver = true + end +end \ No newline at end of file diff --git a/lib/vehicle.rb b/lib/vehicle.rb index e69de29..31f029b 100644 --- a/lib/vehicle.rb +++ b/lib/vehicle.rb @@ -0,0 +1,30 @@ +class Vehicle + + attr_reader :year, :make, :model, :speeding, :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 do |passenger| + passenger.adult? + end + end +end \ No newline at end of file diff --git a/spec/park_spec.rb b/spec/park_spec.rb new file mode 100644 index 0000000..85988e9 --- /dev/null +++ b/spec/park_spec.rb @@ -0,0 +1,80 @@ +require './lib/passenger' +require './lib/vehicle' +require './lib/park' + + +RSpec.describe Park do + before(:each)do + @kennywood = Park.new("Kennywood", 10) + @vehicle_1 = Vehicle.new("2001", "Honda", "Civic") + @vehicle_2 = Vehicle.new("2015", "Subaru", "Forrester") + @coral = Passenger.new({name: 'Coral', age: 29}) + @jonah = Passenger.new({name: 'Jonah', age: 6}) + @charlie = Passenger.new({name: 'Charlie', age: 18}) + @jude = Passenger.new({name: 'Jude', age: 20}) + @taylor = Passenger.new({name: 'Taylor', age: 12}) + end + + describe "#initialization" do + it "exists" do + expect(@kennywood).to be_a Park + end + it "has a name" do + expect(@kennywood.name).to eq('Kennywood') + end + + it "has a price" do + expect(@kennywood.price).to eq(10) + end + end + + describe "#vehicles" do + it "defaults an empty array" do + expect(@kennywood.vehicles).to eq([]) + end + + it "can add a vehicle to array" do + @kennywood.add_vehicle(@vehicle_1) + + expect(@kennywood.vehicles).to eq([@vehicle_1]) + end + + it "can add multiple vehicles to array" do + @kennywood.add_vehicle(@vehicle_1) + @kennywood.add_vehicle(@vehicle_2) + + expect(@kennywood.vehicles).to eq([@vehicle_1, @vehicle_2]) + end + end + + describe "#passengers" do + it "can create array of passengers from multiple vehicles" do + @vehicle_1.add_passenger(@charlie) + @vehicle_1.add_passenger(@jude) + @vehicle_1.add_passenger(@taylor) + @vehicle_2.add_passenger(@coral) + @vehicle_2.add_passenger(@jonah) + + @kennywood.add_vehicle(@vehicle_1) + @kennywood.add_vehicle(@vehicle_2) + + expect(@kennywood.passengers).to eq([@charlie, @jude, @taylor, @coral, @jonah]) + end + end + + describe "#revenue" do + it "can accurately calculate revenue collected from all adults" do + @vehicle_1.add_passenger(@charlie) + @vehicle_1.add_passenger(@jude) + @vehicle_1.add_passenger(@taylor) + @vehicle_2.add_passenger(@coral) + @vehicle_2.add_passenger(@jonah) + + @kennywood.add_vehicle(@vehicle_1) + @kennywood.add_vehicle(@vehicle_2) + + expect(@kennywood.revenue).to eq(30) + end + end + +end \ No newline at end of file diff --git a/spec/passenger_spec.rb b/spec/passenger_spec.rb new file mode 100644 index 0000000..a01e503 --- /dev/null +++ b/spec/passenger_spec.rb @@ -0,0 +1,47 @@ +require './lib/passenger' + +RSpec.describe Passenger do + before(:each) do + @charlie = Passenger.new({name: 'Charlie', age: 18}) + @taylor = Passenger.new({name: 'Taylor', age: 12}) + end + + describe "#initialization" do + it "exists" do + expect(@charlie).to be_a Passenger + expect(@taylor).to be_a Passenger + end + + it "has a name" do + expect(@charlie.name).to eq('Charlie') + expect(@taylor.name).to eq('Taylor') + end + + it "has an age" do + expect(@charlie.age).to eq(18) + expect(@taylor.age).to eq(12) + end + end + + describe "#adult?" do + it "accurately determines adulthood" do + expect(@charlie.adult?).to eq(true) + expect(@taylor.adult?).to eq(false) + end + end + + describe "driver?" do + it "verifies driver default is false" do + expect(@charlie.driver?).to eq(false) + expect(@taylor.driver?).to eq(false) + end + end + + describe "drive" do + it "changes driver status" do + expect(@charlie.driver?).to eq(false) + @charlie.drive + expect(@charlie.driver?).to eq(true) + end + end +end \ No newline at end of file diff --git a/spec/vehicle_spec.rb b/spec/vehicle_spec.rb new file mode 100644 index 0000000..2cfbbcc --- /dev/null +++ b/spec/vehicle_spec.rb @@ -0,0 +1,64 @@ +require './lib/passenger' +require './lib/vehicle' + + +RSpec.describe Vehicle do + before(:each)do + @vehicle = Vehicle.new("2001", "Honda", "Civic") + @charlie = Passenger.new({name: 'Charlie', age: 18}) + @jude = Passenger.new({name: 'Jude', age: 20}) + @taylor = Passenger.new({name: 'Taylor', age: 12}) + end + + describe "#initialization" do + it "exists" do + expect(@vehicle).to be_a Vehicle + end + + it "has a year" do + expect(@vehicle.year).to eq("2001") + end + + it "has a make" do + expect(@vehicle.make).to eq("Honda") + end + + it "has a model" do + expect(@vehicle.model).to eq("Civic") + end + end + + describe "speeding?" do + it "verifies speeding default is false" do + expect(@vehicle.speeding?).to eq(false) + end + + it "changes speeding status" do + expect(@vehicle.speeding?).to eq(false) + @vehicle.speed + expect(@vehicle.speeding?).to eq(true) + end + end + + describe "#passengers" do + it "defaults to empty array" do + expect(@vehicle.passengers).to eq([]) + end + it "add_passengers adds passengers to array" do + @vehicle.add_passenger(@charlie) + @vehicle.add_passenger(@jude) + @vehicle.add_passenger(@taylor) + expect(@vehicle.passengers).to eq([@charlie, @jude, @taylor]) + end + end + + describe "#num_adults" do + it "counts number of adult passengers" do + @vehicle.add_passenger(@charlie) + @vehicle.add_passenger(@jude) + @vehicle.add_passenger(@taylor) + + expect(@vehicle.num_adults).to eq(2) + end + end +end \ No newline at end of file