Advertisement
Guest User

Untitled

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