From 7caffe10c82aef6b42c6fb86e0701df57a0f0400 Mon Sep 17 00:00:00 2001 From: Thomas Bodin Date: Sat, 29 Jan 2022 11:24:30 +0100 Subject: [PATCH 1/4] chore(gitignore): Add .idea folder --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b512c09d..cd981053 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +.idea/ \ No newline at end of file From 37e3dc353bc57484c9e1f7e37fc3a32a7531cbb2 Mon Sep 17 00:00:00 2001 From: Thomas Bodin Date: Sat, 29 Jan 2022 11:26:00 +0100 Subject: [PATCH 2/4] feat(pharmacy): Change rules and add Dafalgan drug --- index.js | 10 ++++-- pharmacy.js | 100 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 65 insertions(+), 45 deletions(-) diff --git a/index.js b/index.js index e5ea0074..4952ca0e 100644 --- a/index.js +++ b/index.js @@ -4,9 +4,13 @@ import fs from "fs"; const drugs = [ new Drug("Doliprane", 20, 30), - new Drug("Herbal Tea", 10, 5), - new Drug("Fervex", 5, 40), - new Drug("Magic Pill", 15, 40) + new Drug("Herbal Tea", 10, 5, "increase"), + new Drug("Fervex", 5, 40, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]), + new Drug("Magic Pill", 15, 40, null, 1, false), + new Drug("Dafalgan", 20, 30, "decrease", 2) ]; const trial = new Pharmacy(drugs); diff --git a/pharmacy.js b/pharmacy.js index cda44c41..4c2d0c71 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -1,8 +1,22 @@ export class Drug { - constructor(name, expiresIn, benefit) { + constructor( + name, + expiresIn, + benefit, + benefitEffect = "decrease", + benefitMultiplier = 1, + hasExpirationDate = true, + hasBenefitAfterExpiration = true, + benefitMultipliersByRemainingDay = [] + ) { this.name = name; this.expiresIn = expiresIn; this.benefit = benefit; + this.benefitEffect = benefitEffect; + this.benefitMultiplier = benefitMultiplier; + this.hasExpirationDate = hasExpirationDate; + this.hasBenefitAfterExpiration = hasBenefitAfterExpiration; + this.benefitMultipliersByRemainingDay = benefitMultipliersByRemainingDay; } } @@ -10,56 +24,58 @@ export class Pharmacy { constructor(drugs = []) { 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; + this.drugs.forEach(drug => { + if (0 < drug.benefit && drug.benefit <= 50) { + let benefitMultiplier = drug.benefitMultiplier; + let hasBenefitEnded = false; + + if (drug.expiresIn <= 0) { + benefitMultiplier = 2; + + if (!drug.hasBenefitAfterExpiration) { + drug.benefit = 0; + hasBenefitEnded = true; } } - } 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 ( + drug.benefitMultipliersByRemainingDay.length > 0 && + !hasBenefitEnded + ) { + drug.benefitMultipliersByRemainingDay + .sort((a, b) => { + return b.expiresIn - a.expiresIn; + }) + .map(element => { + if (drug.expiresIn <= element.expiresIn) { + benefitMultiplier = element.benefitMultiplier; } - } - } + }); } - } - 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; - } - } + + if (drug.benefitEffect !== null && !hasBenefitEnded) { + let benefitValue = drug.benefit; + + benefitValue = + benefitValue + + (drug.benefitEffect === "decrease" ? -1 : 1) * benefitMultiplier; + + if (benefitValue < 0) { + drug.benefit = 0; + } else if (benefitValue > 50) { + drug.benefit = 50; } 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; + drug.benefit = benefitValue; } } } - } + + if (drug.hasExpirationDate) { + drug.expiresIn--; + } + }); return this.drugs; } From 2fe6e168c8fb243e44a081d31f8b0e7a6f845b5f Mon Sep 17 00:00:00 2001 From: Thomas Bodin Date: Sat, 29 Jan 2022 11:27:22 +0100 Subject: [PATCH 3/4] feat(drugs.json): Create a json for all drugs --- drugs.json | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 26 +++++++++++++++---------- 2 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 drugs.json diff --git a/drugs.json b/drugs.json new file mode 100644 index 00000000..4c0bf31e --- /dev/null +++ b/drugs.json @@ -0,0 +1,57 @@ +{ + "drugs": [ + { + "name": "Doliprane", + "expiresIn": 20, + "benefit": 30, + "benefitEffect": "decrease", + "benefitMultiplier": 1, + "hasExpirationDate": true, + "hasBenefitAfterExpiration" : true, + "benefitMultipliersByRemainingDay": [] + }, + { + "name": "Herbal Tea", + "expiresIn": 10, + "benefit": 5, + "benefitEffect": "increase", + "benefitMultiplier": 1, + "hasExpirationDate": true, + "hasBenefitAfterExpiration" : true, + "benefitMultipliersByRemainingDay": [] + }, + { + "name": "Fervex", + "expiresIn": 5, + "benefit": 40, + "benefitEffect": "increase", + "benefitMultiplier": 1, + "hasExpirationDate": true, + "hasBenefitAfterExpiration" : false, + "benefitMultipliersByRemainingDay": [ + { "expiresIn": 10, "benefitMultiplier": 2 }, + { "expiresIn": 5, "benefitMultiplier": 3 } + ] + }, + { + "name": "Magic Pill", + "expiresIn": 15, + "benefit": 40, + "benefitEffect": null, + "benefitMultiplier": 1, + "hasExpirationDate": false, + "hasBenefitAfterExpiration" : true, + "benefitMultipliersByRemainingDay": [] + }, + { + "name": "Dafalgan", + "expiresIn": 20, + "benefit": 30, + "benefitEffect": "decrease", + "benefitMultiplier": 2, + "hasExpirationDate": true, + "hasBenefitAfterExpiration" : true, + "benefitMultipliersByRemainingDay": [] + } + ] +} diff --git a/index.js b/index.js index 4952ca0e..9738bee5 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,23 @@ import { Drug, Pharmacy } from "./pharmacy"; +import * as store from "./drugs.json"; import fs from "fs"; -const drugs = [ - new Drug("Doliprane", 20, 30), - new Drug("Herbal Tea", 10, 5, "increase"), - new Drug("Fervex", 5, 40, "increase", 1, true, false, [ - { expiresIn: 10, benefitMultiplier: 2 }, - { expiresIn: 5, benefitMultiplier: 3 } - ]), - new Drug("Magic Pill", 15, 40, null, 1, false), - new Drug("Dafalgan", 20, 30, "decrease", 2) -]; +const drugs = []; +store.drugs.map(drug => { + drugs.push( + new Drug( + drug.name, + drug.expiresIn, + drug.benefit, + drug.benefitEffect, + drug.benefitMultiplier, + drug.hasExpirationDate, + drug.hasBenefitAfterExpiration, + drug.benefitMultipliersByRemainingDay + ) + ); +}); const trial = new Pharmacy(drugs); const log = []; From 144bdf4b84c6161122705c37e13e8fa7433c7d18 Mon Sep 17 00:00:00 2001 From: Thomas Bodin Date: Sat, 29 Jan 2022 11:28:04 +0100 Subject: [PATCH 4/4] test(pharmacy): Add test for each drugs --- pharmacy.test.js | 226 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 222 insertions(+), 4 deletions(-) diff --git a/pharmacy.test.js b/pharmacy.test.js index f0925fc1..9be51226 100644 --- a/pharmacy.test.js +++ b/pharmacy.test.js @@ -1,9 +1,227 @@ import { Drug, Pharmacy } from "./pharmacy"; describe("Pharmacy", () => { - it("should decrease the benefit and expiresIn", () => { - expect(new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()).toEqual( - [new Drug("test", 1, 2)] - ); + it("should decrease expiresIn", () => { + expect( + new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue() + ).toEqual([new Drug("test", 1, 2)]); + }); + + it("should decrease benefit", () => { + expect( + new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue() + ).toEqual([new Drug("test", 1, 2)]); + }); + + it("should increase benefit", () => { + expect( + new Pharmacy([new Drug("test", 2, 3, "increase")]).updateBenefitValue() + ).toEqual([new Drug("test", 1, 4, "increase")]); + }); + + it("should not have negative benefit", () => { + expect( + new Pharmacy([new Drug("test", 0, 0)]).updateBenefitValue() + ).toEqual([new Drug("test", -1, 0)]); + }); + + it("should not have benefit more than 50", () => { + expect( + new Pharmacy([new Drug("test", 5, 50, "increase")]).updateBenefitValue() + ).toEqual([new Drug("test", 4, 50, "increase")]); + }); + + it("should decrease the benefit twice as fast when expiration date has passed", () => { + expect( + new Pharmacy([new Drug("test", -1, 10)]).updateBenefitValue() + ).toEqual([new Drug("test", -2, 8)]); + }); + + it("should not decrease expiresIn when hasExpirationDate is false", () => { + expect( + new Pharmacy([ + new Drug("test", 2, 3, null, 1, false) + ]).updateBenefitValue() + ).toEqual([new Drug("test", 2, 3, null, 1, false)]); + }); + + it("should not decrease benefit when benefitEffect is null", () => { + expect( + new Pharmacy([new Drug("test", 2, 3, null)]).updateBenefitValue() + ).toEqual([new Drug("test", 1, 3, null)]); + }); + + it("should decrease the benefit twice as fast as normal drug", () => { + expect( + new Pharmacy([new Drug("test", 2, 3, "decrease", 2)]).updateBenefitValue() + ).toEqual([new Drug("test", 1, 1, "decrease", 2)]); + }); + + it("should benefit drops to 0 when hasBenefitAfterExpiration is false and expiration date has passed", () => { + expect( + new Pharmacy([ + new Drug("test", 0, 21, "decrease", 1, true, false) + ]).updateBenefitValue() + ).toEqual([new Drug("test", -1, 0, "decrease", 1, true, false)]); + }); + + it("should decrease the benefit by benefitMultiplier when expiresIn is less than value", () => { + expect( + new Pharmacy([ + new Drug("test", 6, 21, "decrease", 1, true, true, [ + { expiresIn: 10, benefitMultiplier: 3 } + ]) + ]).updateBenefitValue() + ).toEqual([ + new Drug("test", 5, 18, "decrease", 1, true, true, [ + { expiresIn: 10, benefitMultiplier: 3 } + ]) + ]); + }); + + describe("Specifics drugs", () => { + describe("Herbal Tea", () => { + it("should increase benefit drug", () => { + expect( + new Pharmacy([ + new Drug("test", 10, 5, "increase") + ]).updateBenefitValue() + ).toEqual([new Drug("test", 9, 6, "increase")]); + }); + + it("should increase benefit twice as fast when expiration date has passed", () => { + expect( + new Pharmacy([ + new Drug("test", 0, 5, "increase") + ]).updateBenefitValue() + ).toEqual([new Drug("test", -1, 7, "increase")]); + }); + }); + + describe("Fervex", () => { + it("should increase benefit drug", () => { + expect( + new Pharmacy([ + new Drug("test", 11, 21, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]).updateBenefitValue() + ).toEqual([ + new Drug("test", 10, 22, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]); + }); + + it("should increase benefit by 2 when there are 10 days", () => { + expect( + new Pharmacy([ + new Drug("test", 10, 21, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]).updateBenefitValue() + ).toEqual([ + new Drug("test", 9, 23, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]); + }); + + it("should increase benefit by 2 when there are less than 10 days", () => { + expect( + new Pharmacy([ + new Drug("test", 9, 21, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]).updateBenefitValue() + ).toEqual([ + new Drug("test", 8, 23, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]); + }); + + it("should increase benefit by 3 when there are 5 days", () => { + expect( + new Pharmacy([ + new Drug("test", 5, 21, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]).updateBenefitValue() + ).toEqual([ + new Drug("test", 4, 24, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]); + }); + + it("should increase benefit by 3 when there are less than 5 days", () => { + expect( + new Pharmacy([ + new Drug("test", 4, 21, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]).updateBenefitValue() + ).toEqual([ + new Drug("test", 3, 24, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]); + }); + + it("should benefit drops to 0 when expiration date has passed", () => { + expect( + new Pharmacy([ + new Drug("test", 0, 21, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]).updateBenefitValue() + ).toEqual([ + new Drug("test", -1, 0, "increase", 1, true, false, [ + { expiresIn: 10, benefitMultiplier: 2 }, + { expiresIn: 5, benefitMultiplier: 3 } + ]) + ]); + }); + }); + + describe("Magic Pill", () => { + it("should never expire", () => { + expect( + new Pharmacy([ + new Drug("test", 15, 40, null, 1, false) + ]).updateBenefitValue() + ).toEqual([new Drug("test", 15, 40, null, 1, false)]); + }); + + it("should never decrease benefit", () => { + expect( + new Pharmacy([ + new Drug("test", 15, 40, null, 1, false) + ]).updateBenefitValue() + ).toEqual([new Drug("test", 15, 40, null, 1, false)]); + }); + }); + + describe("Dafalgan", () => { + it("should decrease the benefit twice as fast as normal drug", () => { + expect( + new Pharmacy([ + new Drug("test", 2, 3, "decrease", 2) + ]).updateBenefitValue() + ).toEqual([new Drug("test", 1, 1, "decrease", 2)]); + }); + }); }); });