Created
October 17, 2012 23:53
-
-
Save joshrowley/3909073 to your computer and use it in GitHub Desktop.
Object Oriented Jukebox (with Session class)
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, 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 = 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 | |
case command | |
when "help" | |
self.help | |
when "play" | |
self.play | |
when "list" | |
self.list | |
when "exit" | |
self.status = false | |
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