viligen

restaurant

Jun 18th, 2022 (edited)
911
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 = {}; //{mealName: {products: [{prod: qty},.. ], price:num},..'}
  5.         this.stockProducts = {}; // {productName: qty, ..}
  6.         this.history = [];
  7.     }
  8.     get budget() {
  9.         return this.budgetMoney;
  10.     }
  11.     loadProducts(products) {
  12.         let currentHistory = [];
  13.         for (let line of products) {
  14.             let [productName, productQuantity, productTotalPrice] =
  15.                 line.split(' ');
  16.             productQuantity = Number(productQuantity);
  17.             productTotalPrice = Number(productTotalPrice);
  18.             if (this.budgetMoney < productTotalPrice) {
  19.                 this.history.push(
  20.                     `There was not enough money to load ${productQuantity} ${productName}`
  21.                 );
  22.                 currentHistory.push(
  23.                     `There was not enough money to load ${productQuantity} ${productName}`
  24.                 );
  25.                 continue;
  26.             }
  27.             if (this.stockProducts[productName] === undefined) {
  28.                 this.stockProducts[productName] = 0;
  29.             }
  30.             this.stockProducts[productName] += productQuantity;
  31.             this.budgetMoney -= productTotalPrice;
  32.             this.history.push(
  33.                 `Successfully loaded ${productQuantity} ${productName}`
  34.             );
  35.             currentHistory.push(
  36.                 `Successfully loaded ${productQuantity} ${productName}`
  37.             );
  38.         }
  39.         return currentHistory.join('\n');
  40.     }
  41.     addToMenu(meal, neededProducts, price) {
  42.         if (this.menu[meal] !== undefined) {
  43.             return `The ${meal} is already in the our menu, try something different.`;
  44.         }
  45.         this.menu[meal] = {};
  46.         this.menu[meal].price = price;
  47.         this.menu[meal].products = [];
  48.         for (let line of neededProducts) {
  49.             let [productName, productQuantity] = line.split(' ');
  50.             let currProdObj = {};
  51.             currProdObj[productName] = Number(productQuantity);
  52.             this.menu[meal].products.push(currProdObj);
  53.         }
  54.         if (Object.keys(this.menu).length === 1) {
  55.             return `Great idea! Now with the ${meal} we have 1 meal in the menu, other ideas?`;
  56.         }
  57.         return `Great idea! Now with the ${meal} we have ${
  58.             Object.keys(this.menu).length
  59.         } meals in the menu, other ideas?`;
  60.     }
  61.     showTheMenu() {
  62.         if (Object.keys(this.menu).length === 0) {
  63.             return 'Our menu is not ready yet, please come later...';
  64.         }
  65.         //{meal} - $ {meal price}
  66.         let result = [];
  67.         Object.keys(this.menu).forEach((meal) =>
  68.             result.push(`${meal} - $ ${this.menu[meal].price}`)
  69.         );
  70.         return result.join('\n');
  71.     }
  72.     makeTheOrder(meal) {
  73.         if (this.menu[meal] === undefined) {
  74.             return `There is not ${meal} yet in our menu, do you want to order something else?`;
  75.         }
  76.         let availableProductsNames = Object.keys(this.stockProducts);
  77.         let neededProducts = this.menu[meal].products;
  78.         if (
  79.             !neededProducts.every(
  80.                 (o) =>
  81.                     availableProductsNames.includes(Object.keys(o)[0]) &&
  82.                     this.stockProducts[Object.keys(o)[0]] >= Object.values(o)[0]
  83.             )
  84.         ) {
  85.             return `For the time being, we cannot complete your order (${meal}), we are very sorry...`;
  86.         }
  87.         neededProducts.forEach((o) => {
  88.             let [currProd, currQty] = Object.entries(o)[0];
  89.             this.stockProducts[currProd] -= currQty;
  90.         });
  91.         this.budgetMoney += this.menu[meal].price;
  92.         return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`;
  93.     }
  94. }
Add Comment
Please, Sign In to add comment