View difference between Paste ID: d3Sp3pNR and NSf5cxX8
SHOW: | | - or go back to the newest paste.
1
function solve() {
2
    class Article {
3
        constructor(title, content) {
4
            this.title = title;
5
            this.content = content;
6
        }
7
8
        toString() {
9
            return `Title: ${this.title}\nContent: ${this.content}`;
10
        }
11
    }
12
13
    class ShortReports extends Article {
14
        constructor(title, content, originalResearch) {
15
            if (content.length >= 150) {
16
                throw new Error('Short reports content should be less then 150 symbols.');
17
            }
18
19
            if (!originalResearch.hasOwnProperty('title') || !originalResearch.hasOwnProperty('author')) {
20
                throw new Error('The original research should have author and title.');
21
            }  
22
            super(title,content);
23
            this.comments = [];
24
            this.originalResearch = originalResearch;
25
        }
26
27
        addComment(comment) {
28
            this.comments.push(comment);
29
            return 'The comment is added.';
30
        }
31
32
        toString() {
33
            let output = super.toString();
34
            output += `\nOriginal Research: ${this.originalResearch.title} by ${this.originalResearch.author}`;
35
36
            if (this.comments.length > 0) {
37
                output += '\nComments:\n'
38
                output += this.comments.join('\n');
39
            }
40
41
            return output;
42
        }
43
    }
44
45
    class BookReview extends Article {
46
        constructor(title, content, book) {
47
            super(title,content);
48
            this.book = book;
49
            this.clients = [];
50
        }
51
52
        addClient(clientName, orderDescription) {
53
           let hasClient =  this.clients.find(client => client.name === clientName 
54
                                            && client.description === orderDescription);
55
            
56
            if (hasClient) {
57
                throw new Error('This client has already ordered this review.');
58
            }
59
60
            this.clients.push({name:clientName,description:orderDescription});
61
            return `${clientName} has ordered a review for ${orderDescription}`;
62
        }
63
64
        toString() {
65
            let output = super.toString() +'\n';
66
            output += `Book: ${this.book.name}`;
67
            if (this.clients.length > 0) {
68
                output += '\nOrders:\n';
69
                output += this.clients.map(client => `${client.name} - ${client.description}`).join('\n');
70
            }
71
72
            return output;
73
        }
74
    }
75
76
    return {Article, ShortReports, BookReview};
77
}
78
79
let classes = solve();
80
let book = new classes.BookReview("The Great Gatsby is so much more than a love story", "The Great Gatsby is in many ways similar to Romeo and Juliet, yet I believe that it is so much more than just a love story. It is also a reflection on the hollowness of a life of leisure. ...", { name: "The Great Gatsby", author: "F Scott Fitzgerald" });
81
console.log(book.addClient("The Guardian", "100 symbols"));
82
console.log(book.addClient("Goodreads", "30 symbols"));
83
console.log(book.toString());
84