Question
This task build on 5.1P Track File Handling where you made the program read in a number of tracks from a file. Tracksearch.rb - require
This task build on 5.1P Track File Handling where you made the program read in a number of tracks from a file.
Tracksearch.rb -
require './input_functions'
# Task 6.1 T - use the code from 5.1 to help with this
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)
# 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()
# Put a while loop here which increments an index to read the tracks
track = read_track(music_file)
tracks
return tracks
end
# Takes an array of tracks and prints them to the terminal
def print_tracks(tracks)
# print all the tracks use: tracks[x] to access each track.
end
# Takes a single track and prints it to the terminal
def print_track(track)
puts('Track title is: ' + track.title)
puts('Track file location is: ' + track.file_location)
end
# search for track by name.
# Returns the index of the track or -1 if not found
def search_for_track_name(tracks, search_string)
# Put a while loop here that searches through the tracks
# Use the read_string() function from input_functions.
# NB: you might need to use .chomp to compare the strings correctly
# Put your code here.
return found_index
end
# Reads in an Album from a file and then prints all the album
# to the terminal
def main()
music_file = File.new("album.txt", "r")
tracks = read_tracks(music_file)
music_file.close()
search_string = read_string("Enter the track name you wish to find: ")
index = search_for_track_name(tracks, search_string)
if index > -1
puts "Found " + tracks[index].name + " at " + index.to_s()
else
puts "Entry not Found"
end
end
main()
Please answer this question with the appropriate steps, otherwise you would be subject to reporting
This task build on 5.1P Track File Handling where you made the program read in a number of tracks from a file. In this task you need to do or include the following: Once the track information is loaded, prompt the user to enter a search string (use the read_string() function from the library. The search function should have the following prototype: search_for_track_name(tracks, search_name) - where tracks is an array of tracks and is the string being searched for. The function should return 1 if the track name is not found, otherwise it should return the array index of the track with that name. Display a message indicating if the track has been found or not (this is provided in the sample code in Resources). Make sure you test for a track that does exist and one that does not. An example of the expected output of your program is provided below: Crackling Rose sounds/01-Cracklin-rose.wav Soolaimon sounds/06-Soolaimon. wav Sweet Caroline sounds/20-Sweet_Caroline.wav Enter the track name you wish to find: Soolaimon Found Soolaimon at 1 MacBook-Pro-6:6.2 T Arrav search mmitchell\$
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