diff options
Diffstat (limited to 'graphs/js/foodTruck/foodTruck.js')
| -rw-r--r-- | graphs/js/foodTruck/foodTruck.js | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/graphs/js/foodTruck/foodTruck.js b/graphs/js/foodTruck/foodTruck.js new file mode 100644 index 0000000..afe14d4 --- /dev/null +++ b/graphs/js/foodTruck/foodTruck.js @@ -0,0 +1,124 @@ +if (typeof window === "undefined") { + if ( + !globalThis.stocks && + !globalThis.recipes && + !globalThis.globalThis.sleep + ) { + const { stocks, recipes } = require("./data"); + const { sleep } = require("./sleep"); + + globalThis.stocks = stocks; + globalThis.recipes = recipes; + globalThis.globalThis.sleep = sleep; + } +} + +if (typeof window === "undefined") { + module.exports = { + order, + }; +} + +/* + * + * Write your code below these lines + * + */ +async function end_of_order(recipeName, wait) { + if (wait) { + await globalThis.sleep(1); + } + + console.log("All ingredients have been prepared"); + await globalThis.sleep(2); + console.log(`Cooking ${recipeName}`); + await globalThis.sleep(5); + console.log(`Delivering ${recipeName}`); + return null; +} +function check_and_update_ingredient(recipeName, ingredient, quantity) { + if (globalThis.stocks[ingredient] < quantity) { + console.log( + `Not enough ingredients for ${recipeName} because there is no more ${ingredient}`, + ); + return false; + } + + globalThis.stocks[ingredient] -= quantity; + return true; +} + +function check_and_update_ingredients(recipeName, ingredients) { + for (const ingredient in ingredients) { + const quantity = ingredients[ingredient]; + + if (!check_and_update_ingredient(recipeName, ingredient, quantity)) { + return false; + } + } + + return true; +} + +async function order(recipeName) { + console.log(`Ordering ${recipeName}`); + + if ( + !(recipeName in globalThis.recipes.pizza) && + !(recipeName in globalThis.recipes.burger) + ) { + console.log(`Recipe ${recipeName} does not exist`); + return; + } + + await globalThis.sleep(2); + console.log(`Production has started for ${recipeName}`); + await globalThis.sleep(1); + if (recipeName in globalThis.recipes.burger) { + const burger = globalThis.recipes.burger[recipeName]; + + for (const ingredient in burger) { + console.log(`Preparing ${ingredient}`); + if ( + !check_and_update_ingredient( + recipeName, + ingredient, + burger[ingredient], + ) + ) { + return; + } + + await globalThis.sleep(1); + } + + return await end_of_order(recipeName, false); + } else if (recipeName in globalThis.recipes.pizza) { + const pizza = globalThis.recipes.pizza[recipeName]; + + console.log(`Preparing ${pizza.sauce}`); + if (!check_and_update_ingredient(recipeName, pizza.sauce, 1)) { + return; + } + + await globalThis.sleep(1); + console.log(`Preparing ${Object.keys(pizza.toppings).join(", ")}`); + if (!check_and_update_ingredients(recipeName, pizza.toppings)) { + return; + } + + await globalThis.sleep(1); + console.log(`Preparing ${Object.keys(pizza.cheese).join(", ")}`); + if (!check_and_update_ingredients(recipeName, pizza.cheese)) { + return; + } + + return await end_of_order(recipeName, true); + } +} + +if (typeof window === "undefined") { + module.exports = { + order, + }; +} |
