-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook-function.js
90 lines (83 loc) · 2.15 KB
/
book-function.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
/* 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;
}
function Author(name, email, gender) {
this._name = name;
this.email = email;
this.gender = gender;
Object.defineProperty(this, 'name', {
get() {
return capitalize(this._name);
},
set(value) {
const finalValue = value.trim();
if (finalValue.length < 2) {
throw new Error('Authors must have longer names');
}
this._name = value.trim();
},
});
}
Author.prototype.toString = function toString() {
return `${this.name} is a ${this.gender} autor \nContacts: ${this.email}`;
};
function Book(title, author, price, quantity) {
this._title = title;
this._author = author;
this.price = price;
this.quantity = quantity;
Object.defineProperty(this, 'title', {
get() {
return capitalize(this._title);
},
set(value) {
const finalValue = value.trim();
if (finalValue === '') {
throw new Error('Authors must have longer names');
}
this._title = value;
},
});
Object.defineProperty(this, 'author', {
get() {
return this._author.name;
},
set(value) {
const comparingValue = value;
if (typeof comparingValue === 'string') {
throw new Error("We don't know this author");
}
this._author = value;
},
});
}
Book.prototype.getProfit = function getProfit() {
const profit = String(this.price * this.quantity);
const finalanswer = displaywithSpaces(profit);
return finalanswer;
};
Book.prototype.toStirng = function toStirng() {
return `${this.title} was written by ${
this.author
}\nwho is going to have $${this.getProfit()}`;
};