Advertisement
Liliana797979

2_restaurant - js advanced exam

Oct 15th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Restaurant {
  2.     constructor(budget) {
  3.  
  4.         this.budgetMoney = Number(budget);
  5.         this.menu = {};
  6.         this.stockProducts = {};
  7.         this.history = [];
  8.     }
  9.  
  10.  
  11.     loadProducts(products) {
  12.         products.forEach((el) => {
  13.             let [name, quantity, totalPrice] = el.split(" ");
  14.  
  15.  
  16.             quantity = Number(quantity);
  17.             totalPrice = Number(totalPrice);
  18.             if (this.budgetMoney >= totalPrice) {
  19.                 if (!this.stockProducts[name]) {
  20.                     this.stockProducts[name] = 0;
  21.                 }
  22.                 this.stockProducts[name]+= quantity;
  23.                 this.budgetMoney-= totalPrice;
  24.                 this.history.push(`Successfully loaded ${quantity} ${name}`);
  25.             } else {
  26.                 this.history.push(`There was not enough money to load ${quantity} ${name}`);
  27.             }
  28.         });
  29.  
  30.         return this.history.join("\n");
  31.     }
  32.  
  33.     addToMenu(meal, products, price) {
  34.         if (!this.menu[meal]) {
  35.             this.menu[meal] = {
  36.                 products: {},
  37.                 price: price
  38.             }
  39.  
  40.             products.forEach((el)=> {
  41.                 let [name, quantity] = el.split(" ");
  42.                 quantity = Number(quantity);
  43.                 this.menu[meal].products[name] = quantity;
  44.             })
  45.  
  46.             const mealCount = Object.keys(this.menu).length;
  47.             if (mealCount == 1) {
  48.                 return `Great idea! Now with the ${meal} we have 1 meal in the menu, other ideas?`
  49.             } else {
  50.                 return `Great idea! Now with the ${meal} we have ${mealCount} meals in the menu, other ideas?`
  51.             }
  52.  
  53.         } else {
  54.             return `The ${meal} is already in the our menu, try something different.`
  55.         }
  56.     }
  57.  
  58.     showTheMenu() {
  59.         if (Object.keys(this.menu).length == 0) {
  60.             return "Our menu is not ready yet, please come later...";
  61.         } else {
  62.             let result = [];
  63.  
  64.             for (let meal in this.menu) {
  65.                 result.push(`${meal} - $ ${this.menu[meal].price}`);
  66.             }
  67.  
  68.             return result.join("\n");
  69.         }
  70.     }
  71.  
  72.     makeTheOrder(meal) {
  73.         if (!this.menu[meal]) {
  74.             return `There is not ${meal} yet in our menu, do you want to order something else?`
  75.         } else {
  76.             const neededProducts = {};
  77.             for (let product in this.menu[meal].products) {
  78.                 if (!this.stockProducts[product] || this.stockProducts[product] < this.menu[meal].products[product]) {
  79.                     return `For the time being, we cannot complete your order (${meal}), we are very sorry...`
  80.                 } else {
  81.                     neededProducts[product] = this.menu[meal].products[product];
  82.                 }
  83.             }
  84.  
  85.             for (let neededProduct in neededProducts) {
  86.                 this.stockProducts[neededProduct] -= neededProducts[neededProduct];
  87.             }
  88.  
  89.             this.budgetMoney+= this.menu[meal].price;
  90.  
  91.             return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement