Advertisement
Guest User

Best Way 93/100

a guest
Jul 5th, 2019
516
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(arrOfStrings) {
  10.         for (let product of arrOfStrings) {
  11.             let [productName, productQuantity, productPrice] = product.split(" ");
  12.             if (this.budget >= Number(productPrice)) {
  13.                 this.budget -= Number(productPrice);
  14.  
  15.                 if (this.productsInStock.hasOwnProperty(productName)) {
  16.                     this.productsInStock[productName] += productQuantity;
  17.                 } else {
  18.                     this.productsInStock[productName] = productQuantity;
  19.                 }
  20.                 this.actionsHistory.push(`Successfully loaded ${productQuantity} ${productName}`); //test 3
  21.             } else {
  22.                 this.actionsHistory.push(`There was not enough money to load ${productQuantity} ${productName}`); //test 4
  23.             }
  24.         }
  25.         return this.actionsHistory.join("\n"); //test 5
  26.     }
  27.  
  28.     // meal => string | materials => array of strings | price => number
  29.     addToMenu(meal, materials, price) {
  30.         if (this.menu.hasOwnProperty(meal)) {
  31.             return (`The ${meal} is already in our menu, try something different.`);    //test 7
  32.         } else {
  33.             this.menu[meal] = {
  34.                 products: materials,
  35.                 price: price
  36.             };
  37.             return (`Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals in the menu, other ideas?`); //?? test 6
  38.         }
  39.     }
  40.  
  41.     showTheMenu() {
  42.         let output = [];
  43.         for (let key of Object.keys(this.menu)) {
  44.             output.push(`${key} - $ ${this.menu[key].price}`)
  45.         }
  46.         if (output.length === 0) {    //needed for test 8
  47.             return (`Our menu is not ready yet, please come later...`);
  48.         }
  49.         return output.join("\n") + "\n";    //?? + "\n" needed for test 9 ??
  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.         let neededProducts = this.menu[meal].products;
  57.  
  58.         for (let product of neededProducts) {
  59.             let [productName, neededQuantity] = product.split(" ");
  60.             if (this.productsInStock[productName] < neededQuantity || !this.productsInStock.hasOwnProperty(productName)) {
  61.                 return (`For the time being, we cannot complete your order (${meal}), we are very sorry...`); //test 15;
  62.             }
  63.         }
  64.  
  65.         for (let token of neededProducts) {
  66.             let [productName, neededQuantity] = token.split(" ");
  67.             this.productsInStock[productName] -= Number(neededQuantity);
  68.         }
  69.         this.budget += Number(this.menu[meal].price);
  70.         return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`; //test 13
  71.     }
  72. }
  73.  
  74. // let kitchen = new Kitchen(1000);
  75. // console.log(kitchen.loadProducts(['Banana 10 5', 'Banana 20 10', 'Strawberries 50 30', 'Yogurt 10 10', 'Yogurt 500 1500', 'Honey 5 50']));
  76. // console.log(kitchen.addToMenu('frozenYogurt', ['Yogurt 1', 'Honey 1', 'Banana 1', 'Strawberries 10'], 9.99));
  77. // 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));
  78. // console.log(kitchen.showTheMenu());
  79. // console.log(kitchen.makeTheOrder('frozenYogurt'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement