-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.rb
112 lines (87 loc) · 1.8 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
# Please copy/paste all three classes into this file to submit your solution!
class Customer
attr_accessor :first_name, :last_name
@@all = []
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
@@all << self
end
def full_name
"#{first_name} #{last_name}"
end
def self.all
@@all
end
def self.find_by_name(name)
@@all.find do |customer|
name.downcase == customer.full_name.downcase
end
end
def self.find_all_by_first_name(name)
@@all.select do |customer|
name.downcase == customer.first_name.downcase
end
end
def self.all_names
@@all.map do |customer|
customer.full_name
end
end
def add_review(restaurant, content)
Review.new(self, restaurant, content)
end
end
# john = Customer.new("john", "smith")
# johnny = Customer.new("john", "smith")
# Jane = Customer.new("Jane", "Smith")
#
# candy = Restaurant.new("candy shop")
# gum = Restaurant.new("gum shop")
class Restaurant
attr_accessor :name
@@all = []
def initialize(name)
@name = name
@@all << self
end
def self.all
@@all
end
def self.find_by_name(name)
@@all.find do |restaurant|
restaurant.name.downcase == name.downcase
end
end
def reviews
Review.all.select do |review|
review.restaurant == self
end
end
def customers
Review.all.map do |review|
if review.restaurant == self
review.customer
end
end
end
end
class Review
attr_accessor :customer, :restaurant, :content
@@all = []
def initialize(customer, restaurant, content)
@customer = customer
@restaurant = restaurant
@content = content
@@all << self
end
def self.all
@@all
end
def customer
@customer
end
def restaurant
@restaurant
end
end