Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ArtGallery{
- constructor(creator){
- this.creator = creator;
- this.possibleArticles = { "picture":200,"photo":50,"item":250 };
- this.listOfArticles = [];
- this.guests = [];
- }
- addArticle(articleModel, articleName, quantity){
- let model = articleModel.toLowerCase();
- if(model != 'picture' && model != 'photo' && model != 'item'){
- throw new Error("This article model is not included in this gallery!");
- }
- let article = this.listOfArticles.find(x=>x.articleName == articleName && x.articleModel == model);
- if(article != undefined){
- article.quantity += quantity;
- return;
- }
- articleModel = articleModel.toLowerCase();
- this.listOfArticles.push({articleModel, articleName, quantity});
- return `Successfully added article ${articleName} with a new quantity- ${quantity}.`;
- }
- inviteGuest ( guestName, personality){
- let guest = this.guests.find(x=>x.guestName == guestName);
- if(guest != undefined){
- throw new Error(`${guestName} has already been invited.`);
- }
- let points = 0;
- if(personality == 'Vip'){
- points = 500;
- } else if(personality == 'Middle'){
- points = 250;
- } else {
- points = 50;
- }
- this.guests.push({guestName, points, purchaseArticle: 0});
- return `You have successfully invited ${guestName}!`;
- }
- buyArticle ( articleModel, articleName, guestName){
- let artModel = this.listOfArticles.find(x=>x.articleModel == articleModel && x.articleName == articleName);
- if(artModel == undefined){
- throw new Error("This article is not found.")
- }
- if(artModel.quantity == 0){
- return `The ${articleName} is not available.`;
- }
- let guest = this.guests.find(x=>x.guestName == guestName);
- if(guest == undefined){
- return `This guest is not invited.`;
- }
- if(guest.points >= this.possibleArticles[articleModel]){
- guest.points -= this.possibleArticles[articleModel];
- artModel.quantity -= 1;
- guest.purchaseArticle += 1;
- } else {
- return `You need to more points to purchase the article.`;
- }
- return `${guestName} successfully purchased the article worth ${this.possibleArticles[articleModel]} points.`;
- }
- showGalleryInfo (criteria){
- let result = [];
- if(criteria == 'guest'){
- result.push("Guests information:");
- this.guests.forEach(x=> result.push(`${x.guestName} - ${x.purchaseArticle}`));
- } else if(criteria == 'article'){
- result.push("Articles information:");
- this.listOfArticles.forEach(x=>result.push(`${x.articleModel} - ${x.articleName} - ${x.quantity}`));
- }
- return result.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement