-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise2.rb
159 lines (136 loc) · 4.08 KB
/
exercise2.rb
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
# Objetivo:
# Diseñar un mini sistema para administrar un taller mecánico de vehículos. El taller puede
# reparar motocicletas, carros, SUVs, minivans y pickups.
#
# Generar un catálogo de vehículos en taller y una historia de los que ya se han reparado, así como
# el estado en el que están los que están en proceso (recibido, en taller, por entregar, etc.)
#
# Agrega métodos relevantes para contestar las siguientes preguntas:
# - Cuántos vehículos tengo ahorita en reparación?
# - Qué tipo de vehículos he reparado más? y menos?
# - Qué año son los carros que tengo en reparación?
# - Cuántos vehículos he entregado?
# - Cuántos vehículos he tenido y tengo en total dependiendo del tipo?
class VehicleRepairShop
attr_accessor :vehicles
def initialize(vehicles)
@vehicles = vehicles
end
def count_of_vehicles_delivered
count = 0
@vehicles.each do |vehicle|
count += 1 if vehicle.status == "delivered"
end
count
end
def count_of_vehicles_in_repair
count = 0
@vehicles.each do |vehicle|
count += 1 if vehicle.status == "in_repair"
end
count
end
def years_of_vehicles_in_repair
years = []
@vehicles.each do |vehicle|
years.push(vehicle.year) if vehicle.status == "in_repair"
end
years
end
def most_repaired_vehicles
counters = {
car: 0,
motorcycle: 0,
suv: 0,
pickuptruck: 0,
minivan: 0
}
@vehicles.each do |vehicle|
if vehicle.status == "delivered"
counters[:car] += 1 if vehicle.is_a?(Car)
counters[:motorcycle] += 1 if vehicle.is_a?(Motorcycle)
counters[:suv] += 1 if vehicle.is_a?(SUV)
counters[:pickuptruck] += 1 if vehicle.is_a?(PickUpTruck)
counters[:minivan] += 1 if vehicle.is_a?(Minivan)
end
end
biggest_count = 0
biggest_type = nil
counters.each do |type, count|
if count > biggest_count
biggest_count = count
biggest_type = type
end
end
biggest_type
end
def count_of_vehicles_by_type
counters = {
car: 0,
motorcycle: 0,
suv: 0,
pickuptruck: 0,
minivan: 0
}
@vehicles.each do |vehicle|
counters[:car] += 1 if vehicle.is_a?(Car)
counters[:motorcycle] += 1 if vehicle.is_a?(Motorcycle)
counters[:suv] += 1 if vehicle.is_a?(SUV)
counters[:pickuptruck] += 1 if vehicle.is_a?(PickUpTruck)
counters[:minivan] += 1 if vehicle.is_a?(Minivan)
end
counters
end
end
class Vehicle
attr_accessor :make
attr_accessor :model
attr_accessor :year
attr_accessor :color
attr_accessor :owner_name
attr_accessor :status # received, in_repair, done, delivered
attr_accessor :number_of_wheels
attr_accessor :engine
attr_accessor :fuel_type
def initialize(make, model, year, color, owner_name, status = "received")
@make = make
@model = model
@year = year
@color = color
@owner_name = owner_name
@status = status
end
end
class Motorcycle < Vehicle
def initialize(make, model, year, color, owner_name)
super(make, model, year, color, owner_name)
@number_of_wheels = 2
end
end
class FourWheelVehicle < Vehicle
def initialize(make, model, year, color, owner_name)
super(make, model, year, color, owner_name)
@number_of_wheels = 4
end
end
class Car < FourWheelVehicle
end
class SUV < FourWheelVehicle
end
class Minivan < FourWheelVehicle
end
class PickUpTruck < FourWheelVehicle
end
mini = Car.new("MINI", "Cooper", 2016, "Blue", "Abraham Kuri")
ducati = Motorcycle.new("Ducati", "848", 2018, "Red", "Carlos Medellín")
ducati.status = "in_repair"
sienna = Minivan.new("Toyota", "Sienna", 2015, "White", "Pedro Lopez")
sienna.status = "delivered"
rav4 = SUV.new("Toyota", "RAV4", 2010, "Gray", "Luis Perez")
rav4.status = "delivered"
cheyenne = PickUpTruck.new("Chevrolet", "Cheyenne", 2017, "Yellow", "Brayan Rodríguez")
cheyenne.status = "delivered"
titan = PickUpTruck.new("Nissan", "Titan", 2014, "Gray", "Rafael Cárdenas")
titan.status = "delivered"
vehicles = [mini, ducati, sienna, rav4, cheyenne, titan]
shop = VehicleRepairShop.new(vehicles)