Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Restaurant {
- constructor(budget) {
- this.budgetMoney = budget;
- this.menu = {}; //{mealName: {products: [{prod: qty},.. ], price:num},..'}
- this.stockProducts = {}; // {productName: qty, ..}
- this.history = [];
- }
- get budget() {
- return this.budgetMoney;
- }
- loadProducts(products) {
- let currentHistory = [];
- for (let line of products) {
- let [productName, productQuantity, productTotalPrice] =
- line.split(' ');
- productQuantity = Number(productQuantity);
- productTotalPrice = Number(productTotalPrice);
- if (this.budgetMoney < productTotalPrice) {
- this.history.push(
- `There was not enough money to load ${productQuantity} ${productName}`
- );
- currentHistory.push(
- `There was not enough money to load ${productQuantity} ${productName}`
- );
- continue;
- }
- if (this.stockProducts[productName] === undefined) {
- this.stockProducts[productName] = 0;
- }
- this.stockProducts[productName] += productQuantity;
- this.budgetMoney -= productTotalPrice;
- this.history.push(
- `Successfully loaded ${productQuantity} ${productName}`
- );
- currentHistory.push(
- `Successfully loaded ${productQuantity} ${productName}`
- );
- }
- return currentHistory.join('\n');
- }
- addToMenu(meal, neededProducts, price) {
- if (this.menu[meal] !== undefined) {
- return `The ${meal} is already in the our menu, try something different.`;
- }
- this.menu[meal] = {};
- this.menu[meal].price = price;
- this.menu[meal].products = [];
- for (let line of neededProducts) {
- let [productName, productQuantity] = line.split(' ');
- let currProdObj = {};
- currProdObj[productName] = Number(productQuantity);
- this.menu[meal].products.push(currProdObj);
- }
- if (Object.keys(this.menu).length === 1) {
- return `Great idea! Now with the ${meal} 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?`;
- }
- showTheMenu() {
- if (Object.keys(this.menu).length === 0) {
- return 'Our menu is not ready yet, please come later...';
- }
- //{meal} - $ {meal price}
- let result = [];
- Object.keys(this.menu).forEach((meal) =>
- result.push(`${meal} - $ ${this.menu[meal].price}`)
- );
- return result.join('\n');
- }
- makeTheOrder(meal) {
- if (this.menu[meal] === undefined) {
- return `There is not ${meal} yet in our menu, do you want to order something else?`;
- }
- let availableProductsNames = Object.keys(this.stockProducts);
- let neededProducts = this.menu[meal].products;
- if (
- !neededProducts.every(
- (o) =>
- availableProductsNames.includes(Object.keys(o)[0]) &&
- this.stockProducts[Object.keys(o)[0]] >= Object.values(o)[0]
- )
- ) {
- return `For the time being, we cannot complete your order (${meal}), we are very sorry...`;
- }
- neededProducts.forEach((o) => {
- let [currProd, currQty] = Object.entries(o)[0];
- this.stockProducts[currProd] -= currQty;
- });
- this.budgetMoney += 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