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