Created
October 18, 2012 05:22
-
-
Save styliii/3910000 to your computer and use it in GitHub Desktop.
JukeBox Challenge
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 JukeBox | |
# attr_accessor :request | |
@@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" | |
] | |
def initialize | |
print "Hello. I am a jukebox. " | |
self.prompt_user | |
end | |
def prompt_user | |
print "What would you like me to do? " | |
request = self.take_request | |
self.execute_request(request) | |
end | |
def take_request | |
request = gets | |
request = request.chomp.downcase | |
end | |
def take_response | |
response = gets | |
response = response.chomp | |
if response.downcase == "exit" || response.downcase == "list" | |
self.execute_request(response) | |
else | |
response | |
end | |
end | |
def execute_request(request) | |
if self.respond_to?(request.to_sym) | |
self.send(request.to_sym) | |
else | |
print "Sorry, I don't understand. Your options are help, list, play and exit. " | |
request = self.take_request | |
self.execute_request(request) | |
end | |
end | |
def help | |
puts "'list' will show you a list of songs to play" | |
puts "'play' will play a song that you will select" | |
puts "'exit' will sadly close me down" | |
request = self.take_request | |
self.execute_request(request) | |
end | |
def play | |
print "Which song would you like to play? " | |
song = self.take_response | |
# determine if song is an integer, the full title, or partial | |
song_index = self.validates_song(song) | |
self.play_song(song_index) | |
end | |
def play_song(song_index) | |
puts "...playing #{@@songs[song_index]}. Love this song!" | |
puts "Anything else I can help you with?" | |
request = self.take_request | |
self.execute_request(request) | |
end | |
def validates_song(song_input) | |
if song_input.to_i != 0 && song_input.to_i <= @@songs.length | |
song_index = song_input.to_i - 1 | |
elsif @@songs.include?(song_input) | |
song_index = @@songs.index(song_input) | |
else | |
self.possible_match(song_input) | |
end | |
end | |
def possible_match(song_input) | |
possible_songs = @@songs.map do |song_selection| | |
song_selection if song_selection.include?(song_input) | |
end | |
possible_songs.compact! | |
if possible_songs.empty? | |
puts "Sorry, I don't recognize your song selection." | |
self.play | |
else | |
puts "Which one of these songs?" | |
possible_songs.each_with_index do |song, index| | |
puts "#{index + 1} #{song}" | |
end | |
song = self.take_response | |
self.validates_song(song) | |
end | |
end | |
def list | |
@@songs.each_with_index{|song, i| puts "#{i + 1} #{song}"} | |
self.prompt_user | |
end | |
def exit | |
puts "Good Bye" | |
end | |
end | |
JukeBox.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment