Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Kitchen
- {
- constructor(budget)
- {
- this.budget = budget;
- this.menu = [];
- this.productsInStock = [];
- this.actionsHistory = [];
- }
- loadProducts(products)
- {
- for (const element of products)
- {
- let product = element.split(" ");
- let productName = product[0];
- let productQuantity = Number(product[1])
- let productPrice = Number(product[2]);
- if (this.budget - productPrice >= 0)
- {
- if (this.productsInStock[productName])
- {
- this.productsInStock[productName] += productQuantity;
- }
- else
- {
- this.productsInStock[productName] = productQuantity;
- }
- this.budget -= productPrice;
- this.actionsHistory.push(`Successfully loaded ${productQuantity} ${productName}`)
- }
- else
- {
- this.actionsHistory.push(`There was not enough money to load ${productQuantity} ${productName}`)
- }
- }
- return this.actionsHistory.join("\n").trim();
- }
- addToMenu(meal, neededProducts, price)
- {
- if (this.menu[meal])
- {
- return `The ${meal} is already in our menu, try something different.`;
- }
- this.menu[meal] =
- {
- products: neededProducts,
- price : Number(price)
- };
- return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals in the menu, other ideas?`;
- }
- showTheMenu()
- {
- let result = [];
- for (const key of Object.keys(this.menu)) {
- result.push(`${key} - $ ${this.menu[key].price}`);
- }
- if (result.length === 0) {
- return "Our menu is not ready yet, please come later...";
- }
- return result.join("\n") + '\n';
- }
- makeTheOrder(meal)
- {
- let currentMeal = [];
- if (!this.menu[meal])
- {
- return `There is not ${meal} yet in our menu, do you want to order something else?`
- }
- currentMeal = this.menu[meal];
- for (const element of currentMeal.products)
- {
- let product = element.split(" ");
- let neededProductName = product[0];
- let neededProductQuantity = Number(product[1]);
- if (!this.productsInStock[neededProductName] || this.productsInStock[neededProductName] < neededProductQuantity)
- {
- return `For the time being, we cannot complete your order (${meal}), we are very sorry...`
- }
- }
- for (const element of currentMeal.products)
- {
- let product = element.split(" ");
- let neededProductName = product[0];
- let neededProductQuantity = Number(product[1]);
- this.productsInStock[neededProductName] -= neededProductQuantity;
- }
- this.budget += currentMeal.price;
- return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${currentMeal.price}.`.trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement