Advertisement
MartinGeorgiev

Kitchen

Mar 12th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Kitchen {
  2.    
  3.     constructor(budget) {
  4.         this.budget = budget;
  5.         this.menu = {};
  6.         this.productsInStock = {};
  7.         this.actionsHistory = [];
  8.     }
  9.  
  10.     loadProducts(products) {
  11.         products.forEach(product => {
  12.             let [productName, productQuantity, productPrice] = product.split(/\s+/);
  13.             let price = Number(productPrice);
  14.             let quantity = Number(productQuantity);
  15.             let action = "";
  16.  
  17.             if (this.budget >= price) {
  18.                 this.budget -= price;
  19.                 if (!this.productsInStock.hasOwnProperty(productName)) {
  20.                     this.productsInStock[productName] = 0;
  21.                 }
  22.                 this.productsInStock[productName] += quantity;
  23.                 this.actionsHistory.push(`Successfully loaded ${quantity} ${productName}`);
  24.             } else {
  25.                 this.actionsHistory.push(`There was not enough money to load ${quantity} ${productName}`);
  26.             }
  27.         });
  28.  
  29.         return this.actionsHistory.join('\n');
  30.     }
  31.  
  32.     addToMenu(meal, neededProducts, price) {
  33.         if (this.menu.hasOwnProperty(meal)) {
  34.             return `${meal} is already in our menu, try something different.`;
  35.         }
  36.         price = +price;
  37.         this.menu[meal] = {meal, products: neededProducts, price};
  38.         return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals in the menu, other ideas?`;
  39.     }
  40.  
  41.     showTheMenu() {
  42.         let meals = Object.keys(this.menu);
  43.         if (meals.length === 0) {
  44.             return 'Our menu is not ready yet, please come later...';
  45.         }
  46.  
  47.         'dawda', 'dwadwad'
  48.         'dawda - 2.5'
  49.  
  50.         meals.map(meal => `${meal} - $ ${this.menu[meal].price}`).join('\n') + '\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 neededProducts = this.menu[meal].products;
  59.  
  60.         for (const product of neededProducts) {
  61.             let [productName, productQuantity] = product.split(/\s+/);
  62.             let quantityNeeded = Number(productQuantity);
  63.  
  64.             if (!this.productsInStock.hasOwnProperty(productName) || this.productsInStock[productName] < quantityNeeded) {
  65.                 return `For the time being, we cannot complete your order (${meal}), we are very sorry...`;
  66.             }
  67.         }
  68.  
  69.         neededProducts.forEach((product) => {
  70.             let [productName, productQuantity] = product.split(/\s+/);
  71.             this.productsInStock[productName] -= Number(productQuantity);
  72.         });
  73.  
  74.         let price = this.menu[meal].price;
  75.         this.budget += price;
  76.         return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${price}.`
  77.     }
  78. }
  79.  
  80. let kitchen = new Kitchen(1000);
  81. console.log(kitchen.loadProducts(['Banana 10 5', 'Banana 20 10', 'Strawberries 50 30', 'Yogurt 10 10', 'Yogurt 500 1500', 'Honey 5 50']));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement