Skip to content

Instantly share code, notes, and snippets.

@samtalks
Last active December 24, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samtalks/6734324 to your computer and use it in GitHub Desktop.
Save samtalks/6734324 to your computer and use it in GitHub Desktop.
# Download Gist: https://gist.github.com/scottcreynolds/e6534b284373efe0ba6e/download
# Build a Jukebox
# create a file jukebox.rb
# When that program is run, it should introduce itself
# to the user and accept input from the user using the gets command.
# The jukebox should respond to 4 commands, help, play, list and exit.
# The help command should output instructions for the user
# on how to use the jukebox.
# The list command should output a list of songs that the
# user can play.
# the play command should accept a song, either by number/index
# or name. Once the user has indicated which song they want to
# play, the jukebox should output 'Playing The Phoenix - 1901'
# or whatever song name is important.
# if the user types in exit, the jukebox should say goodbye
# and the program should shut down.
# Think about the following things
# How to keep the program running until the exit command is
# executed (Hint: Loop maybe? Loop upon a condition)
# How to normalize the user's input so LIST and list are the
# same. (Hint, maybe downcase and strip it)
# How to give the songs an "index" so that when you list them
# out, you can refer to them by position so the user can just
# type play 1 and then you find the first song. (Hint, check
# out a method called each_with_index)
$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 help
puts "Play - plays a selected song\nList - lists all the songs\nExit - terminates program\n"
end
def list
$songs.each_with_index do |song, index|
puts "#{index+1}. #{song}"
end
end
def play(request)
# trying to add this in later: if request.is_a?(Integer) && request <= 7
if request.length == 1
puts "Playing " + $songs[request.to_i-1]
elsif
$songs.each do |artist_song|
puts "Playing " + artist_song if artist_song.downcase.include?(request.downcase)
end
else
puts "Sorry I couldn't understand your input."
end
end
loop do
puts "Please type one of the following commands: help, play, list and exit."
command = gets.chomp.downcase
case command
when "help" then help
when "play"
list
puts "Enter Song Title or Number (1-7) you would like to play:"
request = gets.chomp
play(request)
when "list" then list
when "exit"
puts "Goodbye"
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment