simeonshopov

Restaurant

Jun 27th, 2021
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Restaurant {
  2.   constructor(budget) {
  3.     this.budgetMoney = budget;
  4.     this.menu = {};
  5.     this.stockProducts = {};
  6.     this.history = [];
  7.   }
  8.   /**
  9.    *
  10.    * @param {Array} products
  11.    */
  12.   loadProducts(products) {
  13.     products.forEach(product => {
  14.       const tokens = product.split(' ');
  15.       const productName = tokens[0];
  16.       const productQuantity = Number(tokens[1]);
  17.       const productTotalPrice = Number(tokens[2]);
  18.       if (productTotalPrice <= this.budgetMoney) {
  19.         if (!Object.keys(this.stockProducts).includes(productName)) {
  20.           this.stockProducts[productName] = 0;
  21.         }
  22.         this.stockProducts[productName] += productQuantity;
  23.         this.history.push(`Successfully loaded ${productQuantity} ${productName}`);
  24.         this.budgetMoney -= productTotalPrice;
  25.       } else { this.history.push(`There was not enough money to load ${productQuantity} ${productName}`); }
  26.     });
  27.  
  28.     return this.history.join('\n');
  29.   }
  30.  
  31.   /**
  32.    *
  33.    * @param {String} meal
  34.    * @param {Array} neededProducts
  35.    * @param {Number} price
  36.    */
  37.   addToMenu(meal, neededProducts, price) {
  38.     if (!Object.keys(this.menu).includes(meal)) {
  39.       const products = {};
  40.       neededProducts.forEach(info => {
  41.         let [productName, quantity] = info.split(' ');
  42.         quantity = Number(quantity);
  43.         products[productName] = quantity;
  44.       });
  45.       this.menu[meal] = { products: products, price: price };
  46.     } else { return `The ${meal} is already in the our menu, try something different.`; }
  47.  
  48.     if (Object.keys(this.menu).length == 1) {
  49.       return `Great idea! Now with the ${meal} we have 1 meal in the menu, other ideas?`;
  50.     } else { return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals in the menu, other ideas?`; }
  51.   }
  52.  
  53.   /**
  54.    *
  55.    * @param {String} meal
  56.    */
  57.   makeTheOrder(meal) {
  58.     if (!Object.keys(this.menu).includes(meal)) {
  59.       return `There is not ${meal} yet in our menu, do you want to order something else?`;
  60.     } else {
  61.       const stocks = Object.keys(this.stockProducts);
  62.       const { products, price } = this.menu[meal];
  63.       for (let product of Object.keys(products)) {
  64.         const haveProduct = stocks.includes(product);
  65.         const haveEnough = products[product] <= this.stockProducts[product];
  66.         if (!haveProduct || !haveEnough) { return `For the time being, we cannot complete your order (${meal}), we are very sorry...`; }
  67.       }
  68.       for (const product in products) {
  69.         this.stockProducts[product] -= products[product];
  70.       }
  71.       this.budgetMoney += price;
  72.       return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${price}.`;
  73.     }
  74.   }
  75.  
  76.   showTheMenu() {
  77.     if (Object.keys(this.menu).length === 0) {
  78.       return 'Our menu is not ready yet, please come later...';
  79.     }
  80.     const meals = [];
  81.     for (const meal in this.menu) {
  82.       meals.push(`${meal} - $ ${this.menu[meal].price}`);
  83.     }
  84.     return meals.join('\n');
  85.   }
  86.  
  87. }
  88.  
  89. let kitchen = new Restaurant(1000);
  90. console.log(kitchen.loadProducts(['Banana 10 5', 'Banana 20 10', 'Strawberries 50 30', 'Yogurt 10 10', 'Yogurt 500 1500', 'Honey 5 50']));
  91.  
  92. console.log(kitchen.addToMenu('frozenYogurt', ['Yogurt 1', 'Honey 1', 'Banana 1', 'Strawberries 10'], 9.99));
  93. console.log(kitchen.addToMenu('Pizza', ['Flour 0.5', 'Oil 0.2', 'Yeast 0.5', 'Salt 0.1', 'Sugar 0.1', 'Tomato sauce 0.5', 'Pepperoni 1', 'Cheese 1.5'], 15.55));
  94. console.log(kitchen.showTheMenu());
  95.  
  96. // let kitchen = new Restaurant(1000);
  97. // kitchen.loadProducts(['Yogurt 30 3', 'Honey 50 4', 'Strawberries 20 10', 'Banana 5 1']);
  98. // kitchen.addToMenu('frozenYogurt', ['Yogurt 1', 'Honey 1', 'Banana 1', 'Strawberries 10'], 9.99);
  99. // console.log(kitchen.makeTheOrder('frozenYogurt'));
Advertisement
Add Comment
Please, Sign In to add comment