Advertisement
Guest User

ChristmasDinner

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