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 - add Registrant class and Factory classes for vehicles and facilities #607

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
.DS_Store
16 changes: 12 additions & 4 deletions lib/dmv.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
# The Dmv class represents the core DMV system that manages facilities.
class Dmv
# Provides read access to the list of facilities.
attr_reader :facilities

# Initializes the Dmv object with an empty array of facilities.
def initialize
@facilities = []
@facilities = [] # Array to store Facility objects
end

# Adds a facility to the DMV.
# @param facility [Facility] The facility object to be added.
def add_facility(facility)
@facilities << facility
end

# Returns facilities that offer a specific service.
# @param service [String] The service to search for.
# @return [Array<Facility>] List of facilities providing the service.
def facilities_offering_service(service)
@facilities.find do |facility|
facility.services.include?(service)
end
@facilities.select { |facility| facility.services.include?(service) }
end
end

16 changes: 12 additions & 4 deletions lib/dmv_data_service.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
require 'faraday'
require 'json'

# The DmvDataService class retrieves external data from an API.
class DmvDataService
# Loads data from a given URL
# @param source: String URL for the data source
# @return: Parsed JSON data
def load_data(source)
response = Faraday.get(source)
JSON.parse(response.body, symbolize_names: true)
end

# Retrieves Washington EV Registration data
def wa_ev_registrations
@wa_ev_registrations ||= load_data('https://data.wa.gov/resource/rpr4-cgyd.json')
load_data('https://data.wa.gov/resource/rpr4-cgyd.json')
end

# Retrieves Colorado DMV Office Locations data
def co_dmv_office_locations
@co_dmv_office_locations ||= load_data('https://data.colorado.gov/resource/dsw3-mrn4.json')
load_data('https://data.colorado.gov/resource/dsw3-mrn4.json')
end

# Retrieves New York DMV Office Locations data
def ny_dmv_office_locations
@ny_dmv_office_locations ||= load_data('https://data.ny.gov/resource/9upz-c7xg.json')
load_data('https://data.ny.gov/resource/9upz-c7xg.json')
end

# Retrieves Missouri DMV Office Locations data
def mo_dmv_office_locations
@mo_dmv_office_locations ||= load_data('https://data.mo.gov/resource/835g-7keg.json')
load_data('https://data.mo.gov/resource/835g-7keg.json')
end
end
17 changes: 17 additions & 0 deletions lib/facility_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'facility'

# The FacilityFactory class creates Facility objects from external data sources.
class FacilityFactory
# Creates an array of Facility objects from external data.
# @param data [Array<Hash>] An array of facility details from the API.
# @return [Array<Facility>] An array of Facility objects.
def create_facilities(data)
data.map do |facility_data|
Facility.new({
name: facility_data[:dmv_office],
address: "#{facility_data[:address_li]}, #{facility_data[:city]}",
phone: facility_data[:phone]
})
end
end
end
25 changes: 25 additions & 0 deletions lib/registrant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# The Registrant class represents a person who interacts with DMV services.
# A registrant can have a permit and progress to earning a license.
class Registrant
attr_reader :name, :age, :license_data

# Initializes the registrant with a name, age, and optional permit status.
# license_data is initialized to track written, road, and renewed license status.
def initialize(name, age, permit = false)
@name = name
@age = age
@permit = permit
@license_data = { written: false, license: false, renewed: false }
end

# Checks if the registrant has a permit.
def permit?
@permit
end

# Allows a registrant to earn a permit.
def earn_permit
@permit = true
end
end

36 changes: 24 additions & 12 deletions lib/vehicle.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
require 'date'

# The Vehicle class represents a registered vehicle.
class Vehicle
attr_reader :vin,
:year,
:make,
:model,
:engine
attr_reader :vin, :year, :make, :model, :engine, :registration_date, :plate_type

def initialize(vehicle_details)
@vin = vehicle_details[:vin]
@year = vehicle_details[:year]
@make = vehicle_details[:make]
@model = vehicle_details[:model]
@engine = vehicle_details[:engine]
def initialize(details)
@vin = details[:vin]
@year = details[:year]
@make = details[:make]
@model = details[:model]
@engine = details[:engine]
@registration_date = nil
@plate_type = nil
end

# Registers the vehicle and determines the plate type.
def register
@registration_date = Date.today
@plate_type = determine_plate_type
end

# Determines plate type.
def determine_plate_type
return :antique if antique?
return :ev if electric?
:regular
end

def antique?
Date.today.year - @year > 25
end

def electric_vehicle?
def electric?
@engine == :ev
end
end
19 changes: 19 additions & 0 deletions lib/vehicle_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require_relative 'vehicle'

# The VehicleFactory class is responsible for creating Vehicle objects from external data.
class VehicleFactory
# Creates Vehicle objects from provided data
# @param data: Array of hashes containing vehicle details
# @return: Array of Vehicle objects
def create_vehicles(data)
data.map do |vehicle_data|
Vehicle.new({
vin: vehicle_data[:vin_1_10],
year: vehicle_data[:model_year].to_i,
make: vehicle_data[:make],
model: vehicle_data[:model],
engine: :ev # Default engine type to electric for external EV data
})
end
end
end
47 changes: 17 additions & 30 deletions spec/dmv_spec.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
require 'spec_helper'
require 'rspec'
require './lib/dmv'

# Test suite for the Dmv class
RSpec.describe Dmv do
before(:each) do
@dmv = Dmv.new
@facility_1 = Facility.new({name: 'DMV Tremont Branch', address: '2855 Tremont Place Suite 118 Denver CO 80205', phone: '(720) 865-4600'})
@facility_2 = Facility.new({name: 'DMV Northeast Branch', address: '4685 Peoria Street Suite 101 Denver CO 80239', phone: '(720) 865-4600'})
@facility_3 = Facility.new({name: 'DMV Northwest Branch', address: '3698 W. 44th Avenue Denver CO 80211', phone: '(720) 865-4600'})
end

describe '#initialize' do
it 'can initialize' do
expect(@dmv).to be_an_instance_of(Dmv)
expect(@dmv.facilities).to eq([])
end
it 'initializes with no facilities' do
expect(@dmv.facilities).to eq([]) # Facilities start as an empty array
end

describe '#add facilities' do
it 'can add available facilities' do
expect(@dmv.facilities).to eq([])
@dmv.add_facility(@facility_1)
expect(@dmv.facilities).to eq([@facility_1])
end
end
it 'can add facilities' do
mock_facility = double('Facility', services: ['Renew License'])
@dmv.add_facility(mock_facility)

describe '#facilities_offering_service' do
it 'can return list of facilities offering a specified Service' do
@facility_1.add_service('New Drivers License')
@facility_1.add_service('Renew Drivers License')
@facility_2.add_service('New Drivers License')
@facility_2.add_service('Road Test')
@facility_2.add_service('Written Test')
@facility_3.add_service('New Drivers License')
@facility_3.add_service('Road Test')
expect(@dmv.facilities).to include(mock_facility)
end

@dmv.add_facility(@facility_1)
@dmv.add_facility(@facility_2)
@dmv.add_facility(@facility_3)
it 'can find facilities offering a specific service' do
facility_1 = double('Facility', services: ['Vehicle Registration'])
facility_2 = double('Facility', services: ['Renew License'])
@dmv.add_facility(facility_1)
@dmv.add_facility(facility_2)

expect(@dmv.facilities_offering_service('Road Test')).to eq([@facility_2, @facility_3])
end
result = @dmv.facilities_offering_service('Renew License')
expect(result).to eq([facility_2])
end
end
22 changes: 22 additions & 0 deletions spec/facility_factory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rspec'
require './lib/facility_factory'
require './lib/facility'

RSpec.describe FacilityFactory do
before(:each) do
@factory = FacilityFactory.new
@data = [
{ dmv_office: 'Denver DMV', address_li: '123 Main St', city: 'Denver', phone: '555-1234' },
{ dmv_office: 'Boulder DMV', address_li: '456 Pearl St', city: 'Boulder', phone: '555-5678' }
]
end

it 'can create facilities from external data' do
facilities = @factory.create_facilities(@data)

expect(facilities.length).to eq(2)
expect(facilities.first.name).to eq('Denver DMV')
expect(facilities.first.address).to eq('123 Main St, Denver')
expect(facilities.first.phone).to eq('555-1234')
end
end
46 changes: 28 additions & 18 deletions spec/facility_spec.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
require 'spec_helper'
require 'rspec' # Require RSpec for testing
require './lib/facility' # Require the Facility class file to test

# RSpec test suite for the Facility class
RSpec.describe Facility do
# Runs before each test block to create a fresh Facility object
before(:each) do
@facility = Facility.new({name: 'DMV Tremont Branch', address: '2855 Tremont Place Suite 118 Denver CO 80205', phone: '(720) 865-4600'})
# Initialize a facility with a hash of details
@facility = Facility.new({
name: 'DMV Main Branch',
address: '123 Main St',
phone: '555-1234'
})
end
describe '#initialize' do
it 'can initialize' do
expect(@facility).to be_an_instance_of(Facility)
expect(@facility.name).to eq('DMV Tremont Branch')
expect(@facility.address).to eq('2855 Tremont Place Suite 118 Denver CO 80205')
expect(@facility.phone).to eq('(720) 865-4600')
expect(@facility.services).to eq([])
end

# Test to check if a Facility object initializes correctly
it 'initializes with details' do
# Check the attributes of the Facility instance
expect(@facility.name).to eq('DMV Main Branch')
expect(@facility.address).to eq('123 Main St')
expect(@facility.phone).to eq('555-1234')
expect(@facility.services).to eq([]) # Services should start as an empty array
end

describe '#add service' do
it 'can add available services' do
expect(@facility.services).to eq([])
@facility.add_service('New Drivers License')
@facility.add_service('Renew Drivers License')
@facility.add_service('Vehicle Registration')
expect(@facility.services).to eq(['New Drivers License', 'Renew Drivers License', 'Vehicle Registration'])
end
# Test to verify that services can be added to a facility
it 'can add services' do
# Add a service to the facility
@facility.add_service('Vehicle Registration')
@facility.add_service('Renew License')

# Check if the services list contains the added services
expect(@facility.services).to include('Vehicle Registration')
expect(@facility.services).to include('Renew License')
expect(@facility.services.length).to eq(2) # Check the count of services
end
end
24 changes: 24 additions & 0 deletions spec/registrant_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rspec'
require './lib/registrant'

RSpec.describe Registrant do
before(:each) do
@registrant = Registrant.new('Bruce', 18, true)
end

it 'can initialize with attributes' do
expect(@registrant.name).to eq('Bruce')
expect(@registrant.age).to eq(18)
expect(@registrant.permit?).to eq(true)
expect(@registrant.license_data).to eq({ written: false, license: false, renewed: false })
end

it 'can earn a permit' do
registrant = Registrant.new('Penny', 15)

expect(registrant.permit?).to eq(false)

registrant.earn_permit
expect(registrant.permit?).to eq(true)
end
end
24 changes: 24 additions & 0 deletions spec/vehicle_factory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rspec'
require './lib/vehicle_factory'
require './lib/vehicle'

RSpec.describe VehicleFactory do
before(:each) do
@factory = VehicleFactory.new
@data = [
{ vin_1_10: '123ABC4567', model_year: '2012', make: 'Toyota', model: 'Prius' },
{ vin_1_10: '456DEF7890', model_year: '2019', make: 'Tesla', model: 'Model S' }
]
end

it 'can create vehicles from external data' do
vehicles = @factory.create_vehicles(@data)

expect(vehicles.length).to eq(2)
expect(vehicles.first).to be_a(Vehicle)
expect(vehicles.first.vin).to eq('123ABC4567')
expect(vehicles.first.make).to eq('Toyota')
expect(vehicles.first.model).to eq('Prius')
expect(vehicles.first.engine).to eq(:ev)
end
end
Loading