Advertisement
Guest User

Untitled

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