Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Display a menu that offers the user the following options: 1 . Read in Albums 2 . Display Albums 3 . Select an Album to

Display a menu that offers the user the following options:
1. Read in Albums
2. Display Albums
3. Select an Album to play
5. Exit the application
Menu option 1 should prompt the user to enter a filename of a file that contains the following information:
The number of albums
The first artist name
The first album name
The record label
The genre of the album
The number of tracks
The name and file location (path) of each track.
The album information for the remaining albums.
Menu option 2 should allow the user to either display all albums or all albums for a particular genre. The albums should be listed with a unique album number which can be used in Option 3 to select an album to play. The album number should serve the role of a primary key for locating an album. But it is allocated internally by your program, not by the user. If the user chooses list by genre - list the available genres.
Menu option 3 should prompt the user to enter the primary key (or album number) for an album as listed using Menu option 2.If the album is found the program should list all the tracks for the album, along with track numbers. The user should then be prompted to enter a track number. If the track number exists, then the system should display the message Playing track then the track name, from album then the album name. You may or may not call an external program to play the track, but if not the system should delay for several seconds before returning to the main menu.
require './input_functions'
require 'sleepy_penguin'
# Define a data structure to store album information
class Album
attr_accessor :artist, :title, :label, :genre, :tracks
def initialize(artist, title, label, genre)
@artist = artist
@title = title
@label = label
@genre = genre
@tracks =[]
end
end
# Function to read an album's information with tracks
def read_album_with_tracks(file)
artist = file.gets.chomp
title = file.gets.chomp
label = file.gets.chomp
genre = file.gets.chomp
num_tracks = file.gets.chomp.to_i
tracks =[]
# Read track information
while num_tracks >0
track_name = file.gets.chomp
track_audio = file.gets.chomp
track ={
name: track_name,
audio: track_audio
}
tracks << track
num_tracks -=1
end
album = Album.new(artist, title, label, genre)
album.tracks = tracks
album
end
# Function to display a list of albums by genre
def list_albums_by_genre(albums)
genres = albums.map {|album| album.genre }.uniq
puts "Available genres:"
genres.each_with_index do |genre, index|
puts "#{index +1}. #{genre}"
end
genre_index{index +1}. #{album.artist}- #{album.title}"
end
end
end
# Function to play a track
def play_track(album, track_number)
if track_number >=0 && track_number < album.tracks.size
track = album.tracks[track_number]
puts "Playing track #{track[:name]} from album #{album.title}"
sleep(3) # Delay for a few seconds (simulating playing the track)
else
puts "Invalid track number. Track not found."
end
end
# Main function to manage the menu options
def main
albums =[]
while true
puts "Menu:"
puts "1. Enter album information"
puts "2. List albums by genre"
puts "3. Play a track"
puts "4. Exit"
choice = read_integer_in_range("Enter your choice (1-4): ",1,4)
if choice ==1
file_name = read_string("Enter the filename that contains album information: ")
file = File.open(file_name)
num_albums = file.gets.chomp.to_i
while num_albums >0
album = read_album_with_tracks(file)
albums << album
num_albums -=1
end
file.close
puts "Album information has been successfully loaded."
elsif choice ==2
list_albums_by_genre(albums)
elsif choice ==3
album_number = read_integer_in_range("Enter the album number you want to play a track from: ",1, albums.size)-1
album = albums[album_number]
puts "Album: #{album.artist}- #{album.title}"
puts "Tracks:"
album.tracks.each_with_index do |track, index|
puts "#{index +1}. #{track[:name]}"
end
track_number = read_integer_in_range("Enter the track number you want to play: ",1, album.tracks.size)-1
play_track(album, track_number)
elsif choice ==4
break
end
end
end
main if __FILE__== $0= read_integer_in_range("Enter the number of the genre you want to list: ",1, genres.size)-1
selected_genre = genres[genre_index]
puts "Albums in the '#{selected_genre}' genre:"
albums.each_with_index do |album, index|
if album.genre == selected_genre
puts "#
# reminder: this file remains the same as before. As It contains methods for reading integers and strings from the user, which are utilized within the main program for user input handling.
music_player file:
require './input_functions'
module Genre
POP, CLASSIC, JAZZ, ROCK =*1..4
end
$genre_names=['Po

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions