Advertisement
zornitsa_zlateva

2.Reastaurant

Feb 18th, 2022
915
0
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.     loadProducts(products) {  //"{productName} {productQuantity} {productTotalPrice}"
  9.  
  10.         for (let i = 0; i < products.length; i++) {
  11.             let [productName, productQuantity, productTotalPrice] = products[i].split(" ");
  12.             console.log(`${productName} - ${productQuantity} - ${productTotalPrice}`);
  13.             productQuantity = Number(productQuantity);
  14.             productTotalPrice = Number(productTotalPrice);
  15.  
  16.             let message = '';
  17.             if (this.stockProducts[productName]) {
  18.                 this.stockProducts[productName] += productQuantity;
  19.             } else if (this.stockProducts[productTotalPrice] <= this.budgetMoney) {
  20.                 this.stockProducts.productName = productName;
  21.                 this.stockProducts.productQuantity = productQuantity;
  22.                 this.stockProducts.productTotalPrice = productTotalPrice;
  23.  
  24.                 this.budgetMoney -= this.productTotalPrice;
  25.                 this.stockProducts[productName] = productQuantity;
  26.                 message = `Successfully loaded ${productQuantity} ${productName}`;
  27.                 this.history.push(message);
  28.             } else {
  29.                 message = `There was not enough money to load ${productQuantity} ${productName}`;
  30.                 this.history.push(message);
  31.             }
  32.         }
  33.  
  34.         return this.history.join('\n');
  35.     }
  36. }
  37. let kitchen = new Restaurant(1000);
  38. console.log(kitchen.loadProducts(['Banana 10 5', 'Banana 20 10', 'Strawberries 50 30', 'Yogurt 10 10', 'Yogurt 500 1500', 'Honey 5 50']));
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement