Answered step by step
Verified Expert Solution
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:
Read in Albums
Display Albums
Select an Album to play
Exit the application
Menu option 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 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 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 should prompt the user to enter the primary key or album number for an album as listed using Menu option 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 inputfunctions'
require 'sleepypenguin'
# Define a data structure to store album information
class Album
attraccessor :artist, :title, :label, :genre, :tracks
def initializeartist title, label, genre
@artist artist
@title title
@label label
@genre genre
@tracks
end
end
# Function to read an album's information with tracks
def readalbumwithtracksfile
artist file.gets.chomp
title file.gets.chomp
label file.gets.chomp
genre file.gets.chomp
numtracks file.gets.chomp.toi
tracks
# Read track information
while numtracks
trackname file.gets.chomp
trackaudio file.gets.chomp
track
name: trackname,
audio: trackaudio
tracks track
numtracks
end
album Album.newartist title, label, genre
album.tracks tracks
album
end
# Function to display a list of albums by genre
def listalbumsbygenrealbums
genres albums.map album album.genre uniq
puts "Available genres:"
genres.eachwithindex do genre index
puts #index #genre
end
genreindexindex #albumartist #albumtitle
end
end
end
# Function to play a track
def playtrackalbum tracknumber
if tracknumber && tracknumber album.tracks.size
track album.trackstracknumber
puts "Playing track #track:name from album #albumtitle
sleep # 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 Enter album information"
puts List albums by genre"
puts Play a track"
puts Exit"
choice readintegerinrangeEnter your choice :
if choice
filename readstringEnter the filename that contains album information:
file File.openfilename
numalbums file.gets.chomp.toi
while numalbums
album readalbumwithtracksfile
albums album
numalbums
end
file.close
puts "Album information has been successfully loaded."
elsif choice
listalbumsbygenrealbums
elsif choice
albumnumber readintegerinrangeEnter the album number you want to play a track from: albums.size
album albumsalbumnumber
puts "Album: #albumartist #albumtitle
puts "Tracks:"
album.tracks.eachwithindex do track index
puts #index #track:name
end
tracknumber readintegerinrangeEnter the track number you want to play: album.tracks.size
playtrackalbum tracknumber
elsif choice
break
end
end
end
main if FILE $ readintegerinrangeEnter the number of the genre you want to list: genres.size
selectedgenre genresgenreindex
puts "Albums in the #selectedgenre genre:"
albums.eachwithindex do album index
if album.genre selectedgenre
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.
musicplayer file:
require inputfunctions'
module Genre
POP, CLASSIC, JAZZ, ROCK
end
$genrenamesPo
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started