Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Restaurant {
- constructor(budgetMoney) {
- this.budgetMoney = budgetMoney;
- this.menu = {};
- this.stockProducts = {};
- this.history = [];
- }
- loadProducts(products) {
- products.forEach(x => {
- let [name, quantity, totalPrice] = x.split(' ');
- quantity = Number(quantity);
- totalPrice = Number(totalPrice);
- if (this.budgetMoney - totalPrice < 0) {
- this.history.push(`There was not enough money to load ${quantity} ${name}`);
- return;
- }
- if (!this.stockProducts.hasOwnProperty(name)) {
- this.stockProducts[name] = 0;
- }
- this.stockProducts[name] += quantity;
- this.budgetMoney -= totalPrice;
- this.history.push(`Successfully loaded ${quantity} ${name}`);
- });
- return this.history.join('\n');
- }
- addToMenu(meal, products, price) {
- price = Number(price);
- if (!this.menu.hasOwnProperty(meal)) {
- this.menu[meal] = { products, price };
- if (Object.keys(this.menu).length == 1) {
- return `Great idea! Now with the ${Object.keys(this.menu)[0]} we have 1 meal in the menu, other ideas?`;
- }
- return `Great idea! Now with the ${meal} we have ${Object.keys(this.menu).length} meals in the menu, other ideas?`;
- }
- return `The ${meal} is already in the our menu, try something different.`;
- }
- showTheMenu() {
- let result = [];
- for (const x in this.menu) {
- result.push(`${x} - $ ${this.menu[x].price}`);
- }
- if (result.length == 0) {
- return "Our menu is not ready yet, please come later...";
- }
- return result.join('\n');
- }
- makeTheOrder(meal) {
- if (!this.menu.hasOwnProperty(meal)) {
- return `There is not ${meal} yet in our menu, do you want to order something else?`;
- }
- let allInstock = true;
- this.menu[meal].products.forEach(x => {
- let [mealProduct, ] = x.split(' ');
- if (!this.stockProducts.hasOwnProperty(mealProduct)) {
- allInstock = false;
- return `For the time being, we cannot complete your order (${meal}), we are very sorry...`;
- }
- });
- if (allInstock) {
- this.menu[meal].products.forEach(meal => {
- let [mealProduct, mealQuantity] = meal.split(' ');
- mealQuantity = Number(mealQuantity);
- if (this.stockProducts[mealProduct].quantity >= mealQuantity) {
- this.stockProducts[mealProduct].quantity -= mealQuantity;
- }
- });
- this.budgetMoney += Number(this.menu[meal].price);
- return `Your order (${meal}) will be completed in the next 30 minutes and will cost you ${this.menu[meal].price}.`;
- }
- }
- }
Add Comment
Please, Sign In to add comment