Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. class Kitchen
  2. {
  3.  
  4. constructor(budget)
  5. {
  6. this.budget = budget;
  7. this.menu = [];
  8. this.productsInStock = [];
  9. this.actionsHistory = [];
  10. }
  11.  
  12. loadProducts(products)
  13. {
  14. for (const element of products)
  15. {
  16. let product = element.split(" ");
  17. let productName = product[0];
  18. let productQuantity = Number(product[1])
  19. let productPrice = Number(product[2]);
  20.  
  21. if (this.budget - productPrice >= 0)
  22. {
  23. if (this.productsInStock[productName])
  24. {
  25. this.productsInStock[productName] += productQuantity;
  26. }
  27. else
  28. {
  29. this.productsInStock[productName] = productQuantity;
  30. }
  31. this.budget -= productPrice;
  32.  
  33. this.actionsHistory.push(`Successfully loaded ${productQuantity} ${productName}`)
  34. }
  35. else
  36. {
  37. this.actionsHistory.push(`There was not enough money to load ${productQuantity} ${productName}`)
  38. }
  39.  
  40. }
  41. return this.actionsHistory.join("\n").trim();
  42. }
  43. addToMenu(meal, neededProducts, price)
  44. {
  45. if (this.menu[meal])
  46. {
  47. return `The ${meal} is already in our menu, try something different.`;
  48. }
  49.  
  50. this.menu[meal] =
  51. {
  52. products: neededProducts,
  53. price : Number(price)
  54. };
  55.  
  56. return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals in the menu, other ideas?`;
  57.  
  58. }
  59. showTheMenu()
  60. {
  61. let result = [];
  62.  
  63. for (const key of Object.keys(this.menu)) {
  64. result.push(`${key} - $ ${this.menu[key].price}`);
  65. }
  66.  
  67. if (result.length === 0) {
  68. return "Our menu is not ready yet, please come later...";
  69. }
  70.  
  71. return result.join("\n") + '\n';
  72. }
  73. makeTheOrder(meal)
  74. {
  75. let currentMeal = [];
  76. if (!this.menu[meal])
  77. {
  78. return `There is not ${meal} yet in our menu, do you want to order something else?`
  79. }
  80.  
  81. currentMeal = this.menu[meal];
  82. for (const element of currentMeal.products)
  83. {
  84. let product = element.split(" ");
  85. let neededProductName = product[0];
  86. let neededProductQuantity = Number(product[1]);
  87.  
  88. if (!this.productsInStock[neededProductName] || this.productsInStock[neededProductName] < neededProductQuantity)
  89. {
  90. return `For the time being, we cannot complete your order (${meal}), we are very sorry...`
  91. }
  92. }
  93. for (const element of currentMeal.products)
  94. {
  95. let product = element.split(" ");
  96. let neededProductName = product[0];
  97. let neededProductQuantity = Number(product[1]);
  98.  
  99. this.productsInStock[neededProductName] -= neededProductQuantity;
  100.  
  101. }
  102. this.budget += currentMeal.price;
  103. return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${currentMeal.price}.`.trim();
  104.  
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement