Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let manager2 = (function manager(){
- let ingredients = {
- protein : 0,
- carbohydrate: 0,
- fat: 0,
- flavour: 0
- };
- let recipes ={
- apple: {carbohydrate: 1, flavour: 2},
- coke: {carbohydrate: 10, flavour: 20},
- burger: {carbohydrate: 5, fat: 7, flavour: 3},
- omelet: {protein: 5, fat: 1, flavour: 1},
- cheverme: {protein: 10,carbohydrate: 10, fat: 10, flavour: 10},
- }
- function restock(ingredient, quantity){
- ingredients[ingredient] += quantity;
- console.log('Success');
- }
- function prepare(meal, quantity){
- let productsAreAvailable = checkProductAvailability();
- if(productsAreAvailable[0]){
- for (const ingredient in recipes[meal]) {
- ingredients[ingredient] -= recipes[meal][ingredient] * quantity;
- }
- console.log('Success');
- } else{
- console.log(`Error: not enough ${productsAreAvailable[1]} in stock`);
- }
- function checkProductAvailability(){
- for (const ingredient in recipes[meal]) {
- if(ingredients[ingredient] < recipes[meal][ingredient] * quantity){
- return [false, ingredient];
- }
- }
- return [true];
- }
- }
- function report(){
- console.log(`protein=${ingredients['protein']} carbohydrate=${ingredients['carbohydrate']} fat=${ingredients['fat']} flavour=${ingredients['flavour']}`);
- }
- return function cmdProcessor(instruction) {
- let instructionArgs = instruction.split(/ /);
- switch(instructionArgs[0]){
- case 'restock': restock(instructionArgs[1], Number(instructionArgs[2])); break;
- case 'prepare': prepare(instructionArgs[1], Number(instructionArgs[2])); break;
- case 'report': report(); break;
- }
- }
- })();
- manager2('restock flavour 50');
- manager2('prepare coke 4');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement