Advertisement
Guest User

Untitled

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