Aliendreamer

kitchen classes js

Feb 24th, 2019
442
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(arr){
  10.         for (const line of arr) {
  11.             let productLine=line.split(' ');        
  12.                 let name=productLine[0];
  13.                 let quantity=Number(productLine[1]);
  14.                 let price=Number(productLine[2]);
  15.        
  16.             let successString=`Successfully loaded ${quantity} ${name}`;
  17.             let failureString=`There was not enough money to load ${quantity} ${name}`;
  18.             let canBuy=this.budget-price>=0;
  19.        
  20.             if(!this.productsInStock.hasOwnProperty(name) && canBuy){
  21.                 this.productsInStock[name]=quantity;
  22.                 this.budget-=price
  23.                 this.actionsHistory.push(successString);
  24.             }else if(canBuy){
  25.                 this.productsInStock[name] += quantity;
  26.                 this.budget-=price;
  27.                 this.actionsHistory.push(successString);
  28.             }else{
  29.                 this.actionsHistory.push(failureString);
  30.             }
  31.         }
  32.         return this.actionsHistory.join('\n').trim()+'\n';
  33.     }
  34.  
  35.     addToMenu(meal,products,price){
  36.         let exist=this.menu.hasOwnProperty(meal);
  37.         if(exist){
  38.            return `The ${meal} is already in our menu, try something different.`;
  39.         }
  40.          this.menu[meal] = {meal, products: products, price};
  41.          let count=Object.keys(this.menu).length;
  42.          return `Great idea! Now with the ${meal} we have ${count} meals in the menu, other ideas?`;
  43.     }
  44.  
  45.     showTheMenu(){
  46.         if(Object.keys(this.menu).length===0){
  47.             return "Our menu is not ready yet, please come later...";
  48.         }
  49.         let result=Object.values(this.menu).map(x=>(`${x.meal} - $ ${x.price}`));
  50.         return result.join('\n').trim()+'\n';
  51.     }
  52.  
  53.    
  54.     makeTheOrder(meal) {
  55.         if (!this.menu.hasOwnProperty(meal)) {
  56.             return `There is not ${meal} yet in our menu, do you want to order something else?`
  57.         }
  58.  
  59.         let neededProducts = this.menu[meal].products;
  60.  
  61.         for (const product of neededProducts) {
  62.             let [productName, productQuantity] = product.split(/\s+/);
  63.             let quantityNeeded = Number(productQuantity);
  64.  
  65.             if (!this.productsInStock.hasOwnProperty(productName) || this.productsInStock[productName] < quantityNeeded) {
  66.                 return `For the time being, we cannot complete your order (${meal}), we are very sorry...`;
  67.             }
  68.         }
  69.  
  70.         neededProducts.forEach((product) => {
  71.             let [productName, productQuantity] = product.split(/\s+/);
  72.             this.productsInStock[productName] -= Number(productQuantity);
  73.         });
  74.  
  75.         let price = this.menu[meal].price;
  76.         this.budget += price;
  77.         return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${price}.`
  78.     }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment