Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. function solve() {
  2. let robot = {
  3. protein: 0,
  4. carbohydrates: 0,
  5. fat: 0,
  6. flavour: 0,
  7. prepareCoke: function (quantity) {
  8. if (this.carbohydrates < 10 * Number(quantity)) {
  9. return 'Error: not enough carbohydrates in stock';
  10. }
  11. if (this.flavour < 20 * Number(quantity)) {
  12. return `Error: not enough flavour in stock`;
  13. }
  14. this.carbohydrates -= 10 * Number(quantity);
  15. this.flavour -= 20 * Number(quantity);
  16. return "Success";
  17. },
  18. prepareApple: function (quantity) {
  19. if (this.carbohydrates < Number(quantity)) {
  20. return `Error: not enough carbohydrates in stock`;
  21. }
  22. if (this.flavour < 2 * Number(quantity)) {
  23. return `Error: not enough flavour in stock`;
  24. }
  25. this.carbohydrates -= Number(quantity);
  26. this.flavour -= 2 * Number(quantity);
  27. return "Success";
  28. },
  29. prepareBurger: function (quantity) {
  30. if (this.carbohydrates < 5 * Number(quantity)) {
  31. return `Error: not enough carbohydrates in stock`;
  32. }
  33. if (this.fat < 7 * Number(quantity)) {
  34. return `Error: not enough fat in stock`;
  35. }
  36. if (this.flavour < 3 * Number(quantity)) {
  37. return `Error: not enough flavour in stock`;
  38. }
  39. this.carbohydrates -= 5 * Number(quantity);
  40. this.fat -= 7 * Number(quantity);
  41. this.flavour -= 3 * Number(quantity);
  42. return "Success";
  43. },
  44. prepareOmelet: function (quantity) {
  45. if (this.protein < 5 * Number(quantity)) {
  46. return `Error: not enough protein in stock`;
  47. }
  48. if (this.fat < Number(quantity)) {
  49. return `Error: not enough fat in stock`;
  50. }
  51. if (this.flavour < Number(quantity)) {
  52. return `Error: not enough flavour in stock`;
  53. }
  54. this.protein -= 5 * Number(quantity);
  55. this.fat -= Number(quantity);
  56. this.flavour -= Number(quantity);
  57. return "Success";
  58. },
  59. prepareCheverme: function (quantity) {
  60. if (this.protein < 10 * Number(quantity)) {
  61. return `Error: not enough protein in stock`;
  62. }
  63. if (this.carbohydrates < 10 * Number(quantity)) {
  64. return `Error: not enough carbohydrates in stock`;
  65. }
  66. if (this.fat < 10 * Number(quantity)) {
  67. return `Error: not enough fat in stock`;
  68. }
  69. if (this.flavour < 10 * Number(quantity)) {
  70. return `Error: not enough flavour in stock`;
  71. }
  72. this.protein -= 10 * Number(quantity);
  73. this.carbohydrates -= 10 * Number(quantity);
  74. this.fat -= 10 * Number(quantity);
  75. this.flavour -= 10 * Number(quantity);
  76. return "Success";
  77. }
  78. };
  79.  
  80. return function (instruction) {
  81. let commandArgs = instruction.split(' ');
  82.  
  83. switch (commandArgs[0]) {
  84. case "restock":
  85. restock(commandArgs[1], commandArgs[2]);
  86. break;
  87. case "prepare":
  88. prepare(commandArgs[1], commandArgs[2]);
  89. break;
  90. case "report":
  91. report();
  92. break;
  93. }
  94.  
  95. function restock(ingredient, quantity) {
  96. if (robot.hasOwnProperty(ingredient)) {
  97. robot[ingredient] += Number(quantity);
  98. return "Success";
  99. }
  100. }
  101.  
  102. function prepare(item, quantity) {
  103. switch (item) {
  104. case "coke":
  105. robot.prepareCoke(quantity);
  106. break;
  107. case "apple":
  108. robot.prepareApple(quantity);
  109. break;
  110. case "burger":
  111. robot.prepareBurger(quantity);
  112. break;
  113. case "cheverme":
  114. robot.prepareCheverme(quantity);
  115. break;
  116. case "omelet":
  117. robot.prepareOmelet(quantity);
  118. break;
  119.  
  120. }
  121. }
  122.  
  123. function report() {
  124. return `protein=${robot.protein} carbohydrate=${robot.carbohydrates} fat=${robot.fat} flavour=${robot.flavour}`;
  125. }
  126. }
  127. }
  128.  
  129. let manager = solve();
  130. manager('restock flavour 50');
  131. manager("report");
  132. manager("prepare coke 4");
  133. manager("report");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement