Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require_relative './input_functions'
- # Task 6.1 T - use the code from last week's tasks to complete this:
- # eg: 5.1T, 5.2T
- module Genre
- POP, CLASSIC, JAZZ, ROCK = *1..4
- end
- $genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']
- class Album
- # NB: 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
- title = music_file.gets().chomp()
- location = music_file.gets().chomp()
- track = Track.new(title, location)
- return track
- end
- # fill in the missing code
- # Returns an array of tracks read from the given file
- def read_tracks music_file
- tracks = Array.new()
- count = music_file.gets().to_i
- index = 0
- while index < count
- track = read_track(music_file)
- tracks << track
- index += 1
- end
- # Put a while loop here which increments an index to read the tracks
- tracks
- 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
- tracks[index]
- # print all the tracks use: tracks[x] to access each track.
- end
- # Reads in and returns a single album from the given file, with all its tracks
- def read_album music_file
- # read in all the Album's fields/attributes including all the tracks
- # complete the missing code
- album_artist = music_file.gets().chomp()
- album_title = music_file.gets().chomp()
- album_genre = music_file.gets.to_i()
- tracks = read_tracks(music_file)
- album = Album.new(album_title, album_artist, album_genre, tracks)
- album
- end
- # Takes a single album and prints it to the terminal along with all its tracks
- def print_album album
- # print out all the albums fields/attributes
- # Complete the missing code.
- puts('ALBUM INFORMATIONS IS ')
- puts ('Title: ' + album.title)
- puts ('Artist: ' + album.artist)
- puts 'Genre: ' + $genre_names[album.genre]
- puts 'The Tracks are: ' + album.tracks.to_s
- end
- # print out the tracks
- # 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.location)
- end
- # Reads in an album from a file and then print the album to the terminal
- def main
- music_file = File.new("album.txt", "r")
- album = read_album(music_file)
- music_file.close()
- print_album(album)
- end
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement