-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.rb
119 lines (94 loc) · 2.08 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Please copy/paste all three classes into this file to submit your solution!
class Movie
attr_accessor :title
@@all = []
def initialize(title)
self.title = title
@@all << self
end
def self.all
@@all
end
def self.find_by_title(title)
all.find do |movie|
movie.title.downcase == title.downcase
end
end
def ratings
Rating.all.select do |rating|
rating.movie == self
end
end
def viewers
self.ratings.map do |rating|
rating.viewer
end
end
def average_rating
# i = 0
# self.ratings.each do |rating|
# i += rating.score
# end
# i / self.ratings.length.to_f
self.ratings.reduce(0.0) do |sum, rating|
sum + rating.score
end / self.ratings.length
end
end
class Rating
attr_accessor :score, :viewer, :movie
@@all = []
def initialize(score, movie, viewer)
@score = score
@viewer = viewer
@movie = movie
@@all << self
end
def self.all
@@all
end
end
class Viewer
attr_accessor :first_name, :last_name, :full_name
@@all = []
def initialize(name)
@full_name = name
@first_name, @last_name = name.split(" ")
@@all << self
end
def self.all
@@all
end
def self.find_by_name(name)
all.find do |viewer|
viewer.full_name.downcase == name.downcase
end
end
# def create_rating(score, movie)
# Rating.new(score, movie, self)
# end
def create_rating(score, movie)
if movie.class == Fixnum
i = movie
movie = score
score = i
end
if movie.class == String
movie = Movie.new(movie)
end
if !!Movie.find_by_title(movie.title)
Movie.new("#{movie.title}")
end
Rating.new(score, movie, self)
end
end
# sammy = Viewer.new("Sammy Steiner")
# scott = Viewer.new("Scott Harrison")
# film1 = Movie.new("Star Wars")
# film2 = Movie.new("Star Trek")
# sammy.create_rating(4, film1)
# sammy.create_rating(1, film2)
# scott.create_rating(5, film1)
# scott.create_rating(film2, 3)
# sammy.create_rating(3, "all I got is a name")
# sammy.create_rating(4, Movie.new("foreign film"))