Created
October 19, 2012 02:22
-
-
Save joshrowley/3915908 to your computer and use it in GitHub Desktop.
Object Oriented Jukebox
This file contains 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 Song | |
attr_accessor :name | |
@@song_library = [] | |
def self.all | |
@@song_library | |
end | |
def initialize(name) | |
@name = name | |
@@song_library << self | |
end | |
end | |
class Session | |
attr_accessor :status, :user_input | |
def initialize | |
@status = true | |
puts "Hi, welcome to Jukebox." | |
end | |
def help | |
puts "To see all songs and their corresponding track numbers, type list" | |
puts "To play a song, type play <track number>" | |
puts "For example, to play Abducted by The Cults, type play 5" | |
puts "To exit, type exit" | |
end | |
def play | |
song_selection = self.user_input[1].to_i | |
song = Song.all[song_selection] | |
puts "Playing #{song.name}" | |
end | |
def prompt | |
print "Enter a command, or type help: " | |
self.user_input = gets.downcase.strip.split | |
command = self.user_input.first.to_sym | |
allowed_inputs = [:help, :list, :prompt] | |
if allowed_inputs.include?(command) | |
self.send(command) | |
else | |
puts "Sorry, that's not a valid command, type help if you need assistance." | |
end | |
end | |
def list | |
Song.all.each_with_index { |song, index| | |
puts "#{index} => #{song.name}" | |
} | |
end | |
end | |
songs = [ | |
"The Phoenix - 1901", | |
"Tokyo Police Club - Wait Up", | |
"Sufjan Stevens - Too Much", | |
"The Naked and the Famous - Young Blood", | |
"(Far From) Home - Tiga", | |
"The Cults - Abducted", | |
"The Phoenix - Consolation Prizes" | |
] | |
songs.each { |song| Song.new(song) } | |
session = Session.new | |
while session.status | |
session.prompt | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment