Advertisement
dilyana2001

Untitled

Oct 22nd, 2021
72
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(productArr) {
  13.         let [product, price] = productArr;
  14.         price = Number(price);
  15.         if (this.budget < price) {
  16.             throw new Error(`Not enough money to buy this product`);
  17.         }
  18.         this.products.push(product);
  19.         this.budget -= price;
  20.         return `You have successfully bought ${product}! `
  21.     }
  22.  
  23.     recipes(recipe) {
  24.         let isHasProducts = true;
  25.         recipe.productsList.forEach(product => {
  26.             if (!this.products.includes(product)) {
  27.                 isHasProducts = false;
  28.             }
  29.         });
  30.  
  31.         if (!isHasProducts) {
  32.             return `We do not have this product`;
  33.         }
  34.  
  35.         this.dishes.push(recipe);
  36.         return `${recipe.recipeName} has been successfully cooked!`;
  37.     }
  38.  
  39.     inviteGuests(name, dish) {
  40.         if (!this.dishes.some(x => x.recipeName == dish)) {
  41.             throw new Error(`We do not have this dish`);
  42.         }
  43.         if (this.guests.hasOwnProperty(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.  
  50.     showAttendance() {
  51.         let result = [];
  52.         Object.entries(this.guests).forEach(guest => {
  53.             this.dishes.forEach(dish => {
  54.                 if (dish.recipeName == guest[1]) {
  55.                     result.push(`${guest[0]} will eat ${dish.recipeName}, which consists of ${dish.productsList.join(', ')}`);
  56.                 }
  57.             });
  58.         });
  59.         return result.join('\n');
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement