Advertisement
viligen

christmasDinner

Jun 20th, 2022
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ChristmasDinner {
  2.     constructor(budget) {
  3.         this.budget = budget;
  4.         this.dishes = [];
  5.         this.products = [];
  6.         this.guests = {};
  7.     }
  8.     get budget() {
  9.         return this._budget;
  10.     }
  11.     set budget(value) {
  12.         if (value < 0) {
  13.             throw new Error('The budget cannot be a negative number');
  14.         }
  15.         this._budget = value;
  16.     }
  17.     shopping([...product]) {
  18.         let [productName, price] = product;
  19.         // price = Number(price);
  20.         if (this.budget < price) {
  21.             throw new Error('Not enough money to buy this product');
  22.         }
  23.         this.budget -= price;
  24.         this.products.push(productName);
  25.         return `You have successfully bought ${productName}!`;
  26.     }
  27.     recipes(recipe) {
  28.         if (recipe.productsList.every((p) => this.products.includes(p))) {
  29.             this.dishes.push({
  30.                 recipeName: recipe.recipeName,
  31.                 productsList: recipe.productsList,
  32.             });
  33.             return `${recipe.recipeName} has been successfully cooked!`;
  34.         } else {
  35.             throw new Error('We do not have this product');
  36.         }
  37.     }
  38.     inviteGuests(name, dish) {
  39.         let searchedDish = this.dishes.find((d) => d.recipeName == dish);
  40.         if (!searchedDish) {
  41.             throw new Error('We do not have this dish');
  42.         }
  43.         if (Object.keys(this.guests).includes(name)) {
  44.             throw new Error('This guest has already been invited');
  45.         }
  46.         this.guests[name] = dish;
  47.         return `You have successfully invited ${name}!`;
  48.     }
  49.     showAttendance() {
  50.         let result = [];
  51.         Object.entries(this.guests).forEach((g) => {
  52.             let [name, dish] = g;
  53.             let dishObj = this.dishes.find((d) => d.recipeName == dish);
  54.             let products = dishObj.productsList;
  55.             result.push(
  56.                 `${name} will eat ${dish}, which consists of ${products.join(
  57.                     ', '
  58.                 )}`
  59.             );
  60.         });
  61.         return result.join('\n');
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement