georgiev955

Breakfast Robot

Oct 2nd, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution() {
  2.     let storage = {
  3.         protein: 0,
  4.         carbohydrate: 0,
  5.         fat: 0,
  6.         flavour: 0,
  7.     }
  8.  
  9.     let foodInfo = {
  10.         apple: {
  11.             carbohydrate: 1,
  12.             flavour: 2,
  13.         },
  14.         lemonade: {
  15.             carbohydrate: 10,
  16.             flavour: 20,
  17.         },
  18.         burger: {
  19.             carbohydrate: 5,
  20.             fat: 7,
  21.             flavour: 3,
  22.         },
  23.         eggs: {
  24.             protein: 5,
  25.             fat: 1,
  26.             flavour: 1,
  27.         },
  28.         turkey: {
  29.             protein: 10,
  30.             carbohydrate: 10,
  31.             fat: 10,
  32.             flavour: 10,
  33.         }
  34.     }
  35.  
  36.     let handler = {
  37.         restock(microelement, quantity) {
  38.             storage[microelement] += quantity;
  39.             return "Success"
  40.         },
  41.         prepare(recipe, quantity) {
  42.             let requiredAmmountPerUnit = foodInfo[recipe];
  43.             let items = Object.entries(requiredAmmountPerUnit);
  44.  
  45.             let canCook = true;
  46.  
  47.             for (const pair of items) {
  48.                 let ingredient = pair[0];
  49.                 let ammount = pair[1];
  50.  
  51.                 if (storage[ingredient] < ammount * quantity) {
  52.                     canCook = false;
  53.                     return `Error: not enough ${ingredient} in stock`
  54.                 }
  55.             }
  56.  
  57.             if (canCook) {
  58.                 items.forEach(x => storage[x[0]] -= x[1] * quantity);
  59.                 return `Success`;
  60.             }
  61.         },
  62.         report() {
  63.             let entries = Object.entries(storage);
  64.             return `${entries[0][0]}=${entries[0][1]} ${entries[1][0]}=${entries[1][1]} ${entries[2][0]}=${entries[2][1]} ${entries[3][0]}=${entries[3][1]}`
  65.         }
  66.     }
  67.  
  68.     return function management(param) {
  69.         let [action, token, quantity] = param.split(' ');
  70.         quantity = +quantity;
  71.         if (action !== 'report') {
  72.             return handler[action](token, quantity);
  73.         } else {
  74.             return handler[action]();
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment