Skip to content

Instantly share code, notes, and snippets.

@rosiehoyem
Created October 13, 2013 17:28
Show Gist options
  • Save rosiehoyem/6964906 to your computer and use it in GitHub Desktop.
Save rosiehoyem/6964906 to your computer and use it in GitHub Desktop.
Object Oriented Jukebox, Day 12
songs = ["Miley Cyrus - We Can't Stop",
"Katy Perry - Roar",
"Robin Thicke - Blurred Lines",
"Daft Punk - Get Lucky",
"Miley Cyrus - Wrecking Ball",
"Lady Gaga - Applause",
"Miley Cyrus - Party in the USA",
"Avicii - Wake Me Up"
]
class Jukebox
attr_accessor :songs
def initialize(songs)
@songs = songs.sort
end
def call
puts "Welcome to Pop Jukebox!"
puts "Enter a command to continue. Type 'help' for a list of commands."
exit = false
while exit == false do
command = gets.strip.downcase
case command
when 'help'
help
when 'list'
list
when 'play'
play
else
puts "Say whaaaaaa?"
end
end
end
def help
puts "Enter one of the following commands: help, play, list, exit"
end
def list
puts self.songs
puts "Type play to proceed"
end
def play
puts "Would you like to play by song title or artist?"
song_title_or_artist = gets.strip.downcase
case song_title_or_artist
when 'song title'
puts list_song_titles
puts "Please select a song"
song_choice = gets.strip.downcase
play_song_choice(song_choice)
when 'artist'
puts list_artists
puts "Please select an artist"
artist_choice = gets.strip.downcase
list_artist_songs(artist_choice)
song_choice = gets.strip.downcase
play_song_choice(song_choice)
else
puts "Enter one of the following commands: help, play, list, exit"
end
when 'exit'
puts "Goodbye! Nice to know ya."
exit = true
end
def list_artists
artists = self.songs.collect do |song|
song.split(" - ")[0]
end.uniq
end
def list_song_titles
song_titles = self.songs.collect do |song|
song.split(" - ")[1]
end.sort
end
def play_song_choice(song_choice)
self.songs.select do |song|
if song.split(" - ")[1].downcase == song_choice
puts "Now playing #{song}!"
end
end
end
def list_artist_songs(artist_choice)
self.songs.select do |song|
if song.split(" - ")[0].downcase == artist_choice
# then just show that artist's song
puts song.split(" - ")[1]
end
end
puts "Select song"
end
end
j = Jukebox.new(songs)
j.call
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment