Dragomana

Christmas Dinner

May 27th, 2022 (edited)
421
336 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. lass 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.         } else {
  15.             this._budget = Number(value)
  16.         }
  17.     }
  18.     shopping(product) {
  19.         let [productName, price] = product
  20.         price = Number(price)
  21.         if (this.budget < price) {
  22.             throw new Error('Not enough money to buy this product');
  23.         }
  24.         this.products.push(productName)
  25.         this.budget -= price;
  26.         return `You have successfully bought ${productName}!`;
  27.     }
  28.  
  29.     recipes(recipe) {
  30.         let {
  31.             recipeName,
  32.             productsList
  33.         } = recipe;
  34.         productsList.forEach(p => {
  35.  
  36.            if(!this.products.includes(p)) {
  37.                throw new Error('We do not have this product');
  38.            }
  39.            
  40.         });
  41.         this.dishes.push(recipe)
  42.         return `${recipeName} has been successfully cooked!`;
  43.     }
  44.  
  45.     inviteGuests(name, dish) {
  46.         let curDish = this.dishes.find(d => d.recipeName == dish);
  47.         if (!curDish) {
  48.             throw new Error('We do not have this dish');
  49.         }
  50.         if (this.guests.hasOwnProperty(name)) {
  51.             throw new Error(`This guest has already been invited`);
  52.         }
  53.         this.guests[name] = dish
  54.         return `You have successfully invited ${name}!`;
  55.     }
  56.  
  57.     showAttendance() {
  58.         let result = [];
  59.         let guestArr = Object.entries(this.guests)
  60.         guestArr.forEach(g => {
  61.             let productArr = this.dishes.find(d => d.recipeName == g[1])
  62.             result.push(`${g[0]} will eat ${g[1]}, which consists of ${productArr.productsList.join(', ')}`)
  63.         })
  64.  
  65.         return result.join('\n')
  66.     }
  67. }
RAW Paste Data Copied