-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.rb
95 lines (68 loc) · 1.46 KB
/
solution.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Please copy/paste all three classes into this file to submit your solution!
class Movie
attr_accessor :title
ALL = []
def self.all
ALL
end
def self.find_by_title(title)
self.all.find {|movie| movie.title == title}
end
def initialize(title)
@title = title
ALL << self
end
def ratings
rating_array = []
Rating.all.each do |rating|
if rating.movie == self.title
rating_array << rating
end
end
rating_array
end
def viewers
self.ratings.collect {|rating| rating.viewer}
end
def average_rating
total_ratings = self.ratings.collect {|rating| rating.score}
total_ratings.inject {|sum, rating| sum + rating}.to_f / total_ratings.length
end
end
class Rating
attr_accessor :score, :viewer, :movie
ALL = []
def self.all
ALL
end
def initialize(viewer, movie, score)
@viewer = viewer
@movie = movie
@score = score
ALL << self
end
end
class Viewer
attr_accessor :first_name, :last_name
ALL = []
def self.all
ALL
end
def self.find_by_name(full_name)
self.all.find {|viewer| viewer.full_name == full_name}
end
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
ALL << self
end
def full_name
first_name = self.first_name
last_name = self.last_name
"#{first_name} #{last_name}"
end
def create_rating(movie, score)
new_rating = Rating.new(self, movie, score)
new_rating
end
end