diff --git a/drugLogics.js b/drugLogics.js new file mode 100644 index 00000000..cd1afb86 --- /dev/null +++ b/drugLogics.js @@ -0,0 +1,101 @@ +/** + * Used to increase the benefit value according to general drug rules + * @param {int} currentBenefitValue the benefit value before the increase + * @param {int} increaseValue the number used to increase + * @returns the increased benefit + */ +function increaseBenefit(currentBenefitValue, increaseValue) { + let increasedBenefit = currentBenefitValue + increaseValue; + if (increasedBenefit > 50) { + increasedBenefit = 50; + } + return increasedBenefit; +} + +/** + * Used to decrease the benefit value according to general drug rules + * @param {int} currentBenefitValue the benefit value before the decrease + * @param {int} decreaseValue the number used to decrease + * @returns the decreased benefit + */ +function decreaseBenefit(currentBenefitValue, decreaseValue) { + let decreasedBenefit = currentBenefitValue - decreaseValue; + if (decreasedBenefit < 0) { + decreasedBenefit = 0; + } + return decreasedBenefit; +} +/** + * Decrease the ExpiresIn value + * @param {int} currentExpiresInValue the ExpiresIn value to decrease + * @param {int} decreaseValue the number used to decrease + * @returns the decreased ExpiresIn + */ +function decreaseExpiresIn(currentExpiresInValue, decreaseValue) { + return currentExpiresInValue - decreaseValue; +} + +/** + * Used to know if a drug is expired or not + * @param {*} expiresIn the number of days left before expiration + * @returns true if the drug is expired + */ +function isExpired(expiresIn) { + return expiresIn <= 0; +} + +function herbalTeaLogic(expiresIn, benefit) { + if (isExpired(expiresIn)) { + benefit = increaseBenefit(benefit, 2); + } else { + benefit = increaseBenefit(benefit, 1); + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + +function magicPillLogic(expiresIn, benefit) { + return [expiresIn, benefit]; +} + +function fervexLogic(expiresIn, benefit) { + if (expiresIn > 10) { + return drugLogic(expiresIn, benefit); + } else if (expiresIn <= 10 && expiresIn > 5) { + benefit = increaseBenefit(benefit, 2); + } else if (expiresIn <= 5 && expiresIn > 0) { + benefit = increaseBenefit(benefit, 3); + } else { + benefit = 0; + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + +function dafalganLogic(expiresIn, benefit) { + if (isExpired(expiresIn)) { + benefit = decreaseBenefit(benefit, 4); + } else { + benefit = decreaseBenefit(benefit, 2); + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + +function drugLogic(expiresIn, benefit) { + if (isExpired(expiresIn)) { + benefit = decreaseBenefit(benefit, 2); + } else { + benefit = decreaseBenefit(benefit, 1); + } + return [decreaseExpiresIn(expiresIn, 1), benefit]; +} + +export { + herbalTeaLogic, + magicPillLogic, + fervexLogic, + dafalganLogic, + drugLogic, + increaseBenefit, + decreaseBenefit, + decreaseExpiresIn, + isExpired +}; diff --git a/drugLogics.test.js b/drugLogics.test.js new file mode 100644 index 00000000..99a19904 --- /dev/null +++ b/drugLogics.test.js @@ -0,0 +1,99 @@ +import { + isExpired, + increaseBenefit, + decreaseBenefit, + magicPillLogic, + herbalTeaLogic, + fervexLogic, + drugLogic, + dafalganLogic, + decreaseExpiresIn +} from "./drugLogics"; + +describe("Usual methods", () => { + it("should return false", () => { + expect(isExpired(1)).toEqual(false); + }); + + it("should return true", () => { + expect(isExpired(-1)).toEqual(true); + }); + + it("should increase value by one", () => { + expect(increaseBenefit(10, 1)).toEqual(11); + }); + + it("should not increase more than 50", () => { + expect(increaseBenefit(10, 100)).toEqual(50); + }); + + it("should decrease value by 2", () => { + expect(decreaseBenefit(10, 2)).toEqual(8); + }); + + it("should not decrease more than 0", () => { + expect(decreaseBenefit(10, 100)).toEqual(0); + }); + + it("should decrease by one", () => { + expect(decreaseExpiresIn(10, 1)).toEqual(9); + }); +}); + +describe("Logic methods - Magic Pill", () => { + it("should not change benefit", () => { + expect(magicPillLogic(10, 20)).toEqual([10, 20]); + }); +}); + +describe("Logic methods - Herbal Tea", () => { + it("should increase benefit by one", () => { + expect(herbalTeaLogic(10, 20)).toEqual([9, 21]); + }); + + it("should increase benefit by two ", () => { + expect(herbalTeaLogic(-1, 20)).toEqual([-2, 22]); + }); + + it("should not increase benefit more than fifty ", () => { + expect(herbalTeaLogic(-1, 49)).toEqual([-2, 50]); + }); +}); + +describe("Logic methods - Fervex Pill", () => { + it("should decrease benefit by one", () => { + expect(fervexLogic(15, 10)).toEqual([14, 9]); + }); + + it("should increase benefit by two", () => { + expect(fervexLogic(10, 10)).toEqual([9, 12]); + }); + + it("should increase benefit by three", () => { + expect(fervexLogic(5, 10)).toEqual([4, 13]); + }); + + it("should decrease benefit to zero", () => { + expect(fervexLogic(0, 10)).toEqual([-1, 0]); + }); +}); + +describe("Logic methods - Drug", () => { + it("should decrease benefit by one", () => { + expect(drugLogic(5, 10)).toEqual([4, 9]); + }); + + it("should decrease benefit by two", () => { + expect(drugLogic(0, 10)).toEqual([-1, 8]); + }); +}); + +describe("Logic methods - Dafalgan", () => { + it("should decrease benefit by two", () => { + expect(dafalganLogic(10, 10)).toEqual([9, 8]); + }); + + it("should decrease benefit by four", () => { + expect(dafalganLogic(0, 10)).toEqual([-1, 6]); + }); +}); diff --git a/pharmacy.js b/pharmacy.js index cda44c41..57a5c599 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -1,3 +1,5 @@ +import { herbalTeaLogic, magicPillLogic, fervexLogic, dafalganLogic, drugLogic } from "./drugLogics"; + export class Drug { constructor(name, expiresIn, benefit) { this.name = name; @@ -11,56 +13,28 @@ 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; - } - } - } - } - } - 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; - } - } else { - if (this.drugs[i].benefit < 50) { - this.drugs[i].benefit = this.drugs[i].benefit + 1; - } - } + for (let i = 0; i < this.drugs.length; i++) { + let resultLogic; + switch (this.drugs[i].name) { + case "Herbal Tea": + resultLogic = herbalTeaLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; + case "Magic Pill": + resultLogic = magicPillLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; + case "Fervex": + resultLogic = fervexLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; + case "Dafalgan": + resultLogic = dafalganLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; + default: + resultLogic = drugLogic(this.drugs[i].expiresIn, this.drugs[i].benefit); + break; } + this.drugs[i].expiresIn = resultLogic[0]; + this.drugs[i].benefit = resultLogic[1]; } - return this.drugs; } } diff --git a/pharmacy.test.js b/pharmacy.test.js index f0925fc1..94bdd945 100644 --- a/pharmacy.test.js +++ b/pharmacy.test.js @@ -1,9 +1,101 @@ import { Drug, Pharmacy } from "./pharmacy"; -describe("Pharmacy", () => { +describe("Pharmacy (Drug)", () => { it("should decrease the benefit and expiresIn", () => { expect(new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()).toEqual( [new Drug("test", 1, 2)] ); }); + + it("should not decrease the benefit and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("test", 0, 0)]).updateBenefitValue()).toEqual( + [new Drug("test", -1, 0)] + ); + }); + + it("should decrease the benefit twice as fast and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("test", 0, 10)]).updateBenefitValue()).toEqual( + [new Drug("test", -1, 8)] + ); + }); + + it("should decrease the benefit and expiresIn for both drugs", () => { + expect(new Pharmacy([new Drug("test", 2, 3), new Drug("test", 3, 4)]).updateBenefitValue()).toEqual( + [new Drug("test", 1, 2), new Drug("test", 2, 3)] + ); + }); +}) + +describe("Pharmacy (Herbal Tea)", () => { + it("should increase the benefit and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 5, 3)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", 4, 4)] + ); + }); + + it("should increase the benefit twice as fast and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 0, 3)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", -1, 5)] + ); + }); + + it("should increase the benefit to 50 and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Herbal Tea", 0, 49)]).updateBenefitValue()).toEqual( + [new Drug("Herbal Tea", -1, 50)] + ); + }); +}) + +describe("Pharmacy (Magic Pill)", () => { + it("should not change benefit and expiresIn (Magic Pill)", () => { + expect(new Pharmacy([new Drug("Magic Pill", 10, 40)]).updateBenefitValue()).toEqual( + [new Drug("Magic Pill", 10, 40)] + ); + }); +}) + +describe("Pharmacy (Fervex)", () => { + it("should decrease the benefit and expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 20, 10)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 19, 9)] + ); + }); + + it("should increase the benefit by two and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 10, 10)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 9, 12)] + ); + }); + + it("should increase the benefit by three and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 4, 10)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 3, 13)] + ); + }); + + it("should decrease the benefit to zero and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 0, 25)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", -1, 0)] + ); + }); + + it("should increase the benefit to 50 and decrease expiresIn", () => { + expect(new Pharmacy([new Drug("Fervex", 3, 49)]).updateBenefitValue()).toEqual( + [new Drug("Fervex", 2, 50)] + ); + }); }); + +describe("Pharmacy (Dafalgan)", () => { + it("should decrease benefit by two and expiresIn (Dafalgan)", () => { + expect(new Pharmacy([new Drug("Dafalgan", 10, 40)]).updateBenefitValue()).toEqual( + [new Drug("Dafalgan", 9, 38)] + ); + }); + + it("should decrease benefit by four and expiresIn (Dafalgan)", () => { + expect(new Pharmacy([new Drug("Dafalgan", 0, 40)]).updateBenefitValue()).toEqual( + [new Drug("Dafalgan", -1, 36)] + ); + }); +})