Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.08 KB | None | 0 0
  1. require_relative 'station'
  2. require_relative 'route'
  3. require_relative 'train'
  4. require_relative 'railcar'
  5. require_relative 'passenger_train'
  6. require_relative 'cargo_train'
  7. require_relative 'passenger_railcar'
  8. require_relative 'cargo_railcar'
  9. require_relative 'instance_counter'
  10.  
  11. class App
  12.   @error
  13.   def start
  14.     loop do
  15.       options
  16.  
  17.       choice = gets.to_i
  18.       break if choice.zero?
  19.  
  20.       case choice
  21.       when 1
  22.         create_station
  23.       when 2
  24.         create_train
  25.       when 3
  26.         route_create
  27.       when 4
  28.         route_add
  29.       when 5
  30.         route_remove
  31.       when 6
  32.         set_route
  33.       when 7
  34.         create_railcar
  35.       when 8
  36.         attach_railcar
  37.       when 9
  38.         unhook_railcar
  39.       when 10
  40.         train_forward
  41.       when 11
  42.         train_backward
  43.       when 12
  44.         all_stations
  45.       when 13
  46.         show_trains
  47.       when 14
  48.         show_railcars
  49.       when 15
  50.         reserve_seat
  51.       when 16
  52.         fill_railcar
  53.       else
  54.         puts 'Enter the valid number'
  55.       end
  56.     end
  57.   end
  58.  
  59.   private
  60.  
  61.   attr_reader :stations, :routes, :trains
  62.  
  63.   def initialize
  64.     @stations = []
  65.     @trains   = []
  66.     @routes   = []
  67.     @railcars = []
  68.   end
  69.  
  70.   def options
  71.     puts [
  72.       '1. Create a station',
  73.       '2. Create a train',
  74.       '3. Create a route',
  75.       '4. Add station to the route',
  76.       '5. Remove station from the route',
  77.       '6. Set route for the train',
  78.       '7. Create railcar',
  79.       '8. Attach railcar',
  80.       '9. Unhook railcar',
  81.       '10. Move train to the next station',
  82.       '11. Move train to the previous station',
  83.       '12. See the stations list',
  84.       '13. See the trains list',
  85.       '14. See the railcars info',
  86.       '15. Reserve a seat',
  87.       '16. Fill railcar',
  88.       '0. Exit'
  89.     ]
  90.   end
  91.  
  92.   def create_station
  93.     puts 'Station name:'
  94.     @stations << Station.new(gets.chomp)
  95.   rescue => @error
  96.     error_output
  97.     retry
  98.   end
  99.  
  100.   def create_train
  101.     puts '1. Passenger Train'
  102.     puts '2. Cargo Train'
  103.     train_type = gets.to_i
  104.     wrong_number if train_type <= 0 || train_type > 2
  105.  
  106.     puts 'Train number:'
  107.     train_number = gets.chomp
  108.  
  109.     @trains << PassengerTrain.new(train_number) if train_type == 1
  110.     @trains << CargoTrain.new(train_number) if train_type == 2
  111.     puts "The train №#{train_number} successfully created"
  112.   rescue => @error
  113.     error_output
  114.     retry
  115.   end
  116.  
  117.   def route_create
  118.     puts 'Available stations:'
  119.     @stations.each.with_index(1) { |station, index| puts "#{index}. #{station.name}" }
  120.     puts 'First station number:'
  121.     first = gets.to_i - 1
  122.     puts 'Last station number:'
  123.     last = gets.to_i - 1
  124.  
  125.     wrong_number if first < 0 || first > @stations.size - 1
  126.     wrong_number if last < 0 || last > @stations.size - 1
  127.  
  128.     first_station = @stations[first]
  129.     last_station = @stations[last]
  130.     @routes << Route.new(first_station, last_station)
  131.   rescue => @error
  132.     error_output
  133.     retry
  134.   end
  135.  
  136.   def route_add
  137.     route_list
  138.     station_list
  139.     @routes[@route].add_station(@stations[station])
  140.   rescue => @error
  141.     error_output
  142.     retry
  143.   end
  144.  
  145.   def route_remove
  146.     route_list
  147.     puts 'Stations in the route:'
  148.     @routes[@route].stations.each.with_index(1) { |station, index| puts "#{index}. #{station.name}" }
  149.     puts 'Station number:'
  150.     number = gets.to_i - 1
  151.  
  152.     station_in_route = @routes[@route].stations[number]
  153.     @routes[@route].remove_station(station_in_route)
  154.   end
  155.  
  156.   def set_route
  157.     train_list
  158.     route_list
  159.     @trains[@train].route = @routes[@route]
  160.   end
  161.  
  162.   def create_railcar
  163.     puts 'Railcar type:'
  164.     puts '1. Passenger Railcar'
  165.     puts '2. Cargo Railcar'
  166.     type = gets.to_i
  167.  
  168.     if type == 1
  169.       puts 'Amount of seats:'
  170.       seats = gets.to_i
  171.       @railcars << PassengerRailcar.new(seats)
  172.     elsif type == 2
  173.       puts 'Railcar volume:'
  174.       volume = gets.to_f
  175.       @railcars << CargoRailcar.new(volume)
  176.     else
  177.       wrong_number
  178.     end
  179.   rescue => @error
  180.     error_output
  181.     retry
  182.   end
  183.  
  184.   def attach_railcar
  185.     puts 'Available railcars:'
  186.     @railcars.each_with_index(&railcar_info)
  187.     puts 'Railcar:'
  188.     railcar = @railcars[gets.to_i - 1]
  189.  
  190.     train_list
  191.     @trains[@train].attach_railcar(railcar)
  192.   rescue => @error
  193.     error_output
  194.     retry
  195.   end
  196.  
  197.   def unhook_railcar
  198.     train_list
  199.     @trains[@train].unhook_railcar
  200.   end
  201.  
  202.   def reserve_seat
  203.     railcar_action('PassengerTrain')
  204.     @trains[@train].railcars[@railcar].take_seat
  205.   end
  206.  
  207.   def fill_railcar
  208.     railcar_action('CargoTrain')
  209.     puts 'Volume:'
  210.     volume = gets.to_f
  211.     @trains[@train].railcars[@railcar].fill(volume)
  212.   end
  213.  
  214.   def train_forward
  215.     train_list
  216.     @trains[@train].forward
  217.   end
  218.  
  219.   def train_backward
  220.     train_list
  221.     @trains[@train].backward
  222.   end
  223.  
  224.   def show_trains
  225.     station_list
  226.     @stations[@number].pass_train(&train_info)
  227.   end
  228.  
  229.   def show_railcars
  230.     show_trains
  231.     puts 'Train:'
  232.     @train = gets.to_i - 1
  233.     @trains[@train].pass_railcar(&railcar_info)
  234.   end
  235.  
  236.   def train_list
  237.     puts 'Available trains:'
  238.     @trains.each.with_index(1) { |train, index| puts "#{index}. #{train}" }
  239.     puts 'Train:'
  240.     @train = gets.to_i - 1
  241.  
  242.     wrong_number if @train < 0 || @train > @trains.size - 1
  243.   end
  244.  
  245.   def station_list
  246.     all_stations
  247.     puts 'Station number:'
  248.     @number = gets.to_i - 1
  249.  
  250.     wrong_number if @number < 0 || @number > @stations.size - 1
  251.   end
  252.  
  253.   def route_list
  254.     puts 'Available routes'
  255.     @routes.each.with_index(1) { |route, index| puts "#{index}. #{route}" }
  256.     puts 'Route:'
  257.     @route = gets.to_i - 1
  258.  
  259.     wrong_number if @route < 0 || @route > @routes.size - 1
  260.   end
  261.  
  262.   def all_stations
  263.     puts 'Available stations:'
  264.     @stations.each.with_index(1) { |station, index| puts "#{index}. #{station.name}" }
  265.   end
  266.  
  267.   def railcar_info
  268.     proc do |railcar, index|
  269.       index += 1
  270.       info = "#{index}. Railcar №#{railcar.number}. Type: #{railcar.class}."
  271.       if railcar.class.to_s == 'PassengerRailcar'
  272.         puts info + "Available seats: #{railcar.free_seats}."\
  273.         " Reserved seats: #{railcar.reserved_seats}"
  274.       else
  275.         puts info + "Available volume: #{railcar.free_volume}."\
  276.         " Taken volume: #{railcar.filled_volume}"
  277.       end
  278.     end
  279.   end
  280.  
  281.   def train_info
  282.     proc do |train, index|
  283.       index += 1
  284.       puts "#{index}. Train №#{train.number}. Type: #{train.class}."\
  285.       " Amount of railcars: #{train.railcars.size}"
  286.     end
  287.   end
  288.  
  289.   def railcar_action(type)
  290.     station_list
  291.     type_train = @stations[@number].station_trains(type)
  292.     type_train.each_with_index(&train_info)
  293.     puts 'Train:'
  294.     @train = gets.to_i - 1
  295.     wrong_number if @train < 0 || @train > type_train.size
  296.  
  297.     @trains[@train].pass_railcar(&railcar_info)
  298.     puts 'Railcar:'
  299.     @railcar = gets.to_i - 1
  300.     wrong_number if @railcar < 0 || @railcar > @trains.size
  301.   end
  302.  
  303.   def error_output
  304.     puts "ERROR: #{@error.message}"
  305.   end
  306.  
  307.   def wrong_number
  308.     raise 'Enter the valid number'
  309.   end
  310. end
  311.  
  312. App.new.start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement