Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ChristmasDinner {
- constructor(budget){
- this.budget = Number(budget);
- this.dishes = [];
- this.products = [];
- this.guests = {};
- }
- get budget(){
- return this._budget;
- }
- set budget(budget){
- if(this.budget < 0){
- return 'The budget cannot be a negative number';
- }else{
- return this._budget = budget;
- }
- }
- shopping(product){
- if(this.budget > product[1]){
- this.products.push(product[0]);
- this.budget -= product[1];
- return `You have successfully bought ${product[0]}!`;
- }else{
- throw new Error ('Not enough money to buy this product');
- }
- }
- recipes(recipe){
- let checker = (arr, target) => target.every(v => arr.includes(v));
- if(checker(this.products, recipe.productsList)){
- this.dishes.push({recipeName:recipe.recipeName, productList: recipe.productsList});
- return `${recipe.recipeName} has been successfully cooked!`
- }else{
- throw new Error ('We do not have this product');
- }
- }
- inviteGuests(name, dish){
- const found = this.dishes.some(item => item.recipeName === dish);
- if(found){
- if(this.guests[name]){
- throw new Error('This guest has already been invited')
- }else{
- this.guests[name] = dish;
- return `You have successfully invited ${name}!`;
- }
- }else{
- throw new Error ('We do not have this dish');
- }
- }
- showAttendance(){
- let result = "";
- for (const name in this.guests) {
- const targetDish = this.dishes.find(d => d.recipeName === this.guests[name]);
- result += `${name} will eat ${this.guests[name]}, which consists of ${targetDish.productList.join(', ')}\n`;
- }
- return result.trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement