Skip to content

Instantly share code, notes, and snippets.

@ndreynolds
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ndreynolds/7e74f7509cca8d54b3fe to your computer and use it in GitHub Desktop.
Save ndreynolds/7e74f7509cca8d54b3fe to your computer and use it in GitHub Desktop.
Zug Simulator

Zug Simulator

In this exercise, we'll model the behavior of an U-Bahn or S-Bahn train.

Given a route which provides stations (just strings), the train's current station, and whether or not it's traveling in reverse (i.e., from the last stop to the first stop), the train should complete the route in the correct direction.

When the train's complete_route method is called, it should print a summary of the stops to standard output. The first line should announce the route and where it's headed. The following lines should list the stations traveled to. After the last stop, print "Endbahnhof" to signal the end of the route.

Here's a complete example of the expected output for the U8 route from Wittenau to Hermannstraße, starting from Schönleinstraße and traveling in the forward direction.

U8 Richtung Hermannstrasse
Hermannplatz
Boddinstrasse
Leinestrasse
Hermannstrasse
Endbahnhof
# Routes are composed of a name (e.g., U8) and an array of stations.
#
# To use one of the sample routes to test your train class, e.g.:
#
# train = Train.new(Route::BVG.u8, 'Wittenau')
#
class Route
attr_reader :name, :stations
def initialize(name, stations)
@name, @stations = name, stations
end
# Example routes that can be used for testing:
module BVG
def self.u8
Route.new('U8', [
'Wittenau',
'Rathaus Reinickendorf',
'Karl-Bonhoeffer-Nervenklinik',
'Lindauer Allee',
'Paracelsus-Bad',
'Residenzstrasse',
'Franz-Neumann-Platz',
'Osloer Strasse',
'Pankstrasse',
'Gesundbrunnen',
'Voltastrasse',
'Bernauer Strasse',
'Rosenthaler Platz',
'Weinmeisterstrasse',
'Alexanderplatz',
'Jannowitzbruecke',
'Heinrich-Heine-Strasse',
'Moritzplatz',
'Kottbusser Tor',
'Schoenleinstrasse',
'Hermannplatz',
'Boddinstrasse',
'Leinestrasse',
'Hermannstrasse',
])
end
def self.s75
Route.new('S75', [
'Westkreuz',
'Charlottenburg',
'Savignyplatz',
'Zoologischer Garten',
'Tiergarten',
'Bellevue',
'Hauptbahnhof',
'Friedrichstrasse',
'Hackescher Markt',
'Alexanderplatz',
'Jannowitzbruecke',
'Ostbahnhof',
'Warschauer Strasse',
'Ostkreuz',
'Noeldnerplatz',
'Lichtenberg',
'Friedrichsfelde Ost',
'Springpfuhl',
'Gehrenseestrasse',
'Hohenschoenhausen',
'Wartenberg',
])
end
end
end
class Train
def initialize(route, current_station, reverse = false)
# ...
end
def complete_route
# ...
end
# define additional methods here...
end
require './lib/train'
describe Train do
describe '#initialize' do
end
describe '#complete_route' do
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment