krasizorbov

Press House

Oct 16th, 2020
2,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function pressHouse() {
  2.  
  3.     class Article {
  4.         constructor(title, content) {
  5.             this.title = title;
  6.             this.content = content;
  7.         }
  8.  
  9.         toString() {
  10.             return `Title: ${this.title}\nContent: ${this.content}`
  11.         }
  12.     }
  13.  
  14.     class ShortReports extends Article {
  15.         constructor(title, content, originalResearch) {
  16.             super(title, content);
  17.             this.originalResearch = originalResearch;
  18.             this.comments = [];
  19.  
  20.             if (content.length > 150) {
  21.                 throw Error("Short reports content should be less then 150 symbols.")
  22.             }
  23.  
  24.             if (!originalResearch.author || !originalResearch.title) {
  25.                 throw Error("The original research should have author and title.")
  26.             }
  27.         }
  28.  
  29.         addComment(comment) {
  30.             this.comments.push(comment);
  31.             return "The comment is added."
  32.         }
  33.  
  34.         toString() {
  35.         let result = [
  36.         super.toString(),
  37.         `Original Research: ${this.originalResearch.title} by ${this.originalResearch.author}`,
  38.             ];
  39.         if (this.comments.length > 0) {
  40.         result.push(`Comments:`);
  41.         this.comments.forEach((c) => result.push(c));
  42.       }
  43.       return result.join("\n");
  44.     }
  45.     }
  46.  
  47.     class BookReview extends Article {
  48.         constructor(title, content, book) {
  49.             super(title, content);
  50.             this.book = book;
  51.             this.clients = []
  52.         }
  53.  
  54.         addClient(clientName, orderDescription) {
  55.             const clientPresent = this.clients.find((x) => { return x.clientName === clientName });
  56.             if (clientPresent) {
  57.                 throw Error('This client has already ordered this review.')
  58.             }
  59.  
  60.             this.clients.push({ clientName, orderDescription });
  61.  
  62.             return `${clientName} has ordered a review for ${this.book.name}`
  63.         }
  64.  
  65.         toString() {
  66.         let result = [super.toString()];
  67.         result.push(`Book: ${this.book.name}`);
  68.         if (this.clients.length > 0) {
  69.         result.push(`Orders:`);
  70.         this.clients.forEach((c) =>
  71.           result.push(`${c.clientName} - ${c.orderDescription}`)
  72.         );
  73.       }
  74.       return result.join("\n");
  75.     }
  76.     }
  77.  
  78.  
  79.     return {
  80.         Article,
  81.         ShortReports,
  82.         BookReview
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment