krasizorbov

Press House

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