Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Handle new Dafalgan supplier #199

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
106 changes: 60 additions & 46 deletions pharmacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,70 @@ export class Pharmacy {
this.drugs = drugs;
}
updateBenefitValue() {
for (var i = 0; i < this.drugs.length; i++) {
if (
this.drugs[i].name != "Herbal Tea" &&
this.drugs[i].name != "Fervex"
) {
if (this.drugs[i].benefit > 0) {
if (this.drugs[i].name != "Magic Pill") {
this.drugs[i].benefit = this.drugs[i].benefit - 1;
}
}
} else {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
if (this.drugs[i].name == "Fervex") {
if (this.drugs[i].expiresIn < 11) {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
}
}
if (this.drugs[i].expiresIn < 6) {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
}
}
}
}
return this.drugs.map(drug => {
if (drug.name === "Magic Pill") {
drug.benefit = this.checkBenefitRange(drug.benefit);
return drug;
}
if (this.drugs[i].name != "Magic Pill") {
this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1;
}
if (this.drugs[i].expiresIn < 0) {
if (this.drugs[i].name != "Herbal Tea") {
if (this.drugs[i].name != "Fervex") {
if (this.drugs[i].benefit > 0) {
if (this.drugs[i].name != "Magic Pill") {
this.drugs[i].benefit = this.drugs[i].benefit - 1;
}
}
} else {
this.drugs[i].benefit =
this.drugs[i].benefit - this.drugs[i].benefit;
}

drug.expiresIn = this.computeExpirationDate(drug);
drug.benefit += this.computeBenefit(drug);

drug.benefit = this.checkBenefitRange(drug.benefit);

return drug;
});
}

/**
*
* @param {Drug} drug
* @returns {int} The new expiresIn value
*/
computeExpirationDate(drug) {
return drug.name === "Magic Pill" ? drug.expiresIn : drug.expiresIn - 1;
}

/**
*
* @param {Drug} drug
* @returns {int} The value to impact on drug's benefit
*/
computeBenefit(drug) {
let impactValue;

switch (drug.name) {
case "Dafalgan":
impactValue = drug.expiresIn >= 0 ? -2 : -4;
break;
case "Herbal Tea":
impactValue = drug.expiresIn >= 0 ? 1 : 2;
break;
case "Fervex":
if (drug.expiresIn < 0) {
// Drops to 0 when expired
impactValue = -Math.abs(drug.benefit);
} else if (drug.expiresIn <= 5) {
impactValue = 3;
} else if (drug.expiresIn <= 10) {
impactValue = 2;
} else {
if (this.drugs[i].benefit < 50) {
this.drugs[i].benefit = this.drugs[i].benefit + 1;
}
impactValue = 1;
}
}
break;
default:
impactValue = drug.expiresIn >= 0 ? -1 : -2;
break;
}
return impactValue;
}

return this.drugs;
/**
* Normalize a benefit value so that it is not out of range
* @param {int} benefit Then benefit to check
* @returns {int} the computed value in range [0-50]
*/
checkBenefitRange(benefit) {
return Math.min(Math.max(0, benefit), 50);
}
}
75 changes: 75 additions & 0 deletions pharmacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,79 @@ describe("Pharmacy", () => {
[new Drug("test", 1, 2)]
);
});

it("should not result to a negative benefit", () => {
const result = new Pharmacy([new Drug("test", 2, 0)]).updateBenefitValue();

expect(result).toEqual([new Drug("test", 1, 0)]);
});

it("shoud not result to a benefit > 50", () => {
const result = new Pharmacy([new Drug("test", 2, 55)]).updateBenefitValue();

expect(result).toEqual([new Drug("test", 1, 50)]);
});

it("should decrease by 2 when expiration date has passed", () => {
const result = new Pharmacy([new Drug("test", 0, 10)]).updateBenefitValue();

expect(result).toEqual([new Drug("test", -1, 8)]);
});

describe("Herbal Tea", () => {
it("should increase in benefit", () => {
const result = new Pharmacy([new Drug("Herbal Tea", 2, 10)]).updateBenefitValue();

expect(result).toEqual([new Drug("Herbal Tea", 1, 11)]);
});

it("should increase in benefit by 2 when expiration date has passed", () => {
const result = new Pharmacy([new Drug("Herbal Tea", 0, 10)]).updateBenefitValue();

expect(result).toEqual([new Drug("Herbal Tea", -1, 12)]);
});
});

describe("Magic Pill", () => {
it("should not expire nor decrease in benefit", () => {
const drugs = [new Drug("Magic Pill", 1, 10)];
const result = new Pharmacy(drugs).updateBenefitValue();

expect(result).toEqual(drugs);
});
});

describe("Fervex", () => {
it("should increase in benefit by 2 when expiration date <= 10", () => {
const result = new Pharmacy([new Drug("Fervex", 7, 5)]).updateBenefitValue();

expect(result).toEqual([new Drug("Fervex", 6, 7)]);
});

it("should increase in benefit by 3 when expiration date <= 5", () => {
const result = new Pharmacy([new Drug("Fervex", 3, 5)]).updateBenefitValue();

expect(result).toEqual([new Drug("Fervex", 2, 8)]);
});

it("should drop benefit to 0 when expiration date has passed", () => {
const result = new Pharmacy([new Drug("Fervex", 0, 8)]).updateBenefitValue();

expect(result).toEqual([new Drug("Fervex", -1, 0)]);
});
});

describe("Dafalgan", () => {
it("should decrease in benefit by 2", () => {
const result = new Pharmacy([new Drug("Dafalgan", 10, 10)]).updateBenefitValue();

expect(result).toEqual([new Drug("Dafalgan", 9, 8)]);
});

it("should decrease in benefit by 4 when expiration date has passed", () => {
const result = new Pharmacy([new Drug("Dafalgan", 0, 10)]).updateBenefitValue();

expect(result).toEqual([new Drug("Dafalgan", -1, 6)]);
});
});
});