Advertisement
kstoyanov

13. Kitchen

Sep 27th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Kitchen {
  2.   constructor(budget) {
  3.     this.budget = Number(budget);
  4.     this.menu = {};
  5.     this.productsInStock = {};
  6.     this.actionsHistory = [];
  7.   }
  8.  
  9.   loadProducts(products) {
  10.     const messageLog = [];
  11.  
  12.  
  13.     products.forEach((str) => {
  14.       const [productName, productQuantity, productPrice] = str.split(' ');
  15.  
  16.       if (this.budget - Number(productPrice) >= 0) {
  17.         if (this.productsInStock[productName]) {
  18.           this.productsInStock[productName] += Number(productQuantity);
  19.         } else {
  20.           this.productsInStock[productName] = Number(productQuantity);
  21.           this.budget -= Number(productPrice);
  22.         }
  23.  
  24.         messageLog.push(`Successfully loaded ${Number(productQuantity)} ${productName}`);
  25.       } else {
  26.         messageLog.push(`There was not enough money to load ${Number(productQuantity)} ${productName}`);
  27.       }
  28.     });
  29.  
  30.     this.actionsHistory = [...this.actionsHistory, ...messageLog];
  31.     return this.actionsHistory.join('\n');
  32.   }
  33.  
  34.   addToMenu(meal, neededIngs, price) {
  35.     if (!this.menu[meal]) {
  36.       this.menu[meal] = {
  37.         products: neededIngs,
  38.         price: Number(price),
  39.       };
  40.       return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals on the menu, other ideas?`;
  41.     }
  42.     return `The ${meal} is already in our menu, try something different.`;
  43.   }
  44.  
  45.   showTheMenu() {
  46.     const toPrint = [];
  47.     Object.keys(this.menu).forEach((key) => {
  48.       toPrint.push(`${key} - $ ${this.menu[key].price}`);
  49.     });
  50.     if (!toPrint.length) {
  51.       return ('Our menu is not ready yet, please come later...');
  52.     }
  53.     return `${toPrint.join('\n')}\n`;
  54.   }
  55.  
  56.   makeTheOrder(meal) {
  57.     if (!this.menu[meal]) {
  58.       return (`There is not ${meal} yet in our menu, do you want to order something else?`);
  59.     }
  60.  
  61.     const ingredientsNeeded = this.menu[meal].products;
  62.     for (const item of ingredientsNeeded) {
  63.       const [product, quantity] = item.split(' ');
  64.  
  65.       if (this.productsInStock[product] < quantity || !this.productsInStock[product]) {
  66.         return (`For the time being, we cannot complete your order (${meal}), we are very sorry...`);
  67.       }
  68.       this.productsInStock[product] -= quantity;
  69.     }
  70.  
  71.  
  72.     this.budget += this.menu[meal].price;
  73.  
  74.     return (`Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`);
  75.   }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement