forked from inato/take-home-test
-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathpharmacy.js
More file actions
49 lines (42 loc) · 1.03 KB
/
Copy pathpharmacy.js
File metadata and controls
49 lines (42 loc) · 1.03 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
import { updateDrugs } from "./lib/drugsUpdater";
export const MIN_BENEFIT = 0;
export const MAX_BENEFIT = 50;
export const ERROR_DRUG_INSTANTIATION =
"Error while creating a new drug: Bad params";
export class Drug {
constructor(name, expiresIn, benefit) {
if (
typeof name !== "string" ||
typeof expiresIn !== "number" ||
typeof benefit !== "number" ||
benefit < MIN_BENEFIT ||
benefit > MAX_BENEFIT
) {
throw new Error(ERROR_DRUG_INSTANTIATION);
}
this.name = name;
this.expiresIn = expiresIn;
this.benefit = benefit;
}
dicreaseExpiresInByOne() {
this.expiresIn -= 1;
}
dicreaseBenefitBy(n) {
this.benefit = Math.max(this.benefit - n, MIN_BENEFIT);
}
increaseBenefitBy(n) {
this.benefit = Math.min(this.benefit + n, MAX_BENEFIT);
}
setBenefitToZero() {
this.benefit = 0;
}
}
export class Pharmacy {
constructor(drugs = []) {
this.drugs = drugs;
}
updateBenefitValue() {
this.drugs.forEach(updateDrugs);
return this.drugs;
}
}