-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoodDB.rb
184 lines (169 loc) · 4.13 KB
/
FoodDB.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Food Database
# Abigail My Tran
# December 3rd, 2016
require './BasicFood'
require './Recipe'
class FoodDB < Hash
private
def initialize()
@data = Hash.new()
f = File.open("FoodDB.txt")
f.each do |line|
line.chomp!
if line != ' '
array = line.split(',')
type = array[1]
if type == 'b'
name = array[0]
calories = array[2]
contain = BasicFood.new(name, calories)
@data.store(name, contain)
elsif type == 'r'
name = array[0]
ingredients = Array.new
for i in 2..(array.length - 1)
ingredients.push(array[i])
end
contain = Recipe.new(name, ingredients)
@data.store(name, contain)
end
end
end
end
public
attr_reader :data
###
#PRINT ALL
###
def to_string()
print_string = ""
if data.size == 0
print_string = 'The food database is empty'
else
data.values.each do | contain |
#Contain is a recipe
if contain.instance_of? Recipe
recipe_string = ""
total_cal = 0
array = contain.ingredients
array.each do | name |
#Find the corresponding basic_food
food = data[name]
cal = food.calories
recipe_string.insert(0, " #{name} #{cal}\n" )
total_cal += cal.to_i
end
name = contain.name
recipe_string.insert(0, "#{name} #{total_cal}\n")
print_string.insert(-1, recipe_string)
elsif contain.instance_of? BasicFood
num = contain.calories
name = contain.name
print_string.insert(-1, "#{name} #{num}\n")
end
end
end
return print_string
end
###
#PRINT FOOD
###
def print_food( food_name)
if !data.has_key? food_name
puts ("There is no such food in the database")
elsif data[food_name].instance_of? Recipe
print_string = ""
total_cal = 0
array = data[food_name].ingredients
array.each do | name |
#Find the corresponding basic_food
food = data[name]
cal = food.calories
print_string.insert(0, " #{name} #{cal}\n" )
total_cal += cal.to_i
end
print_string.insert(0, "#{food_name} #{total_cal}\n")
puts(print_string)
elsif data[food_name].instance_of? BasicFood
calo = data[food_name].calories
puts ("#{food_name} #{calo}")
end
end
###
#FIND PREFIX
###
def find( prefix )
found = "no"
food_list = data.keys
for i in 0..(food_list.size-1)
finding = food_list[i].downcase
if finding.start_with? prefix
found = "yes"
return (food_list[i])
end
end
if (found == "no")
puts ("Can't find such food in the database")
end
end
###
# ADD BASIC FOOD
###
def add_basic_food(name, contain)
if !data.has_key?(name)
data.store(name, contain)
else
puts "The given food is already in the database"
end
end
###
# ADD RECIPE
###
def add_recipe(name, contain)
if data.has_key?(name)
puts "The given recipe is already in the database"
else
found = "yes"
for i in 0..(contain.ingredients.size-1)
if (!data.has_key?(contain.ingredients[i]))
puts ("The ingredients is not in the database")
found = "no"
break
end
end
if found == "yes"
data.store(name, contain)
end
end
end
###
# TO DATA
###
def to_data()
data_text = ""
if (data.size == 0)
return data_text
else
data.values.each do | contain |
#Contain is a Recipe
if (contain.instance_of? Recipe)
data_text.insert(-1, "#{contain.name},r")
ingredient_list = contain.ingredients
ingredient_list.each do |name|
data_text.insert(-1, ",#{name}")
end
data_text.insert(-1, "\n")
elsif (contain.instance_of? BasicFood)
data_text.insert(-1, "#{contain.name},b,#{contain.calories}\n")
end
end
return data_text
end
end
###
# SAVE
###
def save(text)
File.open("FoodDB.txt", 'w') { |file| file.write(text) }
end
end