-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparking.rb
153 lines (143 loc) · 4.62 KB
/
parking.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
class ParkingGarage
attr_reader :parking_spots, :small, :medium, :large
def initialize(small, medium, large)
@small = small
@medium = medium
@large = large
@parking_spots = {
small_spot: [],
medium_spot: [],
large_spot: []
}
end
def admit_car(license_plate_no, car_size)
car = { car: license_plate_no, size: car_size }
case car_size
when 'small'
if @small > 0
@parking_spots[:small_spot] << car
@small -= 1
parking_status(car, 'small')
elsif @medium > 0
@parking_spots[:medium_spot] << car
@medium -= 1
parking_status(car, 'medium')
elsif @large > 0
@parking_spots[:large_spot] << car
@large -= 1
parking_status(car, 'large')
else
parking_status
end
when 'medium'
if @medium > 0
@parking_spots[:medium_spot] << car
@medium -= 1
parking_status(car, 'medium')
elsif @large > 0
@parking_spots[:large_spot] << car
@large -= 1
parking_status(car, 'large')
elsif @medium.zero? && @large.zero?
shuffle_medium(car)
else
parking_status
end
when 'large'
if @large > 0
parking_spots[:large_spot] << car
@large -= 1
parking_status(car, 'large')
elsif @large.zero?
shuffle_large(car)
else
parking_status
end
end
end
def exit_car(license_plate_no)
small_car = @parking_spots[:small_spot].find { |car| car[:car] == license_plate_no }
medium_car = @parking_spots[:medium_spot].find { |car| car[:car] == license_plate_no }
large_car = @parking_spots[:large_spot].find { |car| car[:car] == license_plate_no }
if small_car
@parking_spots[:small_spot].delete(small_car)
@small += 1
exit_status(license_plate_no)
elsif medium_car
@parking_spots[:medium_spot].delete(medium_car)
@medium += 1
exit_status(license_plate_no)
elsif large_car
@parking_spots[:large_spot].delete(large_car)
@large += 1
exit_status(license_plate_no)
else
exit_status
end
end
def shuffle_medium(car)
if [email protected]? && (@parking_spots[:medium_spot].any? do |car|
car[:size] == 'small'
end || @parking_spots[:large_spot].any? do |car|
car[:size] == 'small'
end)
small_at_medium = @parking_spots[:medium_spot].find { |car| car[:size] == 'small' }
small_at_large = @parking_spots[:large_spot].find { |car| car[:size] == 'small' }
if small_at_medium
@parking_spots[:medium].delete(small_at_medium)
@parking_spots[:medium] << car
parking_status(car, 'medium')
@parking_spots[:small] << small_at_medium
@small -= 1
parking_status(small_at_medium, 'small')
elsif small_at_large
@parking_spots[:large].delete(small_at_large)
@parking_spots[:large] << car
parking_status(car, 'large')
@parking_spots[:small] << small_at_large
@small -= 1
parking_status(small_at_large, 'small')
end
end
end
def shuffle_large(car)
if [email protected]? && @parking_spots[:large_spot].any? { |car| car[:size] == 'medium' }
medium_at_large = @parking_spots[:large_spot].find { |car| car[:size] == 'medium' }
@parking_spots[:large_spot].delete(medium_at_large)
@parking_spots[:large_spot] << car
parking_status(car, 'large')
@parking_spots[:medium_spot] << medium_at_large
@medium -= 1
parking_status(medium_at_large, 'medium')
end
if ([email protected]? || [email protected]?) && @parking_spots[:large_spot].any? { |car| car[:size] == 'small' }
small_at_large = @parking_spots[:large_spot].find { |car| car[:size] == 'small' }
@parking_spots[:large_spot].delete(small_at_large)
@parking_spots[:large_spot] << car
parking_status(car, 'large')
@parking_spots[:small_spot] << small_at_large
@small -= 1
parking_status(small_at_large, 'small')
elsif [email protected]?
@parking_spots[:medium_spot] << small_at_large
@medium -= 1
parking_status(small_at_large, 'medium')
end
end
end
def parking_status(car = nil, space = nil)
if car && space
puts "car with license plate no. #{car[:car]} is parked at #{space}"
else
puts 'No space available'
end
end
def exit_status(license_plate = nil)
if license_plate
puts "car with license plate no. #{license_plate} exited"
else
puts 'Car not found!'
end
end
end