Advertisement
kstoyanov

3. Christmas Dinner

Oct 17th, 2020
159
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.  
  9.   get budget() {
  10.     return this._budget;
  11.   }
  12.  
  13.   set budget(value) {
  14.     if (value < 1) {
  15.       throw new Error('The budget cannot be a negative number');
  16.     } else {
  17.       this._budget = value;
  18.     }
  19.   }
  20.  
  21.   shopping(productArr) {
  22.     const [type, price] = [...productArr];
  23.     if (Number(price) > this._budget) {
  24.       throw new Error('Not enough money to buy this product');
  25.     } else {
  26.       this.products.push(type);
  27.       this._budget -= Number(price);
  28.  
  29.       return `You have successfully bought ${type}!`;
  30.     }
  31.   }
  32.  
  33.   recipes(recipeObj) {
  34.     const { productsList } = recipeObj;
  35.     const { recipeName } = recipeObj;
  36.  
  37.     productsList.map((p) => {
  38.       if (this.products.includes(p) === false) {
  39.         throw new Error('We do not have this product');
  40.       }
  41.     });
  42.  
  43.     this.dishes.push({
  44.       recipeName,
  45.       productsList,
  46.     });
  47.  
  48.     return `${recipeName} has been successfully cooked!`;
  49.   }
  50.  
  51.   inviteGuests(name, dish) {
  52.     if (!this.dishes.find((d) => d.recipeName === dish)) {
  53.       throw new Error('We do not have this dish');
  54.     }
  55.  
  56.     if (this.guests.hasOwnProperty(name)) {
  57.       throw new Error('This guest has already been invited');
  58.     }
  59.  
  60.     this.guests[name] = dish;
  61.  
  62.     return `You have successfully invited ${name}!`;
  63.   }
  64.  
  65.   showAttendance() {
  66.     let result = '';
  67.  
  68.     Object.keys(this.guests)
  69.       .map((name) => {
  70.         const dish = this.dishes.find((d) => d.recipeName === this.guests[name]);
  71.         const dishProducts = dish.productsList.join(', ');
  72.  
  73.         result += `${name} will eat ${this.guests[name]}, which consists of ${dishProducts}\n`;
  74.       });
  75.  
  76.     return result.trim();
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement