Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. let manager2 = (function manager(){
  2. let ingredients = {
  3. protein : 0,
  4. carbohydrate: 0,
  5. fat: 0,
  6. flavour: 0
  7. };
  8. let recipes ={
  9. apple: {carbohydrate: 1, flavour: 2},
  10. coke: {carbohydrate: 10, flavour: 20},
  11. burger: {carbohydrate: 5, fat: 7, flavour: 3},
  12. omelet: {protein: 5, fat: 1, flavour: 1},
  13. cheverme: {protein: 10,carbohydrate: 10, fat: 10, flavour: 10},
  14. }
  15.  
  16. function restock(ingredient, quantity){
  17. ingredients[ingredient] += quantity;
  18. console.log('Success');
  19. }
  20.  
  21. function prepare(meal, quantity){
  22. let productsAreAvailable = checkProductAvailability();
  23. if(productsAreAvailable[0]){
  24. for (const ingredient in recipes[meal]) {
  25. ingredients[ingredient] -= recipes[meal][ingredient] * quantity;
  26. }
  27.  
  28. console.log('Success');
  29. } else{
  30. console.log(`Error: not enough ${productsAreAvailable[1]} in stock`);
  31. }
  32.  
  33. function checkProductAvailability(){
  34. for (const ingredient in recipes[meal]) {
  35. if(ingredients[ingredient] < recipes[meal][ingredient] * quantity){
  36. return [false, ingredient];
  37. }
  38. }
  39.  
  40. return [true];
  41. }
  42. }
  43.  
  44. function report(){
  45. console.log(`protein=${ingredients['protein']} carbohydrate=${ingredients['carbohydrate']} fat=${ingredients['fat']} flavour=${ingredients['flavour']}`);
  46. }
  47.  
  48. return function cmdProcessor(instruction) {
  49. let instructionArgs = instruction.split(/ /);
  50. switch(instructionArgs[0]){
  51. case 'restock': restock(instructionArgs[1], Number(instructionArgs[2])); break;
  52. case 'prepare': prepare(instructionArgs[1], Number(instructionArgs[2])); break;
  53. case 'report': report(); break;
  54. }
  55. }
  56. })();
  57.  
  58. manager2('restock flavour 50');
  59. manager2('prepare coke 4');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement