Created
October 21, 2019 01:09
-
-
Save Prigin/0c7ba28b8f98954b45534137b2b0e605 to your computer and use it in GitHub Desktop.
task 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Movie | |
attr_accessor :link, :title, :year, :country, :date, | |
:genre, :duration,:rating, :director, :stars | |
def initialize (unit) | |
@link = unit[0] | |
@title = unit[1] | |
@year = unit[2] | |
@country = unit[3] | |
@date = unit[4] | |
@genre = unit[5] | |
@duration = unit[6] | |
@rating = unit[7] | |
@director = unit[8] | |
@stars = unit[9] | |
end | |
def has_genre?(unit) | |
@genre.include?(unit) | |
end | |
end | |
class MovieCollection | |
attr_accessor :all | |
def read(unit) | |
@all = File.readlines(unit).map{|line| Movie.new(line.chomp.split("|"))} | |
end | |
def sort_by(unit=nil) | |
return @all.sort_by{|u| u.link} if unit == :link | |
return @all.sort_by{|u| u.title} if unit == :title | |
return @all.sort_by{|u| u.year} if unit == :year | |
return @all.sort_by{|u| u.country} if unit == :country | |
return @all.sort_by{|u| u.date} if unit == :date | |
return @all.sort_by{|u| u.genre} if unit == :genre | |
return @all.sort_by{|u| u.duration} if unit == :duration | |
return @all.sort_by{|u| u.rating} if unit == :rating | |
return @all.sort_by{|u| u.director} if unit == :director | |
return @all.sort_by{|u| u.stars} if unit == :stars | |
"not an option" | |
end | |
def filter(unit={}) | |
return @all.select{|u| u.year == unit[:year]} if unit.has_key?(:year) | |
return @all.select{|u| u.country == unit[:country]} if unit.has_key?(:country) | |
return @all.select{|u| u.has_genre?(unit[:genre])} if unit.has_key?(:genre) | |
end | |
def stats(unit=nil) | |
return @all.map{|u| [u.year, 0]}.uniq.each{|a| a[1] = @all.count{|obj| obj.year == a[0]}}.sort if unit == :year | |
return @all.map{|unit| unit.country}.flatten.uniq.map{|unit| | |
unit = [unit, 0]}.each{|unit| unit[1] = @all.count{|u| u.country == unit[0]} }.sort if unit == :country | |
return @all.map{|unit| unit.genre.split(",")}.flatten.uniq.map{|unit| | |
unit = [unit, 0]}.each{|unit| unit[1] = @all.count{|u| u.genre.include?(unit[0])} }.sort if unit == :genre | |
return @all.map{|unit| unit.director.split(",")}.flatten.uniq.map{|unit| | |
unit = [unit, 0]}.each{|unit| unit[1] = @all.count{|u| u.director.include?(unit[0])} }.sort if unit == :director | |
"not an option" | |
end | |
end | |
mas = MovieCollection.new | |
mas.read("movies.txt") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment