Advertisement
radostina92

artGallery

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