-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2-abstract.ts
More file actions
55 lines (44 loc) · 1.18 KB
/
2-abstract.ts
File metadata and controls
55 lines (44 loc) · 1.18 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
abstract class AbstractProduct {
name: string;
price: number;
}
class Product extends AbstractProduct {
name: string;
productPrice: number;
constructor(name: string, price: number) {
super();
this.name = name;
this.productPrice = price;
}
get price() {
return this.productPrice;
}
}
class Collection extends AbstractProduct {
name: string;
collection: Set<AbstractProduct>;
constructor(name: string, ...products: AbstractProduct[]) {
super();
this.name = name;
this.collection = new Set(products);
}
get price() {
let price = 0;
for (const item of this.collection) {
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}`);