Advertisement
poli1993_

Untitled

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