Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nicolas test Libeo #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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)
new Drug("Magic Pill", 15, 40),
];
const trial = new Pharmacy(drugs);

Expand All @@ -16,7 +17,7 @@ for (let elapsedDays = 0; elapsedDays < 30; elapsedDays++) {
}

/* eslint-disable no-console */
fs.writeFile("output.txt", log.toString(), err => {
fs.writeFile("output.txt", log.toString(), (err) => {
if (err) {
console.log("error");
} else {
Expand Down
78 changes: 32 additions & 46 deletions pharmacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,42 @@ export class Pharmacy {
this.drugs = drugs;
}
updateBenefitValue() {
const benefitLimit = (num) => {
if (num > 50) return 50;
else if (num < 0) return 0;
else return num;
};

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;
}
}
}
const special = ["Magic Pill", "Fervex", "Herbal Tea"];
this.drugs[i].expiresIn--;
let { name, expiresIn } = this.drugs[i];
let benefit = 0;

if (!special.includes(name)) {
if (expiresIn < 0) benefit -= 2;
else benefit--;
if (name === "Dafalgan") {
benefit *= 2;
}
}
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;
}
}

switch (name) {
case "Magic Pill":
break;
case "Fervex":
if (expiresIn < 1) this.drugs[i].benefit = 0;
else if (expiresIn < 5) benefit += 3;
else if (expiresIn < 10) benefit += 2;
else benefit += 1;
break;
case "Herbal Tea":
if (expiresIn < 0) benefit += 2;
else benefit += 1;
break;
}
this.drugs[i].benefit = benefitLimit(this.drugs[i].benefit + benefit);
return this.drugs;
}

return this.drugs;
}
}
42 changes: 39 additions & 3 deletions pharmacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,44 @@ 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)]
);
expect(new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()).toEqual([new Drug("test", 1, 2)]);
});
it("should decrease the benefit with 0 limit", () => {
expect(new Pharmacy([new Drug("test", -2, 0)]).updateBenefitValue()).toEqual([new Drug("test", -3, 0)]);
});
it("should stay the same Magic Pill, decrease expiresIn", () => {
expect(new Pharmacy([new Drug("Magic Pill", 12, 14)]).updateBenefitValue()).toEqual([
new Drug("Magic Pill", 11, 14),
]);
});
it("Fervex should decrease expiresIn and increase the benefit by 3", () => {
expect(new Pharmacy([new Drug("Fervex", 5, 20)]).updateBenefitValue()).toEqual([new Drug("Fervex", 4, 23)]);
});
it("Fervex should decrease expiresIn and increase the benefit by 2", () => {
expect(new Pharmacy([new Drug("Fervex", 9, 20)]).updateBenefitValue()).toEqual([new Drug("Fervex", 8, 22)]);
});
it("Fervex should decrease expiresIn and increse benefit", () => {
expect(new Pharmacy([new Drug("Fervex", 11, 46)]).updateBenefitValue()).toEqual([new Drug("Fervex", 10, 47)]);
});
it("Fervex should decrease expiresIn, increse benefit by 3 but max at 50", () => {
expect(new Pharmacy([new Drug("Fervex", 4, 48)]).updateBenefitValue()).toEqual([new Drug("Fervex", 3, 50)]);
});
it("Fervex benefit should be 0", () => {
expect(new Pharmacy([new Drug("Fervex", 1, 46)]).updateBenefitValue()).toEqual([new Drug("Fervex", 0, 0)]);
});

it("herbal tea should decrease expiresIn and increase the benefit", () => {
expect(new Pharmacy([new Drug("Herbal Tea", 1, 20)]).updateBenefitValue()).toEqual([new Drug("Herbal Tea", 0, 21)]);
});
it("herbal tea should decrease expiresIn and increase the benefit by 2", () => {
expect(new Pharmacy([new Drug("Herbal Tea", -1, 14)]).updateBenefitValue()).toEqual([
new Drug("Herbal Tea", -2, 16),
]);
});
it("Dafalgan should decrease twice as fast as normal drugs", () => {
expect(new Pharmacy([new Drug("Dafalgan", 0, 18)]).updateBenefitValue()).toEqual([new Drug("Dafalgan", -1, 14)]);
});
it("Dafalgan should decrease twice as fast as normal drugs but min at 0", () => {
expect(new Pharmacy([new Drug("Dafalgan", 0, 3)]).updateBenefitValue()).toEqual([new Drug("Dafalgan", -1, 0)]);
});
});
Loading