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
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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),
new Drug("Dafalgan", 20, 25)
];
const trial = new Pharmacy(drugs);

Expand Down
86 changes: 39 additions & 47 deletions pharmacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,48 @@ export class Pharmacy {
constructor(drugs = []) {
this.drugs = drugs;
}

decreaseBenefit(drug) {
drug.benefit = drug.benefit - 1;
}

increaseBenefit(drug) {
drug.benefit = drug.benefit + 1;
}

decreaseExpiresIn(drug) {
drug.expiresIn = drug.expiresIn - 1;
}

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;
this.drugs.forEach(drug => {
switch (drug.name) {
case "Herbal Tea":
this.decreaseExpiresIn(drug);
if (drug.benefit < 50) this.increaseBenefit(drug);
if (drug.expiresIn < 0 && drug.benefit < 50)
this.increaseBenefit(drug);
break;
case "Fervex":
this.decreaseExpiresIn(drug);
if (drug.benefit < 50) this.increaseBenefit(drug);
if (drug.expiresIn < 11 && drug.benefit < 50)
this.increaseBenefit(drug);
if (drug.expiresIn < 6 && drug.benefit < 50)
this.increaseBenefit(drug);
if (drug.expiresIn < 0) drug.benefit = drug.benefit - drug.benefit;
break;
case "Magic Pill":
drug.benefit = drug.benefit;
drug.expiresIn = drug.expiresIn;
break;
default:
this.decreaseExpiresIn(drug);
if (drug.benefit > 0) {
this.decreaseBenefit(drug);
}
}
}
}
});

return this.drugs;
}
Expand Down
53 changes: 52 additions & 1 deletion pharmacy.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,60 @@
import { Drug, Pharmacy } from "./pharmacy";

describe("Pharmacy", () => {
it("should decrease the benefit and expiresIn", () => {
it("should decrease the benefit and expiresIn by 1", () => {
expect(new Pharmacy([new Drug("test", 2, 3)]).updateBenefitValue()).toEqual(
[new Drug("test", 1, 2)]
);
expect(
new Pharmacy([new Drug("Doliprane", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("Doliprane", 1, 2)]);
expect(
new Pharmacy([new Drug("Dafalgan", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("Dafalgan", 1, 2)]);
});

it("should increase the benefit by 1 and decrease expiresIn", () => {
expect(
new Pharmacy([new Drug("Fervex", 12, 3)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 11, 4)]);
expect(
new Pharmacy([new Drug("Herbal Tea", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", 1, 4)]);
});

it("should increase the benefit by 2 when expiresIn less than 11", () => {
expect(
new Pharmacy([new Drug("Fervex", 9, 3)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 8, 5)]);
expect(
new Pharmacy([new Drug("Fervex", 8, 2)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 7, 4)]);
});

it("should increase the benefit by 3 when expiresIn less than 6", () => {
expect(
new Pharmacy([new Drug("Fervex", 5, 3)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 4, 6)]);
expect(
new Pharmacy([new Drug("Fervex", 4, 2)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 3, 5)]);
});

it("should the benefit drop to 0 when expiresIn is negative", () => {
expect(
new Pharmacy([new Drug("Fervex", -1, 2)]).updateBenefitValue()
).toEqual([new Drug("Fervex", -2, 0)]);
});

it("should the benefit increase by 2 when expiresIn is negative", () => {
expect(
new Pharmacy([new Drug("Herbal Tea", -1, 3)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", -2, 5)]);
});

it("should not change benefit nor expiresIn", () => {
expect(
new Pharmacy([new Drug("Magic Pill", 1, 2)]).updateBenefitValue()
).toEqual([new Drug("Magic Pill", 1, 2)]);
});
});