-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
183 lines (159 loc) · 4.61 KB
/
app.rb
File metadata and controls
183 lines (159 loc) · 4.61 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
174
175
176
177
178
179
180
181
182
183
require_relative 'modules/accessors'
require_relative 'modules/instance_counter'
require_relative 'modules/manufacturer'
require_relative 'modules/menu'
require_relative 'modules/selectors'
require_relative 'modules/validation'
require_relative 'route'
require_relative 'station'
require_relative 'train'
require_relative 'train/cargo_train'
require_relative 'train/passenger_train'
require_relative 'wagon'
require_relative 'wagon/cargo_wagon'
require_relative 'wagon/passenger_wagon'
class App
include Menu
include Selectors
attr_accessor :routes, :stations, :trains
def initialize
@routes = []
@stations = []
@trains = []
end
def add_station
attempt = 0
print '[?] введите название станции: '
station_name = gets.chomp
@stations << Station.new(station_name)
puts "[+] станция '#{station_name}' успешно создана"
wait_pressing
rescue StandardError => e
error(e)
attempt += 1
retry if attempt < 5
end
def add_train
attempt = 0
number, type = ask_train
add_train!(number, type)
print "[+] #{type.eql?(:cargo) ? 'грузовой' : 'пассажирский'}"
puts " поезд с номером #{number} успешно создан"
wait_pressing
rescue StandardError => e
error(e)
attempt += 1
retry if attempt < 5
end
def manage_route
raise 'сперва добавьте две станции' if @stations.size < 2
case selects_route_actions(@routes.empty?)
when 0 then add_route
when 1 then add_station_to_route
when 2 then remove_station_from_route
end
rescue StandardError => e
error(e)
wait_pressing
end
def add_route
attempt = 0
source, destination = select_stations
route = Route.new(@stations[source], @stations[destination])
@routes << route
puts "[+] маршрут '#{route.name}' добавлен"
wait_pressing
rescue StandardError => e
error(e)
attempt += 1
retry if attempt < 5
end
def add_station_to_route
station = select_station
route = select_route
route.add(station)
puts "[+] станция #{station.name.capitalize} добавлена к маршруту"
wait_pressing
end
def remove_station_from_route
station = select_station
route = select_route
route.remove(station)
puts "[+] станция #{station.name.capitalize} удалена из маршрута"
wait_pressing
end
def set_route_to_train
raise 'добавьте поезда и маршруты' if @trains.empty? || @routes.empty?
train = select_train
train.route = select_route
puts "[+] поезду №#{train.number} назначен маршрут '#{train.route.name}'"
wait_pressing
rescue StandardError => e
error(e)
wait_pressing
end
def move_train
raise 'список поездов пуст' if @trains.empty?
train = select_train
case selects_train_actions
when 0 then to_next_station(train)
when 1 then to_previous_station(train)
end
rescue StandardError => e
error(e)
wait_pressing
end
def manage_wagon
attempt = 0
raise 'список поездов пуст' if @trains.empty?
train = select_train
case_wagon_action(train)
rescue StandardError => e
error(e)
attempt += 1
retry if attempt < 5
end
private
def case_wagon_action(train)
case selects_wagon_actions(train.wagons.empty?)
when 0
wagon = train.cargo? ? create_cargo_wagon : create_passenger_wagon
train.attach_wagon(wagon)
puts '[+] вагон успешно прицеплен'
wait_pressing
when 1 then detach_wagon(train)
when 2 then load_wagon(train)
end
end
def add_train!(number, type)
case type
when :cargo then @trains << CargoTrain.new(number)
when :passenger then @trains << PassengerTrain.new(number)
end
end
def load_wagon(train)
wagon = select_wagon(train)
if wagon.cargo?
puts '[?] объем: '
wagon.load!(gets.to_i)
puts '[!] вагон успешно загружен'
else
wagon.load!
puts '[!] место успешно занято'
end
end
def detach_wagon(train)
wagon = select_wagon(train)
train.detach_wagon(wagon)
puts '[+] вагон успешно отцеплен'
wait_pressing
end
def to_next_station(train)
train.forward
puts "[+] поезд прибыл на станцию #{train.current_station.name}"
end
def to_previous_station(train)
train.backward
puts "[+] поезд вернулся на станцию #{train.current_station.name}"
end
end