Skip to content

Instantly share code, notes, and snippets.

@fee1good
Created September 8, 2016 15:48
Show Gist options
  • Save fee1good/ecf39dee5d1e5e77d14a8682393abf6c to your computer and use it in GitHub Desktop.
Save fee1good/ecf39dee5d1e5e77d14a8682393abf6c to your computer and use it in GitHub Desktop.
class Train
attr_accessor :number
attr_accessor :type
attr_accessor :wagons_count
attr_accessor :speed
attr_accessor :current_route
def initialize(number, type, wagons_count)
@trains_type = ["passanger", "cargo"]
if @trains_type.include?(type)
@number = number
@type = type
@wagons_count = wagons_count
else
puts "Тип поезда должен быть cargo или passanger."
end
end
def to_s
"#{number}, #{type}, #{wagons_count}"
end
def increase_speed(speed)
@speed += speed
end
def show_current_speed
puts @speed
end
def stop
@speed = 0
end
def show_wagon_count
puts @wagon_count
end
def add_wagon(number)
if @speed > 0
puts "Необходимо остановить поезд, чтобы прицепть вагон"
else
@wagon_number += number
end
end
def use_route(route)
@current_route = route
end
def move_to_the_station(station)
station.add_train(self.Train)
end
def show_stations
@full_route.select{|station| @trains_on_station[self.Train]}
end
def delete_wagon
if @wagons_count <= 0
abort
end
@wagons_count -= 1
end
end
class Station
attr_accessor :trains_on_station
def initialize(name)
@name = name
@trains_on_station = []
end
def to_s
"#{@name}"
end
def add_train(train)
@trains_on_station << train
end
def show_trains
puts "Всего поездов на станции: #{@trains_on_station.length}"
puts 'Грузовые поезда: ' + @trains_on_station.select{|train| train.type == 'cargo'}
puts 'Пассажирские поезда: ' + @trains_on_station.select{|train| train.type == 'passanger'}
end
def delete_train(train)
trains_on_station.delete(train)
end
end
class Route
attr_accessor :full_route
def initialize(station_start, station_end)
@full_route = [station_start, station_end]
end
def station_start
@full_route.first
end
def station_end
@full_route.last
end
def add_station (station)
@full_route.insert(-2, station)
end
def delete_station(station)
if [station_start, station_end].include?(station)
abort 'Вы не можете удалить начальный и конечный пункт назначения'
else
@full_route.delete(station)
end
end
def show_route
@full_route.each{|station| puts station}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment