diff --git a/index.js b/index.js index 525c9de1..1edf7649 100644 --- a/index.js +++ b/index.js @@ -2,10 +2,16 @@ import { Drug, Pharmacy } from "./pharmacy"; 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) + // Normal Drugs decrease benefit by 1 each day, do not expire after expir date and have a linear evolution. + new Drug("Doliprane", 20, 30, 1, 1, false, true), + // Herbal tea increases in benefit over time, normal evolution (twice after expiration date). + new Drug("Herbal Tea", 10, 5, 1, -1, false, true), + // Fervex increases in benefit over time, not a linear evolution. + new Drug("Fervex", 5, 40, 1, -1, true, false, [10, 5]), + // magic pill will not decrease benefit nor expiration date. + new Drug("Magic Pill", 15, 40, 0, 0, false, true), + // Dafalgan is a normal drug but decreases benefit twice as fast. + new Drug("Dafalgan", 20, 30, 1, 2, false, true) ]; const trial = new Pharmacy(drugs); @@ -23,4 +29,5 @@ fs.writeFile("output.txt", log.toString(), err => { console.log("success"); } }); + /* eslint-enable no-console */ diff --git a/pharmacy.js b/pharmacy.js index cda44c41..30310f3b 100644 --- a/pharmacy.js +++ b/pharmacy.js @@ -1,8 +1,58 @@ export class Drug { - constructor(name, expiresIn, benefit) { + constructor(name, expiresIn, benefit, exipreEvol = 1, benefitEvol = 1, expire = false, isLinear = true, evolArray = []) { this.name = name; this.expiresIn = expiresIn; this.benefit = benefit; + this.exipreEvol = exipreEvol; + this.benefitEvol = benefitEvol; + this.expire = expire; + this.isLinear = isLinear; + this.evolArray = evolArray; + } + + updateLinear() { + // benefit degrades with time + this.benefit = this.benefit - this.benefitEvol; + // once theexpiration date is reached, benefit degrades twice as fast + if (this.expiresIn == 0) + this.benefit = this.benefit - this.benefitEvol; + } + + updateEvolveWithTime() { + + for (let i = 0; i < this.evolArray.length; i++) { + if (this.expiresIn <= this.evolArray[i]) { + this.benefit = this.benefit - this.benefitEvol; + } + } + this.benefit = this.benefit - this.benefitEvol; + } + + ExpireAtExpirationDate() { + if (this.expire == true && this.expiresIn == 0) { + this.benefit = 0; + } + } + + checkBenefit() { + this.ExpireAtExpirationDate(); + // benefit is never negative. + if (this.benefit < 0) + this.benefit = 0; + // benefit is never more than 50. + else if (this.benefit > 50) + this.benefit = 50; + } + + updateDrug() { + if (this.isLinear == true) + this.updateLinear(); + else + this.updateEvolveWithTime(); + this.checkBenefit(); + // a date should never be negative ? + if (this.expiresIn > 0) + this.expiresIn = this.expiresIn - this.exipreEvol; } } @@ -10,57 +60,11 @@ 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; - } - } - } 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++) { + this.drugs[i].updateDrug(); } - return this.drugs; } }