-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextairbnb.rb
99 lines (68 loc) · 1.85 KB
/
textairbnb.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
require 'pry'
class Home
attr_reader(:name, :city, :capacity, :price)
def initialize(name, city, capacity, price)
@name = name
@city = city
@capacity = capacity
@price = price
end
end
homes = [
Home.new("Nizar's place", "San Juan", 2, 42),
Home.new("Fernando's place", "Seville", 5, 47),
Home.new("Josh's place", "Pittsburgh", 3, 41),
Home.new("Gonzalo's place", "Málaga", 2, 45),
Home.new("Ariel's place", "San Juan", 4, 49),
Home.new("Charlie's place", "Granollers", 3, 35),
Home.new("Aidi's Place", "Valencia", 5, 46),
Home.new("paco´s place", "Campanar", 8, 70),
Home.new("Alvaro's place", "Barcelona",2, 45),
Home.new("Nirie's place", "Barcelona",1,33),
]
def print_homes(array)
array.each do |home|
puts "#{home.name} in #{home.city}\n
Price #{home.price} a night\n
Capacity #{home.capacity}"
end
end
def sort(array)
puts "Choose one: HL: High to low, LH: Low to High, C: Capacity"
option = gets.chomp
if option == "LH"
sorted_homes = array.sort {|home1 ,home2| home2.price <=> home1.price}
elsif option == "C"
sorted_homes = array.sort {|home1 ,home2| home1.capacity <=> home2.capacity}
else
sorted_homes = array.sort {|home1 ,home2| home1.price <=> home2.price}
end
print_homes(sorted_homes)
average(sorted_homes)
end
def where(array)
puts "City?"
where = gets.chomp.capitalize
located_homes = array.select do |place|
place.city == where
end
print_homes(located_homes)
average(located_homes)
end
def average(array)
average_price = array.reduce(0.0) {|sum,hm| sum + (hm.price/array.length)}
puts "Average price #{average_price}"
end
def price(array)
puts "Name the price!"
offer = gets.chomp.to_i
correct_price = array.find do |home|
home.price == offer
end
puts correct_price.name
end
binding.pry
#price(homes)
#sort(homes)
# average(homes)
# where(homes)