Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -23,4 +29,5 @@ fs.writeFile("output.txt", log.toString(), err => {
console.log("success");
}
});

/* eslint-enable no-console */
104 changes: 54 additions & 50 deletions pharmacy.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,70 @@
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;
}
}

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;
}
}