-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpizza.js
77 lines (66 loc) · 1.72 KB
/
pizza.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
export const availableIngredients = [
"flour",
"water",
"salt",
"tomatoes",
"olive oil",
"garlic",
"mozzarella cheese",
"pepperoni",
"sausage",
];
export class Pizza {
constructor(plan) {
this.plan = plan;
}
prepare() {
this.makeDough();
this.addSauce();
this.addCheese();
this.addToppings();
this.bake();
}
makeDough() {
const doughIngredients = this.plan.dough.ingredients.join(", ");
console.log(`Mixing and kneading dough with ${doughIngredients}`);
}
addSauce() {
const sauceIngredients = this.plan.sauce.ingredients.join(", ");
console.log(`Adding sauce made with ${sauceIngredients}`);
}
addCheese() {
const cheeseIngredients = this.plan.cheese.ingredients.join(", ");
console.log(`Adding cheese made with ${cheeseIngredients}`);
}
addToppings() {
const toppingIngredients = this.plan.toppings.ingredients.join(", ");
console.log(`Adding toppings: ${toppingIngredients}`);
}
bake() {
console.log(
`Baking pizza at ${this.plan.baking.temperature} degrees for ${this.plan.baking.duration} minutes`
);
}
}
export const pizzaFactory = {
createPizza(plan) {
const unavailableIngredients = Object.values(plan)
.map(({ ingredients }) =>
ingredients
? ingredients.filter(
(ingredient) => !availableIngredients.includes(ingredient)
)
: []
)
.flat();
if (unavailableIngredients.length > 0) {
throw new Error(
`The following ingredients are not available: ${unavailableIngredients.join(
", "
)}`
);
}
console.log("All ingredients for Detroit-style pizza are available!");
return new Pizza(plan);
},
};