scottcreynolds
10/2/2013 - 6:04 PM

jukebox.rb

def full_library
  {
    :"U2" => {
      :albums => {
        :"The Joshua Tree" => {
          :songs => ["With or Without You", "Still Haven't Found What I'm Looking For", "Bullet the Blue Sky"]
        },
        :"Zooropa" => {
          :songs => ["Numb"]
        }
       }
    },
    :"Talking Heads" => {
      :albums => {
        :"Fear of Music" => {
          :songs => ["Life During Wartime", "Heaven"]
        },
        :"Speaking in Tongues" => {
          :songs => ["This Must Be the Place (Naive Melody)", "Burning Down the House"]
        }
      }
    },
    :"Huey Lewis and the News" => {
      :albums => {
        :"Sports" => {
          :songs => ["I Want a New Drug", "If This is It", "Heart of Rock and Roll"]
        }
      }
    }
  }
end
require_relative './song_library.rb'
require_relative './jukebox.rb'

def run
  command = ""
  puts "Welcome to Ruby Console Jukebox!"
  while command.downcase != "exit" do
    puts "Enter a command to continue. Type 'help' for a list of commands."
    command = get_command
    run_command(command) unless command.downcase == "exit"
  end
end

def get_command
  gets.strip
end

def run_command(command)
  case command
  when "help"
    show_help
  else
    jukebox(command)
  end
end

def show_help
  help = "Never worked a jukebox, eh? Pretty standard. Available commands are:\n"
  help += "'help' - shows this menu\n"
  help += "'list' - lists the whole song library\n"
  help += "or you can enter an artist's name to show that artist's songs\n"
  help += "or you can enter a song title to play that song!\n\n"
  puts help
end

run
require_relative './song_library.rb'

def jukebox(command)
  if command.downcase == "list"
    list_library
  else
    parse_command(command)
  end
end

def list_library
  lib = full_library
  lib.each do |artist, album_hash|
    puts list_artist(artist, album_hash)
  end
end

def parse_command(command)
  parse_artist(command, full_library) || play_song(command, full_library) || not_found(command)
end

def parse_artist(command, lib)
  cmd = command.to_sym
  parsed = false
  if lib.has_key?(cmd)
    puts list_artist(command, lib[cmd])
    parsed = true
  else
    lib.each do |artist, hash|
      if command.downcase == artist.to_s.gsub("_"," ").downcase
        puts list_artist(artist, lib[artist])
        parsed = true
        break
      end
    end
  end
  parsed
end

def play_song(command, lib)
  lib.each do |artist, hash|
    hash.each do |album_name, albums_hash|
      albums_hash.each do |album, songs_hash|
        songs_hash.each do |songs|
          songs[1].each do |song|
            if song.downcase == command.downcase
              puts "Now Playing: #{album} - #{song}\n\n"
              return true
            end
          end
        end
      end
    end
  end
  false
end

def list_artist(artist, album_hash)
   artist_list = "\n---------------\n"
   artist_list += "#{artist}:\n"
   artist_list += "---------------"
   album_hash[:albums].each do |album_name, songs_hash|
     artist_list += "\n#{album_name}:\n\t"
     artist_list += songs_hash[:songs].join("\n\t")
   end
   artist_list
end

def not_found(command)
  puts "I did not understand '#{command}'!\n\n"
  true
end