Advertisement
didkoslawow

Untitled

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