100%(2)2 out of 2 people found this document helpful
This preview shows page 1 - 3 out of 3 pages.
COS10009 Task 5.2T Track file handlingclass Trackattr_accessor :track_title, :track_locationdef initialize(track_title, track_location)@track_title = track_title@track_location = track_locationendend# Returns an array of tracks read from the given file# Put a while loop here which increments an index to read the tracksdef read_tracks music_filecount = music_file.gets().to_itracks = Array.newtrack = File.open("input.txt")i = 0while (i < count)track = read_track(music_file)tracks << tracki += 1end tracksend# reads in a single track from the given file.# complete this function# you need to create a Track here - see 5.1 T, Music Record for this too.def read_track a_filetrack_title = a_file.gets()
track_location = a_file.gets()track = Track.new(track_title, track_location)trackend# Takes an array of tracks and prints them to the terminal# Use a while loop with a control variable index# to print each track. Use tracks.length to determine how# many times to loop.# Print each track use: tracks[index] to get each track recorddef print_tracks tracksindex = 0while index < tracks.length print_track tracks[index]index += 1endend# Takes a single track and prints it to the terminaldef print_track trackputs('Track title is: ' + track.track_title)puts('Track file location is: ' + track.track_location)end# Open the file and read in the tracks then print them# if nil this test will be false# open for reading# Print all the tracksdef maina_file = File.new("input.txt", "r") if a_file tracks = read_tracks(a_file)
a_file.closeelseputs "Unable to open file to read!"endprint_tracks(tracks)endmain