Advertisement
Vassss

Vasss

Apr 16th, 2019
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.80 KB | None | 0 0
  1. require_relative './input_functions'
  2. # Task 6.1 T - use the code from last week's tasks to complete this:
  3. # eg: 5.1T, 5.2T
  4.  
  5. module Genre
  6.   POP, CLASSIC, JAZZ, ROCK = *1..4
  7. end
  8.  
  9. $genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']
  10.  
  11. class Album
  12. # NB: you will need to add tracks to the following and the initialize()
  13.     attr_accessor :title, :artist, :genre, :tracks
  14.  
  15. # complete the missing code:
  16.     def initialize (title, artist, genre, tracks)
  17.         # insert lines here
  18.         @title = title
  19.         @artist = artist
  20.         @genre = genre
  21.         @tracks = tracks
  22.     end
  23. end
  24.        
  25.  
  26. class Track
  27.     attr_accessor :name, :location
  28.  
  29.     def initialize (name, location)
  30.         @name = name
  31.         @location = location
  32.     end
  33. end
  34.  
  35. # Reads in and returns a single track from the given file
  36.  
  37. def read_track music_file
  38.     title = music_file.gets().chomp()
  39.     location = music_file.gets().chomp()
  40.     track = Track.new(title, location)
  41.     return track
  42. end
  43.     # fill in the missing code
  44.  
  45.  
  46. # Returns an array of tracks read from the given file
  47.  
  48. def read_tracks music_file
  49.     tracks = Array.new()
  50.     count = music_file.gets().to_i
  51.  
  52.     index = 0
  53.         while index < count
  54.             track = read_track(music_file)
  55.             tracks << track
  56.         index += 1
  57.     end
  58.  
  59.  # Put a while loop here which increments an index to read the tracks
  60.     tracks
  61. end
  62.  
  63.    
  64.  
  65.  
  66. # Takes an array of tracks and prints them to the terminal
  67.  
  68. def print_tracks tracks
  69.     index = 0
  70.         while (index < tracks.length)
  71.             print_track(tracks[index])
  72.             index += 1 
  73.         end
  74.        
  75.     tracks[index]
  76.    
  77.     # print all the tracks use: tracks[x] to access each track.
  78. end
  79.  
  80.  
  81. # Reads in and returns a single album from the given file, with all its tracks
  82.  
  83. def read_album music_file
  84. # read in all the Album's fields/attributes including all the tracks
  85.   # complete the missing code
  86.     album_artist = music_file.gets().chomp()
  87.     album_title = music_file.gets().chomp()
  88.     album_genre = music_file.gets.to_i()
  89.     tracks = read_tracks(music_file)
  90.     album = Album.new(album_title, album_artist, album_genre, tracks)
  91.     album
  92. end
  93.  
  94.  
  95. # Takes a single album and prints it to the terminal along with all its tracks
  96. def print_album album
  97.    
  98.   # print out all the albums fields/attributes
  99.   # Complete the missing code.
  100.     puts('ALBUM INFORMATIONS IS ')
  101.     puts ('Title: ' + album.title)
  102.     puts ('Artist: ' + album.artist)
  103.     puts 'Genre: ' + $genre_names[album.genre]
  104.     puts 'The Tracks are: ' + album.tracks.to_s
  105.    
  106. end
  107.     # print out the tracks
  108.  
  109.  
  110.  
  111. # Takes a single track and prints it to the terminal
  112. def print_track track
  113.   puts('Track title is: ' + track.title)
  114.     puts('Track file location is: ' + track.location)
  115. end
  116.  
  117. # Reads in an album from a file and then print the album to the terminal
  118.  
  119. def main
  120.   music_file = File.new("album.txt", "r")
  121.     album = read_album(music_file)
  122.   music_file.close()
  123.    
  124.     print_album(album)  
  125. end
  126.  
  127. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement