Advertisement
ilianrusev

Untitled

Feb 3rd, 2022
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Restaurant {
  2. constructor(budgetMoney) {
  3. this.budgetMoney = budgetMoney;
  4. this.menu = {};
  5. this.stockProducts = {};
  6. this.history = [];
  7. }
  8.  
  9. loadProducts(arr) {
  10. for (let el of arr) {
  11. let [productName, productQuantity, productTotalPrice] = el.split(' ');
  12.  
  13. if (+productTotalPrice <= this.budgetMoney) {
  14. if (this.stockProducts.hasOwnProperty(productName)) {
  15. this.stockProducts[productName] += +productQuantity
  16. this.budgetMoney -= +productTotalPrice;
  17. } else {
  18. this.stockProducts[productName] = +productQuantity
  19. this.budgetMoney -= +productTotalPrice
  20. }
  21. this.history.push(`Successfully loaded ${productQuantity} ${productName}`)
  22. } else {
  23. this.history.push(`There was not enough money to load ${productQuantity} ${productName}`)
  24. }
  25.  
  26. }
  27. return this.history.join('\n');
  28.  
  29. }
  30.  
  31. addToMenu(meal, neededProducts, price) {
  32. // neededProducts: "{productName} {productQuantity}"
  33.  
  34. if (!this.menu.hasOwnProperty(meal)) {
  35. this.menu[meal] = {
  36. products: neededProducts,
  37. price: +price
  38. }
  39.  
  40. }
  41. let number = Object.keys(this.menu).length
  42. if (number == 1) {
  43. return `Great idea! Now with the ${meal} we have 1 meal in the menu, other ideas?`
  44. } else {
  45. return `Great idea! Now with the ${meal} we have ${number} meals in the menu, other ideas?`
  46. }
  47. }
  48.  
  49. showTheMenu() {
  50. if (Object.keys(this.menu).length == 0) {
  51. return "Our menu is not ready yet, please come later..."
  52. } else {
  53. let output = []
  54. for (const [key, value] of Object.entries(this.menu)) {
  55. output.push(`${key} - $ ${value.price}`)
  56. }
  57. return output.join("\n")
  58. }
  59. }
  60.  
  61. makeTheOrder(meal) {
  62. if (!this.menu.hasOwnProperty(meal)) {
  63. return `There is not ${meal} yet in our menu, do you want to order something else?`
  64. } else {
  65. let neededProd = Object.values(this.menu[meal])[0] //to get the products arr
  66. for (const el of neededProd) {
  67. let name = el.split(" ")[0]
  68. let quant = el.split(" ")[1]
  69. if (!this.stockProducts.hasOwnProperty(name) || this.stockProducts[name] < quant) {
  70. return `For the time being, we cannot complete your order (${meal}), we are very sorry...`
  71. }
  72. }
  73. for (const el of neededProd) {
  74. let [product, quantity] = el.split(" ")
  75. this.stockProducts[product] -= +quantity
  76. }
  77. this.budgetMoney += this.menu[meal].price
  78.  
  79. return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`
  80. }
  81. }
  82. }
  83.  
Advertisement
RAW Paste Data Copied
Advertisement