-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain of resp.js
73 lines (56 loc) · 1.01 KB
/
chain of resp.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
class ShoppingCart {
constructor() {
this.products = [];
}
addProduct(p) {
this.products.push(p);
};
}
class Discount {
calc(products) {
let ndiscount = new NumberDiscount();
let pdiscount = new PriceDiscount();
let none = new NoneDiscount();
ndiscount.setNext(pdiscount);
pdiscount.setNext(none);
return ndiscount.exec(products);
};
}
class NumberDiscount {
constructor() {
this.next = null;
}
setNext(fn) {
this.next = fn;
};
exec(products) {
let result = 0;
if (products.length > 3)
result = 0.05;
return result + this.next.exec(products);
};
}
class PriceDiscount {
constructor() {
this.next = null;
}
setNext(fn) {
this.next = fn;
};
exec(products) {
let result = 0;
let total = products.reduce((a, b) => a + b);
if (total >= 500)
result = 0.1;
return result + this.next.exec(products);
};
}
class NoneDiscount {
exec() {
return 0;
};
}
export {
ShoppingCart,
Discount
};