Music player

mail@pastecode.io avatar
unknown
ruby
a month ago
1.7 kB
1
Indexable
Never
require './input_functions'
require './class'

def read_album()
  file_name = read_string("Enter album file: ")
  begin
    file = File.new(file_name, "r")
  rescue => exception
    puts("No file found. Please try again.")
    read_album()
  else
    albums = read_albums(file_name)
    file.close()
    puts("Musics loaded. Press ENTER to continue.")
    gets()
    return albums
  end
end

def display_albums(albums)
  puts("Albums: ")
  puts("1 - Display all Albums")
  puts("2 - Display Albums by Genre")
  choice = read_integer_in_range('Please enter your choice:', 1, 2)
  begin
    case choice
    when 1
      display_all(albums)
    when 2
      display_by_genre(albums)
    end
  end
end

def play_albums(albums)
  puts("Number of albums: #{albums.length}")
  i = 0
  while i < albums.length
    puts("#{albums[i].id} - #{albums[i].title} by #{albums[i].artist}")
    i += 1
  end

  success = 0
  if albums.length > 0
    index = read_integer_in_range('Please enter the index of the album to play:', 1, albums.length)
    success = albums[index - 1].play_album()
  else
    puts("No albums found. Please try again.")
  end
end

def main()
  finished = false
  while !finished
    puts('Main Menu:')
    puts('1 - Read in Albums')
    puts('2 - Display Albums')
    puts('3 - Select an Album to play')
    puts('4 - Exit the application')
    choice = read_integer_in_range('Please enter your choice:', 1, 4)
    case choice
    when 1
      albums = read_album()
    when 2
      display_albums(albums)
    when 3
      play_albums(albums)
    else
      finished = true
    end
  end
end

main()