I have to change this code so that now it is capable of searching for a track in an album and then display a message that the specific track is playing. For example, if user selected second track of the second album, then the message should be ‘The selected track Track 2 from album Billboard 2019 Rock is now playing…’ I am not sure how I can use the search_for_track_name function in the play_selected_track function to achieve this.
text file:
2
Greatest Hits
Neil Diamond
1
3
Crackling Rose
sounds/01-Cracklin-rose.wav
Soolaimon
sounds/06-Soolaimon.wav
Sweet Caroline
sounds/20-Sweet_Caroline.wav
Billboard 2019 Rock
Various Artist
4
3
Track 1
sounds/album2/track1.mp3
Track 2
sounds/album2/track2.mp3
Track 3
sounds/album2/track3.mp3
# TASK 6.3
# Same as Task 6.1, can copy from it here
module Genre
POP, CLASSIC, JAZZ, ROCK = *1..4
end
class Album
# you will need to add tracks to the following and the initialize()
attr_accessor :title, :artist, :genre, :tracks
# complete the missing code:
def initialize (title, artist, genre, tracks)
# insert lines here
@title = title
@artist = artist
@genre = genre
@tracks = tracks
end
end
class Track
attr_accessor :name, :location
def initialize (name, location)
@name = name
@location = location
end
end
# Reads in and returns a single track from the given file
def read_track(music_file)
track_name=music_file.gets.chomp
track_location = music_file.gets.chomp
track = Track.new(track_name, track_location)
track.name= track_name
track.location= track_location
return track
# fill in the missing code
end
# Returns an array of tracks read from the given file
def read_tracks(music_file)
count = music_file.gets().to_i()
tracks = Array.new()
i=0
while (i<count)
track = read_track(music_file)
tracks << track
i+=1
end
return tracks
end
# Reads in and returns a single album from the given file, with all its tracks
def read_album(music_file)
album_title = music_file.gets
album_artist = music_file.gets
album_genre = music_file.gets.chomp.to_i
tracks=read_tracks(music_file)
album_tracks=tracks
album = Album.new(album_title, album_artist, album_genre, album_tracks)
album.title = album_title
album.artist = album_artist
album.genre = album_genre
album.tracks = album_tracks
return album
# read in all the Album's fields/attributes including all the tracks
# complete the missing code
end
# Reads in and returns multiple albums from the given file, with all its tracks
def read_albums(music_file)
number_of_albums = music_file.gets().to_i
albums=Array.new()
j=0
while (j<number_of_albums)
album = read_album(music_file)
albums << album
j+=1
end
return albums
end
# Reads in and returns a single album from the given file, with all its tracks
def read_album(music_file)
album_title = music_file.gets
album_artist = music_file.gets
album_genre = music_file.gets.chomp.to_i
tracks=read_tracks(music_file)
album_tracks=tracks
album = Album.new(album_title, album_artist, album_genre, album_tracks)
album.title = album_title
album.artist = album_artist
album.genre = album_genre
album.tracks = album_tracks
return album
# read in all the Album's fields/attributes including all the tracks
# complete the missing code
end
# Takes a single track and prints it to the terminal
def print_track(track)
puts(track.name)
puts(track.location)
end
# Takes an array of tracks and prints them to the terminal
def print_tracks(tracks)
index = 0
while (index<tracks.length)
print_track (tracks[index])
index+=1
end
# print all the tracks use: tracks[x] to access each track.
end
# Takes a single album and prints it to the terminal along with all its tracks
def print_album(album)
genre_names = ['Null', 'Classic', 'Pop', 'Jazz', 'Rock']
# print out all the albums fields/attributes
# Complete the missing code.
puts album.title
puts album.artist
puts('Genre is ' + album.genre.to_s())
puts(genre_names[album.genre])
tracks=album.tracks
print_tracks(tracks)
# print out the tracks
end
# Takes a file name as string and read the data from it
def read_albums_file(file_name)
music_file = File.new(file_name, "r")
albums = read_albums(music_file)
music_file.close()
return album
end
def search_for_track_name(tracks, search_string)
index = 0
found_index=-1
while (index<tracks.length)
if (tracks[index].name==search_string)
found_index=index
end
index+=1
end
return found_index
end
# Play track from album. What can I change here?
def play_selected_track(albums)
number=read_string("Please enter ID: ")
# ask user to enter ID number of an album in the albums-list
album_id =
# check if the album_id is found - then
# ask user to enter ID number of a track in the tracks-list of that album
track_id = ??
# check if the track_id is found - then
# dipslay 'The selected track [] is now playing...'
# ??
end
# Reads in an album from a file and then print the album to the terminal
def main
# not sure about this part
music_file_name = ??
albums = read_albums_file(??)
play_selected_track(albums)
end
# Program starts by calling this 'main' function
main