dilyana2001

Untitled

Oct 21st, 2021 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ArtGallery {
  2.     constructor(creator) {
  3.         this.creator = creator;
  4.         this.possibleArticles = { "picture": 200, "photo": 50, "item": 250 };
  5.         this.listOfArticles = [];
  6.         this.guests = [];
  7.     }
  8.  
  9.     addArticle(articleModel, articleName, quantity) {
  10.         articleModel = articleModel.toLowerCase();
  11.         if (Object.keys(this.possibleArticles).some(x => x == articleModel)) {
  12.             if (!this.listOfArticles.some(x => x.articleName == articleName)) {
  13.                 this.listOfArticles.push({ articleModel, articleName, quantity });
  14.             } else {
  15.                 let article = this.listOfArticles.find(x => x.articleName == articleName);
  16.                 article.quantity = Number(article.quantity) + Number(quantity);
  17.             }
  18.             return `Successfully added article ${articleName} with a new quantity- ${quantity}.`;
  19.         } else {
  20.             throw new Error(`This article model is not included in this gallery!`)
  21.         }
  22.     }
  23.  
  24.     inviteGuest(guestName, personality) {
  25.         if (this.guests.some(x => x.guestName == guestName)) {
  26.             throw new Error(`${guestName} has already been invited.`);
  27.         } else {
  28.             let points = 0;
  29.             if (personality == 'Vip') {
  30.                 points = 500;
  31.             } else if (personality == 'Middle') {
  32.                 points = 250
  33.             } else if (!personality) {
  34.                 points = 50;
  35.             }
  36.             this.guests.push({ guestName, points, purchaseArticle: 0 });
  37.             return `You have successfully invited ${guestName}!`
  38.         }
  39.     }
  40.  
  41.     buyArticle(articleModel, articleName, guestName) {
  42.         articleModel = articleModel.toLowerCase();
  43.         let model = Object.keys(this.possibleArticles).find(x => x == articleModel);
  44.         let article = this.listOfArticles.find(x => x.articleName == articleName);
  45.         let guest = this.guests.find(x => x.guestName == guestName);
  46.         let requiredPoints = Number((Object.entries(this.possibleArticles).find(x => x[0] == articleModel))[1]);
  47.  
  48.         if (article) {
  49.             if (article.quantity == 0) {
  50.                 return `The ${articleName} is not available`;
  51.             }
  52.         } else if (!article) {
  53.             throw new Error(`This article is not found.`);
  54.         }
  55.         if (article.articleModel != model) {
  56.             throw new Error(`This article is not found.`);
  57.         }
  58.  
  59.         if (!guest) {
  60.             return `This guest is not invited.`;
  61.         } else {
  62.             if (guest.points < requiredPoints) {
  63.                 return `You need to more points to purchase the article.`;
  64.             } else {
  65.                 guest.points -= requiredPoints;
  66.                 article.quantity -= 1;
  67.                 guest.purchaseArticle += 1;
  68.             }
  69.         }
  70.         return `${guestName} successfully purchased the article worth ${requiredPoints} points.`;
  71.     }
  72.  
  73.     showGalleryInfo(criteria) {
  74.         let result = [];
  75.         if (criteria == 'article') {
  76.             result.push(`Articles information:`);
  77.             for (const x of this.listOfArticles) { result.push(`${x.articleModel} - ${x.articleName} - ${x.quantity}`) }
  78.         } else if (criteria == 'guest') {
  79.             result.push(`Guests information:`);
  80.             for (const x of this.guests) { result.push(`${x.guestName} - ${x.purchaseArticle}`) }
  81.         }
  82.         return result.join('\n')
  83.     }
  84. }
  85.  
Add Comment
Please, Sign In to add comment