-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook-class.js
102 lines (87 loc) · 2.28 KB
/
book-class.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* eslint-disable max-classes-per-file */
/* eslint-disable no-underscore-dangle */
// to display names adn titles with capitalized
function capitalize(val) {
let str = val;
str = str[0].toUpperCase() + str.slice(1).toLowerCase();
return str.trim();
}
// to display numbers with space for better readability
function displaywithSpaces(val) {
let str = String(val);
let final = '';
const res = [];
while (str.length > 3) {
res.push(str.substr(str.length - 3));
str = str.substr(0, str.length - 3);
}
final = str;
for (const item of res) {
final += ` ${item}`;
}
return final;
}
class Author {
constructor(name, email, gender) {
this._name = name;
this.email = email;
this.gender = gender;
}
get name() {
return capitalize(this._name);
}
set name(value) {
const finalValue = value.trim();
if (finalValue.length < 2) {
throw new Error('Authors must have longer names');
}
this._name = value;
}
toString() {
return `${this.name} is a ${this.gender} autor \nContacts: ${this.email}`;
}
}
class Book {
constructor(title, author, price, quantity) {
this._title = title;
this._author = author;
this.price = price;
this.quantity = quantity;
}
get title() {
return capitalize(this._title);
}
set title(value) {
const finalValue = value.trim();
if (finalValue === '') {
throw new Error('Authors must have longer names');
}
this._title = value;
}
get author() {
return this._author.name;
}
set author(value) {
const comparingValue = value;
if (typeof comparingValue === 'string') {
throw new Error("We don't know this author");
}
this._author = value;
}
getProfit() {
const profit = String(this.price * this.quantity);
const finalanswer = displaywithSpaces(profit);
return finalanswer;
}
toStirng() {
return `${this.title} was written by ${
this.author
}\nwho is going to have $${this.getProfit()}`;
}
}
const Tsitsernak = new Author('Tsitsernak', 'armen@mail.com', 'male');
const shikahav = new Author('shikahav', 'arpen@mail.com', 'female');
const swallow = new Book('swallow', Tsitsernak, 200, 1000);
const robin = new Book('robin', shikahav, 10, 3000);
console.log(swallow.toStirng());
console.log(robin.toStirng());