forked from kth-csc-iprog/dinnerplanner-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dinnerModel.js
377 lines (365 loc) · 8.76 KB
/
dinnerModel.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//DinnerModel class
class DinnerModel {
constructor() {
this.dishes = dishesConst;
this.dinner = {guests:0, menu:[]};
//TODO Lab 0
// implement the data structure that will hold number of guests
// and selected dishes for the dinner menu
}
setNumberOfGuests(num) {
//TODO Lab 0
if(num >= 0){
this.dinner.guests = num;
}
}
getNumberOfGuests() {
//TODO Lab 0
return this.dinner.guests;
}
//Returns the dish that is on the menu for selected type
getSelectedDish(type) {
//TODO Lab 0
for(let dish of this.dinner.menu){
if (dish.type === type){
return dish;
}
}
return undefined;
}
//Returns all the dishes on the menu.
getFullMenu() {
return this.dinner.menu;
}
//Returns all ingredients for all the dishes on the menu.
getAllIngredients() {
//TODO Lab 0
let y = [];
for(let x of this.dinner.menu){
for(let z of x.ingredients){
y.push(z);
}
}
return y;
}
//Returns the total price of the menu (all the ingredients multiplied by number of guests).
getTotalMenuPrice() {
let z = 0;
x = this.getAllIngredients;
for( y of x.price){
z = y*this.dinner.guests;
}
return z;
}
//Adds the passed dish to the menu. If the dish of that type already exists on the menu
//it is removed from the menu and the new one added.
addDishToMenu(id) {
//TODO Lab 0
if(!(this.getSelectedDish(this.getDish(id).type) === undefined)){
this.removeDishFromMenu(this.getSelectedDish(this.getDish(id)));
}
this.dinner.menu.push(this.getDish(id));
}
//Removes dish from menu
removeDishFromMenu(id) {
//TODO Lab 0
this.dinner.menu.splice(this.dinner.menu.indexOf(id));
}
//Returns all dishes of specific type (i.e. "starter", "main dish" or "dessert").
//query argument, text, if passed only returns dishes that contain the query in name or one of the ingredients.
//if you don't pass any query, all the dishes will be returned
getAllDishes(type, query) {
if(query === undefined && type === undefined){
return this.dishes;
}
return this.dishes.filter(function (dish) {
let found = true;
if (query) {
found = false;
dish.ingredients.forEach(function (ingredient) {
if (ingredient.name.indexOf(query) !== -1) {
found = true;
}
});
if (dish.name.indexOf(query) !== -1) {
found = true;
}
}
return (dish.type === type || type == "") && found;
});
}
//Returns a dish of specific ID
getDish(id) {
for (let dsh of this.dishes) {
if (dsh.id === id) {
return dsh;
}
}
return undefined;
}
}
// the dishes constant contains an array of all the
// dishes in the database. Each dish has id, name, type,
// image (name of the image file), description and
// array of ingredients. Each ingredient has name,
// quantity (a number), price (a number) and unit (string
// defining the unit i.e.//// "g", "slices", "ml". Unit
// can sometimes be empty like in the example of eggs where
// you just say "5 eggs" and not "5 pieces of eggs" or anything else.
const dishesConst = [{
'id': 1,
'name': 'French toast',
'type': 'starter',
'image': 'toast.jpg',
'description': "In a large mixing bowl, beat the eggs. Add the milk, brown sugar and nutmeg; stir well to combine. Soak bread slices in the egg mixture until saturated. Heat a lightly oiled griddle or frying pan over medium high heat. Brown slices on both sides, sprinkle with cinnamon and serve hot.",
'ingredients': [{
'name': 'eggs',
'quantity': 0.5,
'unit': '',
'price': 10
}, {
'name': 'milk',
'quantity': 30,
'unit': 'ml',
'price': 6
}, {
'name': 'brown sugar',
'quantity': 7,
'unit': 'g',
'price': 1
}, {
'name': 'ground nutmeg',
'quantity': 0.5,
'unit': 'g',
'price': 12
}, {
'name': 'white bread',
'quantity': 2,
'unit': 'slices',
'price': 2
}]
}, {
'id': 2,
'name': 'Sourdough Starter',
'type': 'starter',
'image': 'sourdough.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'active dry yeast',
'quantity': 0.5,
'unit': 'g',
'price': 4
}, {
'name': 'warm water',
'quantity': 30,
'unit': 'ml',
'price': 0
}, {
'name': 'all-purpose flour',
'quantity': 15,
'unit': 'g',
'price': 2
}]
}, {
'id': 3,
'name': 'Baked Brie with Peaches',
'type': 'starter',
'image': 'bakedbrie.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'round Brie cheese',
'quantity': 10,
'unit': 'g',
'price': 8
}, {
'name': 'raspberry preserves',
'quantity': 15,
'unit': 'g',
'price': 10
}, {
'name': 'peaches',
'quantity': 1,
'unit': '',
'price': 4
}]
}, {
'id': 100,
'name': 'Meat balls',
'type': 'main dish',
'image': 'meatballs.jpg',
'description': "Preheat an oven to 400 degrees F (200 degrees C). Place the beef into a mixing bowl, and season with salt, onion, garlic salt, Italian seasoning, oregano, red pepper flakes, hot pepper sauce, and Worcestershire sauce; mix well. Add the milk, Parmesan cheese, and bread crumbs. Mix until evenly blended, then form into 1 1/2-inch meatballs, and place onto a baking sheet. Bake in the preheated oven until no longer pink in the center, 20 to 25 minutes.",
'ingredients': [{
'name': 'extra lean ground beef',
'quantity': 115,
'unit': 'g',
'price': 20
}, {
'name': 'sea salt',
'quantity': 0.7,
'unit': 'g',
'price': 3
}, {
'name': 'small onion, diced',
'quantity': 0.25,
'unit': '',
'price': 2
}, {
'name': 'garlic salt',
'quantity': 0.7,
'unit': 'g',
'price': 2
}, {
'name': 'Italian seasoning',
'quantity': 0.6,
'unit': 'g',
'price': 3
}, {
'name': 'dried oregano',
'quantity': 0.3,
'unit': 'g',
'price': 3
}, {
'name': 'crushed red pepper flakes',
'quantity': 0.6,
'unit': 'g',
'price': 3
}, {
'name': 'Worcestershire sauce',
'quantity': 6,
'unit': 'ml',
'price': 7
}, {
'name': 'milk',
'quantity': 20,
'unit': 'ml',
'price': 4
}, {
'name': 'grated Parmesan cheese',
'quantity': 5,
'unit': 'g',
'price': 8
}, {
'name': 'seasoned bread crumbs',
'quantity': 15,
'unit': 'g',
'price': 4
}]
}, {
'id': 101,
'name': 'MD 2',
'type': 'main dish',
'image': 'bakedbrie.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'ingredient 1',
'quantity': 1,
'unit': 'pieces',
'price': 8
}, {
'name': 'ingredient 2',
'quantity': 15,
'unit': 'g',
'price': 7
}, {
'name': 'ingredient 3',
'quantity': 10,
'unit': 'ml',
'price': 4
}]
}, {
'id': 102,
'name': 'MD 3',
'type': 'main dish',
'image': 'meatballs.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'ingredient 1',
'quantity': 2,
'unit': 'pieces',
'price': 8
}, {
'name': 'ingredient 2',
'quantity': 10,
'unit': 'g',
'price': 7
}, {
'name': 'ingredient 3',
'quantity': 5,
'unit': 'ml',
'price': 4
}]
}, {
'id': 103,
'name': 'MD 4',
'type': 'main dish',
'image': 'meatballs.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'ingredient 1',
'quantity': 1,
'unit': 'pieces',
'price': 4
}, {
'name': 'ingredient 2',
'quantity': 12,
'unit': 'g',
'price': 7
}, {
'name': 'ingredient 3',
'quantity': 6,
'unit': 'ml',
'price': 4
}]
}, {
'id': 200,
'name': 'Chocolat Ice cream',
'type': 'dessert',
'image': 'icecream.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'ice cream',
'quantity': 100,
'unit': 'ml',
'price': 6
}]
}, {
'id': 201,
'name': 'Vanilla Ice cream',
'type': 'dessert',
'image': 'icecream.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'ice cream',
'quantity': 100,
'unit': 'ml',
'price': 6
}]
}, {
'id': 202,
'name': 'Strawberry',
'type': 'dessert',
'image': 'icecream.jpg',
'description': "Here is how you make it... Lore ipsum...",
'ingredients': [{
'name': 'ice cream',
'quantity': 100,
'unit': 'ml',
'price': 6
}]
}
];
// Deepfreeze
// https://github.com/substack/deep-freeze/blob/master/index.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
function deepFreeze(o) {
Object.freeze(o);
Object.getOwnPropertyNames(o).forEach(function (prop) {
if (o.hasOwnProperty(prop)
&& o[prop] !== null
&& (typeof o[prop] === "object" || typeof o[prop] === "function")
&& !Object.isFrozen(o[prop])) {
deepFreeze(o[prop]);
}
});
}
deepFreeze(dishesConst);