-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path7-3-Order.js
77 lines (68 loc) Β· 1.49 KB
/
7-3-Order.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
74
75
76
77
/**
* p252 μμ
*/
class Order {
#priority;
constructor(data) {
this.#priority = new Priority(data.priority);
}
get priority() {
return this.#priority;
}
get priorityString() {
return this.#priority.toString();
}
set priority(aString) {
this.#priority = new Priority(aString);
}
}
class Priority {
#value;
constructor(value) {
if (value instanceof Priority) return value;
if (Priority.legalValues().includes(value)) {
this.#value = value;
} else {
throw new Error(`<${value}> is invalid for Priority`);
}
this.#value = value;
}
toString() {
return this.#value;
}
get #index() {
return Priority.legalValues().findIndex((s) => s === this.#value);
}
static legalValues() {
return ['low', 'normal', 'high', 'rush'];
}
equals(other) {
return this.#index === other.#index;
}
higherThan(other) {
return this.#index > other.#index;
}
lowerThan(other) {
return this.#index < other.#index;
}
}
/**
* μμ μ€νμ μν μμμ μ½λ
*/
const orders = [
new Order({ id: 1, priority: 'low' }),
new Order({ id: 2, priority: 'normal' }),
new Order({ id: 3, priority: 'high' }),
new Order({ id: 4, priority: 'rush' }),
];
let highPriorityCount = 0;
/**
* μμ μ½λ μ¬μ©
*/
highPriorityCount = orders.filter((o) =>
o.priority.higherThan(new Priority('normal'))
).length;
for (const order of orders) {
console.log(order.priority.toString());
}
console.log(highPriorityCount);