diff --git a/pharmacy.js b/pharmacy.js index cda44c41..f14bfaf4 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -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); } } diff --git a/pharmacy.test.js b/pharmacy.test.js index f0925fc1..3a165fb9 100644 --- a/pharmacy.test.js +++ b/pharmacy.test.js @@ -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)]); + }); + }); });