-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2-theory.js
More file actions
59 lines (48 loc) · 1.26 KB
/
2-theory.js
File metadata and controls
59 lines (48 loc) · 1.26 KB
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
'use strict';
class AbstractProduct {
constructor() {
const proto = Object.getPrototypeOf(this);
if (proto.constructor === AbstractProduct) {
throw new Error('Abstract class should not be instanciated');
}
}
get price() {
throw new Error('Method is not implemented');
}
}
class Product extends AbstractProduct {
constructor(name, price) {
super();
this.name = name;
this.productPrice = price;
}
get price() {
return this.productPrice;
}
}
class Collection extends AbstractProduct {
constructor(name, ...products) {
super();
this.name = name;
this.set = new Set(products);
}
get price() {
let price = 0;
for (const item of this.set) {
price += item.price;
}
return price;
}
}
// Usage
const p1 = new Product('Laptop', 1500);
const p2 = new Product('Mouse', 25);
const p3 = new Product('Keyboard', 100);
const p4 = new Product('HDMI cable', 10);
const electronics = new Collection('Electronics', p1, p2, p3, p4);
const p5 = new Product('Bag', 50);
const p6 = new Product('Mouse pad', 5);
const textile = new Collection('Textile', p5, p6);
const purchase = new Collection('Purchase', electronics, textile);
console.dir(purchase, { depth: null });
console.log(`Total is ${purchase.price}`);