-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathpet.rb
140 lines (122 loc) · 4.07 KB
/
pet.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class Pet
ANIMAL_TYPE = {
1 => 'Arthropod',
2 => 'Fish',
3 => 'Amphibia',
4 => 'Reptiles',
5 => 'Birds',
6 => 'Mammals'
}
# Each animal type requires a diffrent type of food
# The food cost per 1 kg is is stored in the `FOOD_COST_PER_KG` Hash
# where the `key` is the animal id and `value` is the cost
#
# Eg- The food of reptiles(animal_type id = 4) costs 300/kg.
# (FOOD_COST_PER_KG[4] = 300)
FOOD_COST_PER_KG = {
1 => 100,
2 => 600,
3 => 1000,
4 => 300,
5 => 450,
6 => 800
}
# Each animal requires its own habitat.
#
# The `HABITATS` hash stores the habitat required by each animal
# The `key` is the habitat and the `value` is the array of
# animal_type ids that live in that habitat.
#
# "habitat" => [animal_type_id]
HABITATS = {
"Aquatic" => [2, 1, 3],
"Terrariums" => [4],
"Cages" => [5, 6]
}
attr_accessor :name, :animal_type_id, :food_consumed_per_day
def initialize(name: , animal_type_id: , food_consumed_per_day:)
@name = name
@animal_type_id = animal_type_id
@food_consumed_per_day = food_consumed_per_day
end
# Return the habitat of the pet
def habitat
HABITATS.each do |key, value|
# print "Animal id #{@animal_type_id} key is #{key} value is #{value}"
if (value.include?(@animal_type_id)) then
return key
end
end
end
# Returns the cost of food required to feed the animal
# per day
def food_cost_per_day
return FOOD_COST_PER_KG[animal_type_id] * food_consumed_per_day
end
# This function takes the number of `days` as the input
# and returns the food required(in kgs) to feed the
# pet for that many number of days
#
# Eg - if the pet requires 0.5 kg of food per day
# (i.e, food_consumed_per_day = 0.5), then
# pet.food_required(5) will return 2.5 as the answer
#
# cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4)
# cat.food_required(28) = 11.2 (0.4 * 28)
def food_required(days)
return @food_consumed_per_day * days
end
# This function takes the number of `days` as the input
# and returns the cost incurred to feed the pet for
# that many number of days
#
# cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4)
# cat.food_cost(28) = 8960
def food_cost(days)
return FOOD_COST_PER_KG[@animal_type_id] * days * @food_consumed_per_day
end
# This function takes an array of pets and the `days`
# as input and returns the cost to feed them all
# for the specified number of `days`
#
# Eg -
# cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4)
# dog = Pet.new(name: 'dog', animal_type_id: 6, food_consumed_per_day: 0.7)
# fish = Pet.new(name: 'clownfish', animal_type_id: 2, food_consumed_per_day: 0.1)
# snake = Pet.new(name: 'python', animal_type_id: 4, food_consumed_per_day: 0.3)
# Pet.cost_to_feed([cat, dog, fish, snake], 6) will return 6180.0
def self.cost_to_feed(pets, days)
total_cost = 0
pets.each do |pet|
total_cost = total_cost + pet.food_consumed_per_day*days*FOOD_COST_PER_KG[pet.animal_type_id]
end
return total_cost
end
# This function takes an array of pets as input
# and returns a hash with pets name grouped by their animal type
#
# Eg -
# cat = Pet.new(name: 'cat', animal_type_id: 6, food_consumed_per_day: 0.4)
# dog = Pet.new(name: 'dog', animal_type_id: 6, food_consumed_per_day: 0.7)
# fish = Pet.new(name: 'clownfish', animal_type_id: 2, food_consumed_per_day: 0.1)
# snake = Pet.new(name: 'python', animal_type_id: 4, food_consumed_per_day: 0.3)
# Pet.group_by_animal_type([cat, dog, fish, snake]) will return the follwing hash
# {
# 6 => ['cat', 'dog'],
# 2 => ['clownfish'],
# 4 => ['python']
# }
#
# Note - Order is not important
def self.group_by_animal_type(pets)
result = {}
pets.each do |pet|
if result[pet.animal_type_id]
result[pet.animal_type_id] << pet.name
else
result[pet.animal_type_id] = [pet.name]
end
end
result
end
end