-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.4_ReplaceConditionalWithPolymorphism_refactring.ts
More file actions
87 lines (73 loc) · 2 KB
/
Copy path10.4_ReplaceConditionalWithPolymorphism_refactring.ts
File metadata and controls
87 lines (73 loc) · 2 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
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
/**
* 第一步:函数组合成类
*/
function rating(voyage, history) {
return createRating(voyage, history).value;
}
class Rating {
_voyage;
_history;
constructor(voyage, history) {
this._voyage = voyage;
this._history = history;
}
get value() {
const vpf = this.voyageProfitFactor;
const vr = this.voyageRisk;
const chr = this.captainHistoryRisk;
if (vpf * 3 > vr + chr * 2) return 'A';
else return 'B';
}
get voyageRisk() {
let result = 1;
if (this._voyage.length > 4) result += 4;
if (this._voyage.length > 8) result += this._voyage.length - 8;
if (this.isSpecialZone) result += 4;
return Math.max(result, 0);
}
get captainHistoryRisk() {
let result = 1;
if (this._history.length < 5) result += 4;
result += this._history.filter((v) => v.profit < 0).length;
return Math.max(result, 0);
}
get voyageProfitFactor() {
let result = 2;
if (this.isSpecialZone) result += 1;
result += this.historyLengthFactor;
result += this.voyageLengthFactor;
return result;
}
get voyageLengthFactor(): number{
return this._voyage.length > 14 ? -1 : 0;
}
get historyLengthFactor() {
return this._history.length > 8 ? 1 : 0;
}
get isSpecialZone() {
return ['china', 'east-indies'].includes(this._voyage.zone);
}
}
class ExperiencedChinaRating extends Rating {
get captainHistoryRisk() {
const result = super.captainHistoryRisk - 2;
return Math.max(result, 0);
}
get voyageLengthFactor() {
let result = 0;
if (this._voyage.length > 12) result += 1;
if (this._voyage.length > 18) result -= 1;
return result;
}
get historyLengthFactor() {
return this._history.length > 10 ? 1 : 0;
}
get voyageProfitFactor() {
return super.voyageProfitFactor + 3;
}
}
function createRating(voyage, history) {
if (voyage.zone === 'china' && history.some((v) => 'china' === v.zone))
return new ExperiencedChinaRating(voyage, history);
else return new Rating(voyage, history);
}