Advertisement
didkoslawow

Untitled

Jun 7th, 2023
84
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.         if (!this.possibleArticles.hasOwnProperty(articleModel.toLowerCase())) {
  11.             throw new Error('This article model is not included in this gallery!');
  12.         }
  13.  
  14.         const article = this.listOfArticles.find(
  15.             (a) => a.articleModel === articleModel.toLowerCase() && a.articleName === articleName
  16.         );
  17.  
  18.         if (!article) {
  19.             this.listOfArticles.push({
  20.                 articleModel: articleModel.toLowerCase(),
  21.                 articleName,
  22.                 quantity,
  23.             });
  24.         } else {
  25.             article.quantity += Number(quantity);
  26.         }
  27.  
  28.         return `Successfully added article ${articleName} with a new quantity- ${quantity}.`;
  29.     }
  30.  
  31.     inviteGuest(guestName, personality) {
  32.         const guest = this.guests.find((g) => g.guestName === guestName);
  33.         const pointsDict = {
  34.             Vip: 500,
  35.             Middle: 250,
  36.         };
  37.  
  38.         if (guest) {
  39.             throw new Error(`${guestName} has already been invited.`);
  40.         }
  41.  
  42.         this.guests.push({
  43.             guestName,
  44.             points: pointsDict[personality] ? pointsDict[personality] : 50,
  45.             purchaseArticle: 0,
  46.         });
  47.  
  48.         return `You have successfully invited ${guestName}!`;
  49.     }
  50.  
  51.     buyArticle(articleModel, articleName, guestName) {
  52.         const article = this.listOfArticles.find(
  53.             (a) => a.articleModel === articleModel.toLowerCase() && a.articleName === articleName
  54.         );
  55.         const guest = this.guests.find((g) => g.guestName === guestName);
  56.  
  57.         if (!article) {
  58.             throw new Error('This article is not found.');
  59.         }
  60.  
  61.         if (article.quantity === 0) {
  62.             throw new Error(`The ${articleName} is not available.`);
  63.         }
  64.  
  65.         if (!guest) {
  66.             throw new Error('This guest is not invited.');
  67.         }
  68.  
  69.         const articlePoint = Number(this.possibleArticles[articleModel]);
  70.  
  71.         if (articlePoint > guest.points) {
  72.             return 'You need to more points to purchase the article.';
  73.         }
  74.  
  75.         guest.points -= articlePoint;
  76.         article.quantity -= 1;
  77.         guest.purchaseArticle += 1;
  78.  
  79.         return `${guestName} successfully purchased the article worth ${articlePoint} points.`;
  80.     }
  81.  
  82.     showGalleryInfo(criteria) {
  83.         const articlesInformation = this.listOfArticles.map((a) => `${a.articleModel} - ${a.articleName} - ${a.quantity}`);
  84.         const guestsInformation = this.guests.map((g) => `${g.guestName} - ${g.purchaseArticle}`);
  85.  
  86.         const criterias = {
  87.             article: `Articles information:\n${articlesInformation.join('\n')}`,
  88.             guest: `Guests information:\n${guestsInformation.join('\n')}`,
  89.         };
  90.  
  91.         return criterias[criteria];
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement