dilyana2001

Untitled

Oct 22nd, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. class Restaurant {
  2. constructor(budgetMoney) {
  3. this.budgetMoney = budgetMoney;
  4. this.menu = {};
  5. this.stockProducts = {};
  6. this.history = [];
  7. }
  8.  
  9. loadProducts(products) {
  10. products.forEach(x => {
  11. let [name, quantity, totalPrice] = x.split(' ');
  12. quantity = Number(quantity);
  13. totalPrice = Number(totalPrice);
  14.  
  15. if (this.budgetMoney - totalPrice < 0) {
  16. this.history.push(`There was not enough money to load ${quantity} ${name}`);
  17. return;
  18. }
  19. if (!this.stockProducts.hasOwnProperty(name)) {
  20. this.stockProducts[name] = 0;
  21. }
  22. this.stockProducts[name] += quantity;
  23. this.budgetMoney -= totalPrice;
  24. this.history.push(`Successfully loaded ${quantity} ${name}`);
  25.  
  26. });
  27. return this.history.join('\n');
  28. }
  29.  
  30. addToMenu(meal, products, price) {
  31. price = Number(price);
  32. if (!this.menu.hasOwnProperty(meal)) {
  33. this.menu[meal] = { products, price };
  34. if (Object.keys(this.menu).length == 1) {
  35. return `Great idea! Now with the ${Object.keys(this.menu)[0]} we have 1 meal in the menu, other ideas?`;
  36. }
  37. return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals in the menu, other ideas?`;
  38. }
  39. return `The ${meal} is already in the our menu, try something different.`;
  40. }
  41.  
  42. showTheMenu() {
  43. let result = [];
  44. for (const x in this.menu) {
  45. result.push(`${x} - $ ${this.menu[x].price}`);
  46. }
  47. if (result.length == 0) {
  48. return "Our menu is not ready yet, please come later...";
  49. }
  50. return result.join('\n');
  51. }
  52.  
  53. makeTheOrder(meal) {
  54. if (!this.menu.hasOwnProperty(meal)) {
  55. return `There is not ${meal} yet in our menu, do you want to order something else?`;
  56. }
  57.  
  58. let allInstock = true;
  59. this.menu[meal].products.forEach(x => {
  60. let [mealProduct, ] = x.split(' ');
  61. if (!this.stockProducts.hasOwnProperty(mealProduct)) {
  62. allInstock = false;
  63. return `For the time being, we cannot complete your order (${meal}), we are very sorry...`;
  64. }
  65. });
  66.  
  67. if (allInstock) {
  68. this.menu[meal].products.forEach(meal => {
  69. let [mealProduct, mealQuantity] = meal.split(' ');
  70. mealQuantity = Number(mealQuantity);
  71. if (this.stockProducts[mealProduct].quantity >= mealQuantity) {
  72. this.stockProducts[mealProduct].quantity -= mealQuantity;
  73. }
  74. });
  75. this.budgetMoney += Number(this.menu[meal].price);
  76. return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`;
  77. }
  78. }
  79. }
Add Comment
Please, Sign In to add comment