-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10.4_ReplaceConditionalWithPolymorphism.ts
More file actions
77 lines (71 loc) · 1.75 KB
/
10.4_ReplaceConditionalWithPolymorphism.ts
File metadata and controls
77 lines (71 loc) · 1.75 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
/**
* Replace Conditional with Polymorphism
* 以多态取代条件表达式
*/
/**
*
* 使用继承的一种情况是:我想表达某个对象与另一个对象大体类似,但又有一些不同之处。
*
*/
/**
* 评分
* @param voyage
* @param history
* @returns
*/
function _rating(voyage, history) {
const vpf = voyageProfitFactor(voyage, history);
const vr = voyageRisk(voyage);
const chr = captainHistoryRisk(voyage, history);
if (vpf * 3 > vr + chr * 2) return 'A';
else return 'B';
}
/**
* 航海风险
* @param voyage
* @returns
*/
function voyageRisk(voyage) {
let result = 1;
if (voyage.length > 4) result += 4;
if (voyage.length > 8) result += voyage.length - 8;
if (['china', 'east-indies'].includes(voyage.zone)) result += 4;
return Math.max(result, 0);
}
/**
* 船长历史风险
* @param voyage 航海
* @param history
* @returns
*/
function captainHistoryRisk(voyage, history) {
let result = 1;
if (history.length < 5) result += 4;
result += history.filter((v) => v.profit < 0).length;
if (voyage.zone === 'china' && hasChina(history)) result -= 2;
return Math.max(result, 0);
}
function hasChina(history) {
return history.some((v) => 'china' === v.zone);
}
/**
* 打出 盈利潜力 分数
* @param voyage
* @param history
* @returns
*/
function voyageProfitFactor(voyage, history) {
let result = 2;
if (voyage.zone === 'china') result += 1;
if (voyage.zone === 'east-indies') result += 1;
if (voyage.zone === 'china' && hasChina(history)) {
result += 3;
if (history.length > 10) result += 1;
if (voyage.length > 12) result += 1;
if (voyage.length > 18) result -= 1;
} else {
if (history.length > 8) result += 1;
if (voyage.length > 14) result -= 1;
}
return result;
}