Advertisement
kstoyanov

02. Press House Exam may 2020

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