Created
October 17, 2012 22:05
-
-
Save joshrowley/3908612 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 add_to_library | |
@@song_library << self | |
end | |
def self.all | |
@@song_library | |
end | |
def initialize(name) | |
@name = 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 do |song| | |
Song.new(song).add_to_library | |
end | |
def help | |
puts "To see all songs and their corresponding track numbers, use list" | |
puts "To play a song, use play <track number>" | |
puts "For example, to play Abducted by The Cults, use play 5" | |
puts "To exit, use exit" | |
end | |
def play(song_selection) | |
song = Song.all[song_selection] | |
puts "Playing #{song.name}" | |
end | |
def list | |
Song.all.each_with_index { |song, index| | |
puts "#{index} => #{song.name}" | |
} | |
end | |
exit = false | |
puts "Hi, welcome to Jukebox." | |
while exit == false | |
print "Enter a command, or type help: " | |
command = gets.downcase.strip.split | |
case command.first | |
when "help" | |
help | |
when "play" | |
song_selection = command[1].to_i | |
play(song_selection) | |
when "list" | |
list | |
when "exit" | |
exit = true | |
else | |
puts "Sorry, that's not a valid command, type help if you need assistance." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment