Created
September 13, 2013 00:53
-
-
Save steven-ferguson/6545734 to your computer and use it in GitHub Desktop.
Train System
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ScheduledStop | |
attr_reader :time, :stop_id, :id | |
def initialize(attributes) | |
@time = Time.new(2013, 9, 12, attributes['time'][0..1].to_i, attributes['time'][3..4].to_i) | |
@stop_id = attributes['stop_id'].to_i | |
@id = attributes['id'].to_i | |
end | |
def ==(another_scheduled_stop) | |
self.time == another_scheduled_stop.time && | |
self.stop_id == another_scheduled_stop.stop_id && | |
self.id == another_scheduled_stop.id | |
end | |
def save | |
@id = DB.exec("INSERT INTO scheduled_stops (time, stop_id) VALUES ('#{@time.strftime("%H:%M")}', #{@stop_id}) RETURNING id;").first['id'].to_i | |
end | |
def self.all | |
DB.exec("SELECT * FROM scheduled_stops").inject([]) do |scheduled_stops, result| | |
scheduled_stops << ScheduledStop.new(result) | |
end | |
end | |
def delete | |
DB.exec("DELETE FROM scheduled_stops WHERE id = #{@id};") | |
end | |
def get_stop | |
Stop.new(DB.exec("SELECT * FROM stops WHERE id = #{@stop_id}").first) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe ScheduledStop do | |
it "initializes with a hash of attributes" do | |
scheduled_stop = ScheduledStop.new({'time' => "09:04", 'stop_id' => 1}) | |
scheduled_stop.should be_an_instance_of ScheduledStop | |
end | |
it 'lets you read its stop id and its time' do | |
scheduled_stop = ScheduledStop.new({'time' => "09:04", 'stop_id' => 2}) | |
scheduled_stop.time.should eq Time.new(2013, 9, 12, 9, 4) | |
scheduled_stop.stop_id.should eq 2 | |
end | |
it 'lets you save it' do | |
scheduled_stop = ScheduledStop.new({'time' => "09:04", 'stop_id' => 2}) | |
scheduled_stop.save | |
ScheduledStop.all.should eq [scheduled_stop] | |
end | |
it 'is equal if the stop id and time are the same' do | |
scheduled_stop = ScheduledStop.new({'time' => "09:04", 'stop_id' => 2}) | |
scheduled_stop2 = ScheduledStop.new({'time' => "09:04", 'stop_id' => 2}) | |
scheduled_stop.should eq scheduled_stop2 | |
end | |
it 'lets you delete it' do | |
scheduled_stop = ScheduledStop.new({'time' => "09:04", 'stop_id' => 2}) | |
scheduled_stop.save | |
scheduled_stop2 = ScheduledStop.new({'time' => "09:14", 'stop_id' => 3}) | |
scheduled_stop2.save | |
scheduled_stop.delete | |
ScheduledStop.all.should eq [scheduled_stop2] | |
end | |
it 'lets you get its stop' do | |
stop = Stop.new({'train_station_id' => 1, 'train_line_id' => 2}) | |
stop.save | |
scheduled_stop = ScheduledStop.new({'time' => "09:04", 'stop_id' => stop.id}) | |
scheduled_stop.save | |
scheduled_stop.get_stop.should eq stop | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'pg' | |
require 'train_line' | |
require 'train_station' | |
require 'stop' | |
require 'scheduled_stops' | |
DB = PG.connect(:dbname => 'train_system_test') | |
RSpec.configure do |config| | |
config.after(:each) do | |
DB.exec("DELETE FROM train_lines *;") | |
DB.exec("DELETE FROM train_stations *;") | |
DB.exec("DELETE FROM stops *;") | |
DB.exec("DELETE FROM scheduled_stops *;") | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stop | |
attr_reader :id, :train_line_id, :train_station_id | |
def initialize(attributes) | |
@id = attributes['id'].to_i | |
@train_line_id = attributes['train_line_id'].to_i | |
@train_station_id = attributes['train_station_id'].to_i | |
end | |
def save | |
results = DB.exec("INSERT INTO stops (train_line_id, train_station_id) VALUES (#{@train_line_id}, #{@train_station_id}) RETURNING id;") | |
@id = results.first['id'].to_i | |
end | |
def self.all | |
DB.exec("SELECT * FROM stops;").inject([]) do |stops, result| | |
stops << Stop.new(result) | |
end | |
end | |
def ==(another_stop) | |
@id == another_stop.id && @train_line_id == another_stop.train_line_id && @train_station_id == another_stop.train_station_id | |
end | |
def delete | |
DB.exec("DELETE FROM stops WHERE id = #{id};") | |
end | |
def get_station | |
TrainStation.new(DB.exec("SELECT * FROM train_stations WHERE id = #{train_station_id};").first) | |
end | |
def get_line | |
TrainLine.new(DB.exec("SELECT * FROM train_lines WHERE id = #{train_line_id};").first) | |
end | |
def self.find(train_line, train_station) | |
Stop.new(DB.exec("SELECT * FROM stops WHERE train_line_id = #{train_line.id} AND train_station_id = #{train_station.id};").first) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Stop do | |
it "initializes with a hash of attributes" do | |
stop = Stop.new({'train_line_id' => 1, 'train_station_id' => 2}) | |
stop.should be_an_instance_of Stop | |
end | |
it 'lets you read its train_station_id and train_line_id and id' do | |
stop = Stop.new({'train_line_id' => 1, 'train_station_id' => 2, 'id' => 1}) | |
stop.id.should eq 1 | |
stop.train_line_id.should eq 1 | |
stop.train_station_id.should eq 2 | |
end | |
it 'lets you save it to the database' do | |
stop = Stop.new({'train_line_id' => 1, 'train_station_id' => 2}) | |
stop.save | |
Stop.all.length.should eq 1 | |
end | |
it 'lets you set the id when the database is saved' do | |
stop = Stop.new({'train_line_id' => 1, 'train_station_id' => 2}) | |
stop.save | |
Stop.all.first.id.should eq stop.id | |
end | |
it 'lets you delete a stop from the database' do | |
stop = Stop.new({'train_line_id' => 1, 'train_station_id' => 2}) | |
stop.save | |
stop.delete | |
Stop.all.should eq [] | |
end | |
it 'gets the station associated with the stop' do | |
train_station = TrainStation.new({'name' => 'Ikebukuro'}) | |
train_station.save | |
stop = Stop.new({'train_line_id' => 1, 'train_station_id' => train_station.id}) | |
stop.save | |
stop.get_station.should eq train_station | |
end | |
it 'gets the train line associated with the stop' do | |
train_line = TrainLine.new({"name" => 'blue'}) | |
train_line.save | |
stop = Stop.new({'train_line_id' => train_line.id, 'train_station_id' => 4}) | |
stop.save | |
stop.get_line.should eq train_line | |
end | |
it 'is the same if the train line id, train station id and id are the same' do | |
stop1 = Stop.new({'train_line_id' => 1, 'train_station_id' => 2, 'id' => 1}) | |
stop2 = Stop.new({'train_line_id' => 1, 'train_station_id' => 2, 'id' => 1}) | |
stop1.should eq stop2 | |
end | |
it 'finds the stop with the matching train_line and station' do | |
train_station = TrainStation.new({'name' => 'Central Tokyo'}) | |
train_station.save | |
train_line = TrainLine.new({'name' => 'Ginza'}) | |
train_line.save | |
stop = train_line.add_stop(train_station) | |
Stop.find(train_line, train_station).should eq stop | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrainLine | |
attr_reader :name, :id | |
def initialize(attributes) | |
@name = attributes['name'] | |
@id = attributes['id'].to_i | |
end | |
def ==(another_train_line) | |
self.name == another_train_line.name && | |
self.id == another_train_line.id | |
end | |
def self.all | |
DB.exec("SELECT * FROM train_lines;").inject([]) do |train_lines, result| | |
train_lines << TrainLine.new(result) | |
end | |
end | |
def save | |
results = DB.exec("INSERT INTO train_lines (name) VALUES ('#{@name}') RETURNING id;") | |
@id = results.first['id'].to_i | |
end | |
def delete | |
DB.exec("DELETE FROM train_lines WHERE id = #{@id};") | |
end | |
def add_stop(train_station) | |
stop = Stop.new({'train_station_id' => train_station.id, 'train_line_id' => @id}) | |
stop.save | |
stop | |
end | |
def get_stops | |
DB.exec("SELECT * FROM stops WHERE train_line_id = #{@id};").inject([]) do |stops, result| | |
stops << Stop.new(result) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe TrainLine do | |
it 'is initialized with an attributes hash' do | |
train_line = TrainLine.new({'name' => 'blue line'}) | |
train_line.should be_an_instance_of TrainLine | |
end | |
it 'lets you read its name and id' do | |
train_line =TrainLine.new({'name' => 'blue line', 'id' => 1}) | |
train_line.name.should eq 'blue line' | |
train_line.id.should eq 1 | |
end | |
it 'is equal if the id and name are the same' do | |
train_line =TrainLine.new({'name' => 'blue line', 'id' => 1}) | |
train_line2 =TrainLine.new({'name' => 'blue line', 'id' => 1}) | |
train_line.should eq train_line2 | |
end | |
it 'lets you see all the train lines' do | |
TrainLine.all.should eq [] | |
end | |
it 'lets you save it to the database' do | |
train_line =TrainLine.new({'name' => 'blue line'}) | |
train_line.save | |
TrainLine.all.should eq [train_line] | |
end | |
it 'sets the id when saved' do | |
train_line = TrainLine.new({'name' => 'red line'}) | |
train_line.save | |
TrainLine.all.first.id.should eq train_line.id | |
end | |
it 'lets you delete the train line' do | |
train_line1 = TrainLine.new({'name' => 'red line'}) | |
train_line1.save | |
train_line2 = TrainLine.new({'name' => 'blue line'}) | |
train_line2.save | |
train_line1.delete | |
TrainLine.all.should eq [train_line2] | |
end | |
it 'lets you add a stop to the line' do | |
train_line1 = TrainLine.new({'name' => 'red line'}) | |
train_line1.save | |
train_station1 = TrainStation.new({'name' => 'Main Street Station'}) | |
train_station1.save | |
train_line1.add_stop(train_station1) | |
Stop.all.length.should eq 1 | |
end | |
it 'gets all the stops for a line' do | |
train_line = TrainLine.new({'name' => 'red line'}) | |
train_line.save | |
train_station = TrainStation.new({'name' => 'Ikebukuro'}) | |
train_station.save | |
stop = train_line.add_stop(train_station) | |
train_line.get_stops.should eq [stop] | |
end | |
it 'returns an empty array if there are no stops asscoiated with a train line' do | |
train_line = TrainLine.new({'name' => 'red line'}) | |
train_line.save | |
train_line.get_stops.should eq [] | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TrainStation | |
attr_reader :name, :id | |
def initialize(attributes) | |
@name = attributes['name'] | |
@id = attributes['id'].to_i | |
end | |
def self.all | |
DB.exec("SELECT * FROM train_stations;").inject([]) do |train_stations, result| | |
train_stations << TrainStation.new(result) | |
end | |
end | |
def save | |
results = DB.exec("INSERT INTO train_stations (name) VALUES ('#{@name}') RETURNING id;") | |
@id = results.first['id'].to_i | |
end | |
def ==(another_station) | |
@id == another_station.id && @name == another_station.name | |
end | |
def delete | |
DB.exec("DELETE FROM train_stations WHERE id = #{@id};") | |
end | |
def get_stops | |
DB.exec("SELECT * FROM stops WHERE train_station_id = #{@id};").inject([]) do |stops, result| | |
stops << Stop.new(result) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe TrainStation do | |
it 'is initialzed with a hash of attributes' do | |
train_station = TrainStation.new({'name' => 'Main St. Station'}) | |
train_station.should be_an_instance_of TrainStation | |
end | |
it 'lets you see its name and id' do | |
train_station = TrainStation.new({'name' => 'Main St. Station', 'id' => 1}) | |
train_station.name.should eq 'Main St. Station' | |
train_station.id.should eq 1 | |
end | |
it 'lets you save to the database' do | |
train_station = TrainStation.new({'name' => 'Grand Central Station'}) | |
train_station.save | |
puts TrainStation.all | |
TrainStation.all.length.should eq 1 | |
end | |
it 'sets the id when saved' do | |
train_station = TrainStation.new({'name' => 'Shinjuku'}) | |
train_station.save | |
TrainStation.all.first.id.should eq train_station.id | |
end | |
it 'is equal if the name and id are equal' do | |
train_station1 = TrainStation.new({'name' => 'Grand Central Station', 'id' => 1}) | |
train_station2 = TrainStation.new({'name' => 'Grand Central Station', 'id' => 1}) | |
train_station1.should eq train_station2 | |
end | |
it 'lets you delete it from the database' do | |
train_station1 = TrainStation.new({'name' => 'Ikebukuro'}) | |
train_station1.save | |
train_station2 = TrainStation.new({'name' => 'Shinjuku'}) | |
train_station2.save | |
train_station1.delete | |
TrainStation.all.should eq [train_station2] | |
end | |
it 'gets all the stops which are associated with it' do | |
train_station = TrainStation.new({'name' => 'Shinjuku'}) | |
train_station.save | |
stop = Stop.new({'train_station_id' => train_station.id, 'train_line_id' => 2}) | |
stop.save | |
train_station.get_stops.should eq [stop] | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './lib/train_station.rb' | |
require './lib/train_line.rb' | |
require './lib/stop.rb' | |
require './lib/scheduled_stops.rb' | |
require 'pg' | |
DB = PG.connect(:dbname => 'train_system') | |
def welcome | |
puts "Welcome to the train system. Are you a train [r]ider or an [o]perator?" | |
input = gets.chomp | |
if input == 'r' | |
rider_menu | |
elsif input == 'o' | |
operator_menu | |
end | |
end | |
def operator_menu | |
puts "\nPress 1 to add a new train line" | |
puts "Press 2 to delete a train line" | |
puts "Press 3 to add a train station" | |
puts "Press 4 to delete a train station" | |
puts "Press 5 to add a stop to a train line" | |
puts "Press 6 to view details about a train line" | |
puts "Press 7 to view details about a station" | |
puts "Press 8 to add a scheduled stop for a train line" | |
puts "Press 0 to exit" | |
user_choice = gets.to_i | |
if user_choice == 1 | |
add_train_line | |
elsif user_choice == 2 | |
delete_train_line | |
elsif user_choice == 3 | |
add_train_station | |
elsif user_choice == 4 | |
delete_train_station | |
elsif user_choice == 5 | |
add_stop | |
elsif user_choice == 6 | |
train_line_menu | |
elsif user_choice == 7 | |
train_station_menu | |
elsif user_choice == 8 | |
add_scheduled_stop_menu | |
elsif user_choice == 0 | |
puts "goodbye!" | |
end | |
end | |
def rider_menu | |
puts "\nPress 1 to see train lines" | |
puts "Press 2 to see all the train stations" | |
puts "Press 0 to go back" | |
user_choice = gets.to_i | |
if user_choice == 1 | |
train_line_menu | |
elsif user_choice == 2 | |
train_station_menu | |
elsif user_choice == 0 | |
welcome | |
end | |
end | |
def train_line_menu | |
puts "\nSelect a train line you would like to view" | |
display_train_lines | |
train_lines = TrainLine.all | |
index = gets.to_i - 1 | |
display_stations_on(train_lines[index]) | |
rider_menu | |
end | |
def train_station_menu | |
puts "\nSelect the train station you would like to view" | |
display_train_stations | |
train_stations = TrainStation.all | |
index = gets.to_i - 1 | |
display_lines_at(train_stations[index]) | |
rider_menu | |
end | |
def add_train_line | |
print "\nEnter a name for the new train line:\t" | |
name = gets.chomp | |
train_line = TrainLine.new({'name' => name}) | |
train_line.save | |
puts "\nYou have added #{train_line.name} as a new train line. Its id is: #{train_line.id}." | |
operator_menu | |
end | |
def delete_train_line | |
display_train_lines | |
train_lines = TrainLine.all | |
print "\nEnter the number of the train line you would like to delete:\t" | |
index = gets.chomp.to_i | |
train_lines[index - 1].delete | |
puts "The #{train_lines[index - 1].name} has been deleted." | |
operator_menu | |
end | |
def display_train_lines | |
puts "\n\n" | |
train_lines = TrainLine.all | |
train_lines.each_with_index do |train_line, index| | |
puts "#{index + 1}. #{train_line.name}" | |
end | |
end | |
def add_train_station | |
print "\nEnter a name for the new station:\t" | |
name = gets.chomp | |
train_station = TrainStation.new({'name' => name}) | |
train_station.save | |
puts "\nYou have added #{train_station.name} as a new station." | |
operator_menu | |
end | |
def delete_train_station | |
display_train_stations | |
train_stations = TrainStation.all | |
print "\nEnter the number of the train station you would like to delete:\t" | |
index = gets.to_i - 1 | |
train_stations[index].delete | |
puts "#{train_stations[index].name} has been deleted as a station." | |
operator_menu | |
end | |
def display_train_stations | |
puts"\n\n" | |
train_stations = TrainStation.all | |
train_stations.each_with_index do |train_station, index| | |
puts "#{index + 1}. #{train_station.name}" | |
end | |
end | |
def add_stop | |
display_train_lines | |
puts "Select the line you would like to add a stop to." | |
train_line = TrainLine.all[gets.to_i - 1] | |
puts "\n #{train_line.name} has been selected." | |
display_train_stations | |
puts "\nSelect the station you would like to add as a stop for the selected line." | |
station = TrainStation.all[gets.to_i - 1] | |
train_line.add_stop(station) | |
puts "\n#{station.name} has been added as a stop on the #{train_line.name} line." | |
operator_menu | |
end | |
def display_stations_on(train_line) | |
stops = train_line.get_stops | |
if stops.empty? | |
puts "There are crrently no stops on this line." | |
else | |
puts "Stops on the #{train_line.name} line are:" | |
puts "_____________________________" | |
stops.each_with_index do |stop, index| | |
puts "#{index + 1} #{stop.get_station.name}" | |
end | |
puts "_____________________________" | |
end | |
end | |
def display_lines_at(train_station) | |
stops = train_station.get_stops | |
if stops.empty? | |
puts "There are currently no lines stopping at this station." | |
else | |
puts "Lines stopping at this station are:" | |
puts "_____________________________" | |
stops.each_with_index do |stop, index| | |
puts "#{index + 1} #{stop.get_line.name} Line" | |
end | |
puts "_____________________________" | |
end | |
end | |
def add_scheduled_stop_menu | |
display_train_lines | |
puts "Select the train line you would like to add a scheduled stop to" | |
train_line = TrainLine.all[gets.to_i - 1] | |
display_stations_on(train_line) | |
puts "Select the station for the scheduled stop" | |
train_station = TrainStation.all[gets.to_i - 1] | |
stop = Stop.find(train_line, train_station) | |
puts "What time would you like to add to the stop? 00:00" | |
scheduled_stop = ScheduledStop.new({'time' => gets.chomp, 'stop_id' => stop.id}) | |
scheduled_stop.save | |
puts "The stop has been scheduled for #{scheduled_stop.time.strftime("%H:%M")}" | |
operator_menu | |
end | |
welcome | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment