-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_car.rb
More file actions
executable file
·173 lines (137 loc) · 5 KB
/
my_car.rb
File metadata and controls
executable file
·173 lines (137 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env ruby
# frozen_string_literal: true
# From https://launchschool.com/books/oo_ruby/read/classes_and_objects_part1#exercises
# 1. Create a class called MyCar. When you initialize a new instance or object of the class, allow the user to define
# some instance variables that tell us the year, color, and model of the car. Create an instance variable that is set
# to 0 during instantiation of the object to track the current speed of the car as well. Create instance methods that
# allow the car to speed up, brake, and shut the car off.
# 2. Add an accessor method to your MyCar class to change and view the color of your car. Then add an accessor method
# that allows you to view, but not modify, the year of your car.
# 3. You want to create a nice interface that allows you to accurately describe the action you want your program to
# perform. Create a method called spray_paint that can be called on an object and will modify the color of the car.
# From https://launchschool.com/books/oo_ruby/read/classes_and_objects_part2#exercises#
# 1. Add a class method to your MyCar class that calculates the gas mileage (i.e. miles per gallon) of any car.
# 2. Override the to_s method to create a user friendly print out of your object.
# From https://launchschool.com/books/oo_ruby/read/inheritance#exercises
# 1. Create a superclass called Vehicle for your MyCar class to inherit from and move the behavior that isn't
# specific to the MyCar class to the superclass. Create a constant in your MyCar class that stores information
# about the vehicle that makes it different from other types of Vehicles.
#
# Then create a new class called MyTruck that inherits from your superclass that also has a constant defined that
# separates it from the MyCar class in some way.
# 2. Add a class variable to your superclass that can keep track of the number of objects created that inherit from
# the superclass. Create a method to print out the value of this class variable as well.
class Vehicle
@@vehicle_count = 0
def self.vehicle_count
@@vehicle_count ||= 0
end
def self.gas_mileage(gallons, miles)
gm = miles.to_f / gallons
puts "#{gm} miles per gallon of gas"
gm
end
attr_reader :model, :year, :speed
attr_accessor :color
def initialize(model, year, color)
@speed = 0
@model = model
@year = year
@color = color
@@vehicle_count += 1
end
def current_speed
puts "Your current speed is #{@current_speed} mph."
end
def speed_up(incremental_speed)
puts "You are accelerating by #{incremental_speed} mph."
@speed += incremental_speed
end
def brake(decremental_speed)
puts "You are decelerating by #{decremental_speed} mph."
@speed -= decremental_speed
end
def shut_off
puts 'You are stopped'
@speed = 0
end
def spray_paint(color)
@color = color
puts "Your new #{color} paint job looks great!"
end
end
# For Truck
module Towable
def can_tow?(pound)
pound < 2000
end
end
class MyCar < Vehicle
NUMBER_OF_WHEELS = 4
def to_s
"My car is #{@model}/#{@year}/#{@color}"
end
end
class MyTruck < Vehicle
NUMBER_OF_WHEELS = 18
include Towable
def to_s
"My Truck is #{@model}/#{@year}/#{@color}"
end
end
# MyCarTest is a class designed to performed test over MyCar
class MyCarTest
attr_reader :id, :passed, :failed
def initialize
@id = 0
@passed = 0
@failed = 0
end
def verify_speed(car, action, param, expected)
result = (param ? car.send(action, param) : car.send(action)) == expected
update_counters(result)
puts "Test #{@id}: #{result ? 'PASS' : 'FAIL'} #{car} -> #{action}(#{param}) == #{expected}"
end
def verify_color(car, expected)
result = car.color == expected
update_counters(result)
puts "Test #{id}: #{result ? 'PASS' : 'FAIL'} #{car} is #{expected}"
end
def verify_instance_counter(expected)
result = Vehicle.vehicle_count == expected
update_counters(result)
puts "Test #{id}: #{result ? 'PASS' : 'FAIL'} instance count is #{expected}"
end
def to_s
"\nTest Summary: #{@passed}/#{@id} passed, #{@failed}/#{@id} failed"
end
def verify_gas_mileage(gallons, miles, expected)
result = MyCar.gas_mileage(gallons, miles)
update_counters(result == expected)
puts "Test #{id}: #{result == expected ? 'PASS' : 'FAIL'} Gas Mileage(#{gallons}, #{miles}) is #{expected} miles per gallon"
end
private
def update_counters(test_status)
@id += 1
if test_status
@passed += 1
else
@failed += 1
end
end
end
f150 = MyCar.new('F150', '2025', 'yellow')
test = MyCarTest.new
test.verify_instance_counter(1)
test.verify_speed(f150, 'shut_off', nil, 0)
test.verify_speed(f150, 'speed_up', 10, 10)
test.verify_speed(f150, 'speed_up', 10, 20)
test.verify_speed(f150, 'speed_up', 100, 120)
test.verify_speed(f150, 'brake', 50, 70)
test.verify_color(f150, 'yellow')
f150.color = 'blue'
test.verify_color(f150, 'blue')
f150.spray_paint('red')
test.verify_color(f150, 'red')
test.verify_gas_mileage(8, 100, 100.0 / 8.0)
puts test # print summary