Todorov_Stanimir

03. Christmas Dinner JS Advanced Retake Exam - 10 December 2

Dec 10th, 2019
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ChristmasDinner {
  2.     //test 1, 2, 3
  3.     _budget
  4.     constructor(_budget) {
  5.         this.dishes = [];
  6.         this.products = [];
  7.         this.guests = {};
  8.         this.budget = _budget;
  9.     }
  10.  
  11.     set budget(value) {
  12.         if (value < 0) {
  13.             throw new Error(`The budget cannot be a negative number`);
  14.         }
  15.         this._budget = value;
  16.     }
  17.  
  18.     get budget() { return this._budget };
  19.     //test 4, 5
  20.     shopping(arr) {
  21.         const [typeProduct, price] = [...arr];
  22.  
  23.         if (this.budget < price)
  24.             throw new Error('Not enough money to buy this product')
  25.  
  26.         this.products.push(typeProduct);
  27.         this.budget -= price;
  28.  
  29.         return `You have successfully bought ${typeProduct}!`
  30.     }
  31.     //test 6, 7
  32.     recipes(recipe) {
  33.         const { recipeName, productsList } = recipe;
  34.         const missingProduct = productsList.filter(product => !this.products.includes(product)).length;
  35.  
  36.         if (missingProduct)
  37.             throw new Error(`We do not have this product`);
  38.  
  39.         this.dishes.push(recipe);
  40.  
  41.         return `${recipeName} has been successfully cooked!`
  42.     }
  43.     //test 8, 9
  44.     inviteGuests(name, dish) {
  45.         const indexDish = this.dishes.findIndex(recipe => recipe.recipeName === dish);
  46.  
  47.         if (indexDish === -1)
  48.             throw new Error(`We do not have this dish`);
  49.  
  50.         const indexGuest = Object.keys(this.guests).findIndex(guest => guest === name);
  51.  
  52.         if (indexGuest !== -1)
  53.             throw new Error(`This guest has already been invited`);
  54.  
  55.         this.guests[name] = dish;
  56.  
  57.         return `You have successfully invited ${name}!`
  58.     }
  59.     //test 10
  60.     showAttendance() {
  61.         return Object.entries(this.guests).map(guest => {
  62.             return `${guest[0]} will eat ${guest[1]}, which consists of ${this.dishes.find(recipe => recipe.recipeName === guest[1]).productsList.join(', ')}`
  63.         }).join('\n')
  64.     }
  65. }
  66.  
  67. let dinner = new ChristmasDinner(300);
  68.  
  69. dinner.shopping(['Salt', 1]);
  70. dinner.shopping(['Beans', 3]);
  71. dinner.shopping(['Cabbage', 4]);
  72. dinner.shopping(['Rice', 2]);
  73. dinner.shopping(['Savory', 1]);
  74. dinner.shopping(['Peppers', 1]);
  75. dinner.shopping(['Fruits', 40]);
  76. dinner.shopping(['Honey', 10]);
  77.  
  78. dinner.recipes({
  79.     recipeName: 'Oshav',
  80.     productsList: ['Fruits', 'Honey']
  81. });
  82. dinner.recipes({
  83.     recipeName: 'Folded cabbage leaves filled with rice',
  84.     productsList: ['Cabbage', 'Rice', 'Salt', 'Savory']
  85. });
  86. dinner.recipes({
  87.     recipeName: 'Peppers filled with beans',
  88.     productsList: ['Beans', 'Peppers', 'Salt']
  89. });
  90.  
  91. dinner.inviteGuests('Ivan', 'Oshav');
  92. dinner.inviteGuests('Petar', 'Folded cabbage leaves filled with rice');
  93. dinner.inviteGuests('Georgi', 'Peppers filled with beans');
  94.  
  95. console.log(dinner.showAttendance());
Advertisement
Add Comment
Please, Sign In to add comment