class.rb

mail@pastecode.io avatar
unknown
ruby
9 months ago
3.7 kB
2
Indexable
Never
 require './input_functions'

module Genre
  POP, CLASSIC, JAZZ, ROCK = *1..4
end

GENRE_NAMES = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']

class Track
	attr_accessor :name, :location, :id, :album_title

	def initialize(name, location, id, album_title)
    @id = id
    @name = name
    @location = location.chomp
    @album_title = album_title
	end

  def print_track()
    puts("--> #{@id}: Track #{@name} Located in: #{@location}")
  end

  def play()
    puts("Playing track: #{@name} From album: #{@album_title}")
    puts("Press ENTER to stop listening.")
    gets()
  end
end

class Album
  attr_accessor :id, :title, :artist, :genre, :record_label, :track_count, :tracks

  def initialize(id, artist, title, genre, record_label, track_count)
    @id = id
    @artist = artist
    @title = title
    @genre = genre
    @record_label = record_label
    @track_count = track_count
    @tracks = Array.new()
  end

  def add_track(track)
    @tracks << track
  end

  def print_album()
    puts("Album ID: #{@id}")
    puts("Album: #{@title}")
    puts("Artist: #{@artist}")
    puts("Genre: #{GENRE_NAMES[@genre]}")
    puts("Record label: #{@record_label}")
    puts("Track count: #{@track_count}")
    if @track_count > 0
      puts("Tracks:")
      print_tracks(@tracks)
    end
  end

  def play_album()
    if @track_count > 0
      puts("Playing album: #{@title}")
      puts("Number of tracks: #{@track_count}")
      print_tracks(@tracks)
      track_id = read_integer_in_range('Please enter the track ID to play:', 1, @tracks.length)
      @tracks[track_id - 1].play()
      return 1
    else
      puts("No tracks found. Please try again.")
      return 0
    end
  end
end

def print_tracks(tracks)
  i = 0
  count = tracks.length
  while i < count
    tracks[i].print_track()
    i += 1
  end
end

def read_albums(file_name)
  file = File.new(file_name, "r")
  album_count = file.gets.to_i
  albums = Array.new

  i = 1
  while i <= album_count
    id = i
    artist = file.gets
    title = file.gets
    record_label = file.gets
    genre = file.gets.to_i
    track_count = file.gets.to_i
    album = Album.new(id, artist, title, genre, record_label, track_count)

    index = 1
    while index <= track_count
      id = index
      name = file.gets
      address = file.gets
      album_title = album.title
      track = Track.new(name, address, id, album_title)
      album.add_track(track)
      index += 1
    end
    albums << album
    i += 1
  end

  return albums
end

def display_all(albums)
  count = albums.count
  i = 0
  while i < count
    album = albums[i]
    album.print_album
    i += 1
  end
end

def display_by_genre(albums)
  puts "Genres available: "
  i = 1
  while i <= 4
    puts "#{i} - #{GENRE_NAMES[i]}"
    i += 1
  end
  puts "Please enter the genre you want to display:"
  genre = gets.chomp.to_i
  puts "Albums in genre #{GENRE_NAMES[genre]}"
  i = 0
  while i < albums.length
    album = albums[i]
    if album.genre == genre
      album.print_album
    end
    i += 1
  end
end

def write_albums(file_name, albums)
  file = File.new(file_name, "w")
  file.puts(albums.length)
  i = 0
  while i < albums.length
    album = albums[i]
    file.puts(album.artist)
    file.puts(album.title)
    file.puts(album.record_label)
    file.puts(album.genre)
    file.puts(album.tracks.count)
    j = 0
    while j < album.tracks.length
      track = album.tracks[j]
      file.puts(track.name)
      file.puts(track.location)
      j += 1
    end
    i += 1
  end
  file.close
end