Advertisement
Guest User

Hmmmm... dude come on

a guest
Jul 4th, 2019
256
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(arrOfStrings) {
  10.         for (let product of arrOfStrings) {
  11.             let [productName, productQuantity, productPrice] = product.split(" ");
  12.             let pattern = { [productName]: Number(productQuantity) };
  13.  
  14.             if (this.budget >= Number(productPrice)) {
  15.                 this.budget -= Number(productPrice);
  16.  
  17.                 let check = false;
  18.                 for (let element of this.productsInStock) {
  19.                     if (element.hasOwnProperty(productName)) {
  20.                         check = true;
  21.                         element[productName] += Number(productQuantity);
  22.                     }
  23.                 }
  24.                 if (check === false) {
  25.                     this.productsInStock.push(pattern);
  26.                 }
  27.                 this.actionsHistory.push(`Successfully loaded ${productQuantity} ${productName}`);
  28.             } else {
  29.                 this.actionsHistory.push(`There was not enough money to load ${productQuantity} ${productName}`);
  30.             }
  31.         }
  32.         return this.actionsHistory.join("\n");
  33.     }
  34.  
  35.     // meal => string | neededProducts => array of strings | price => number
  36.     addToMenu(meal, neededProducts, price) {
  37.         if (this.menu.hasOwnProperty(meal)) {
  38.             return (`The ${meal} is already in our menu, try something different.`);    //return || print??
  39.         } else {
  40.             this.menu[meal] = [];
  41.             this.menu[meal].push(price)
  42.             this.menu[meal].push(neededProducts);
  43.             return `Great idea! Now with the ${meal} we have ${this.numberOfMeals} meals in the menu, other ideas?`
  44.         }
  45.     }
  46.  
  47.     showTheMenu() {
  48.         if (this.numberOfMeals === 0) {
  49.             return (`Our menu is not ready yet, please come later...`);
  50.         } else {
  51.             let output = "";
  52.             Object.entries(this.menu).forEach(([key, value]) => {
  53.                 output += `${key} - $ ${value[0]}\n`;
  54.             });
  55.             return output.trim(); //now it have trim;
  56.         }
  57.     }
  58.  
  59.     get numberOfMeals() {
  60.         let counter = 0;
  61.         for (let key in this.menu) {
  62.             counter++
  63.         }
  64.         return counter;
  65.     }
  66.  
  67.     makeTheOrder(meal) {
  68.         if (!this.menu.hasOwnProperty(meal)) {
  69.             return (`There is not ${meal} yet in our menu, do you want to order something else?`); //return || print??
  70.         } else {
  71.             let price = this.menu[meal][0];
  72.             let products = this.menu[meal][1];
  73.  
  74.             let arrOfTruth = [];            //are there all of the productName's
  75.             let arrAreThereMaterials = [];  //is there enough neededQuantity
  76.  
  77.             for (let product of products) {
  78.                 let [productName, neededQuantity] = product.split(" ");
  79.  
  80.                 for (let obj of this.productsInStock) {
  81.                     if (obj.hasOwnProperty(productName)) {
  82.                         arrOfTruth.push(true);
  83.  
  84.                         if (Object.values(obj) >= Number(neededQuantity)) {
  85.                             arrAreThereMaterials.push(true);
  86.                         }
  87.                     }
  88.                 }
  89.             }
  90.             // console.log(this.productsInStock)
  91.             // console.log(arrOfTruth, arrAreThereMaterials, products.length)
  92.             // console.log(arrOfTruth.length === products.length && arrAreThereMaterials.length === products.length)
  93.             if (arrOfTruth.length === products.length && arrAreThereMaterials.length === products.length) {
  94.                 for (let product of products) {
  95.                     let [productName, neededQuantity] = product.split(" ");
  96.  
  97.                     for (let element of this.productsInStock) {
  98.                         if (element.hasOwnProperty(productName)) {
  99.                             element[productName] -= Number(neededQuantity);
  100.                         }
  101.                     }
  102.                 }
  103.                 this.budget += Number(price);
  104.                 return `Your order ${meal} will be completed in the next 30 minutes and will cost you ${price}.`; //return || print??
  105.             } else {
  106.                 return (`For the time being, we cannot complete your order ${meal}, we are very sorry...`) //return || print??
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. let kitchen = new Kitchen(1000);
  113. console.log(kitchen.loadProducts(['Banana 10 5', 'Banana 20 10', 'Strawberries 50 30', 'Yogurt 10 10', 'Yogurt 500 1500', 'Honey 5 50']));
  114. console.log(kitchen.addToMenu('frozenYogurt', ['Yogurt 1', 'Honey 1', 'Banana 1', 'Strawberries 10'], 9.99));
  115. console.log(kitchen.addToMenu('Pizza', ['Flour 0.5', 'Oil 0.2', 'Yeast 0.5', 'Salt 0.1', 'Sugar 0.1', 'Tomato sauce 0.5', 'Pepperoni 1', 'Cheese 1.5'], 15.55));
  116. console.log(kitchen.showTheMenu());
  117. console.log(kitchen.makeTheOrder('frozenYogurt'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement