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
183 changes: 134 additions & 49 deletions pharmacy.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,149 @@
export class Drug {
const DrugName = {
HerbalTea: "Herbal Tea",
MagicPill: "Magic Pill",
Fervex: "Fervex",
Dafalgan: "Dafalgan",
};

const maxBenefit = 50;
const minBenefit = 0;

export class GenericDrug {
constructor(name, expiresIn, benefit) {
this.name = name;
this.expiresIn = expiresIn;
this.benefit = benefit;
}

get isExpired() {
return this.expiresIn <= 0;
}

get canUpdateBenefit() {
return this.benefit < maxBenefit && this.benefit > minBenefit;
}
get canUpdateExpiresIn() {
return true;
}
get benefitVariation() {
return this.isExpired ? -2 : -1;
}

updateBenefit() {
if (!this.canUpdateBenefit) {
return;
}

this.benefit += this.benefitVariation;
}

verifyBenefit() {
if (this.benefit > maxBenefit) this.benefit = maxBenefit;

if (this.benefit < minBenefit) this.benefit = minBenefit;
}

updateExpiresIn() {
if (!this.canUpdateExpiresIn) {
return;
}

this.expiresIn--;
}

updateBenefitValue() {
this.updateBenefit();
this.updateExpiresIn();
this.verifyBenefit();
}
}
class HerbalTea extends GenericDrug {
constructor(name, expiresIn, benefit) {
super(DrugName.HerbalTea, expiresIn, benefit);
}

get benefitVariation() {
return this.isExpired ? 2 : 1;
}
}

class MagicPill extends GenericDrug {
constructor(name, expiresIn, benefit) {
super(DrugName.MagicPill, expiresIn, benefit);
}

get canUpdateBenefit() {
return false;
}
get canUpdateExpiresIn() {
return false;
}
}

class Fervex extends GenericDrug {
constructor(name, expiresIn, benefit) {
super(DrugName.Fervex, expiresIn, benefit);
}

get isExpired() {
return this.expiresIn < 0;
}

get benefitVariation() {
if (this.expiresIn <= 5) {
return 3;
} else if (this.expiresIn <= 10) {
return 2;
}

return 1;
}

updateBenefitValue() {
super.updateBenefitValue();

if (this.isExpired) {
this.benefit = 0;
}
}
}

class Dafalgan extends GenericDrug {
constructor(name, expiresIn, benefit) {
super(DrugName.Dafalgan, expiresIn, benefit);
}

get benefitVariation() {
console.log(super.benefitVariation);
console.log(super.benefitVariation * 2);
return super.benefitVariation * 2;
}
}

export class Drug {
constructor(name, expiresIn, benefit) {
switch (name) {
case DrugName.HerbalTea:
return new HerbalTea(name, expiresIn, benefit);
case DrugName.MagicPill:
return new MagicPill(name, expiresIn, benefit);
case DrugName.Fervex:
return new Fervex(name, expiresIn, benefit);
case DrugName.Dafalgan:
return new Dafalgan(name, expiresIn, benefit);
default:
return new GenericDrug(name, expiresIn, benefit);
}
}
}

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 (const drug of this.drugs) {
drug.updateBenefitValue();
}

return this.drugs;
Expand Down
164 changes: 160 additions & 4 deletions pharmacy.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,165 @@
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)]
);
describe("Regular Drug", () => {
describe("When regular drug is not expired and benefit not null", () => {
it("should decrease the benefit and expiresIn by one", () => {
expect(
new Pharmacy([new Drug("Doliprane", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("Doliprane", 1, 2)]);
});
});

describe("When regular drug has benefit to 0", () => {
it("should decrease expiresIn by one and not benefit", () => {
expect(
new Pharmacy([new Drug("Doliprane", 2, 0)]).updateBenefitValue()
).toEqual([new Drug("Doliprane", 1, 0)]);
});
});

describe("When regular drug is expired", () => {
it("Should decrease expiresIn by one and benefit by two", () => {
expect(
new Pharmacy([new Drug("Doliprane", -2, 30)]).updateBenefitValue()
).toEqual([new Drug("Doliprane", -3, 28)]);
});
});
});

describe("Magic Pill", () => {
describe("When Magic Pill has valid values", () => {
it("Should not decrease the benefit nor expiresIn", () => {
expect(
new Pharmacy([new Drug("Magic Pill", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("Magic Pill", 2, 3)]);
});
});
describe("When Magic Pill has benefit > 50", () => {
it("Should not decrease expiresIn ans put the benefiit to 50", () => {
expect(
new Pharmacy([new Drug("Magic Pill", 2, 60)]).updateBenefitValue()
).toEqual([new Drug("Magic Pill", 2, 50)]);
});
});
});

describe("Dafalgan", () => {
describe("When Dafalgan is not expired", () => {
it("Should decrease the benefit by 2 and decrease expiresIn by one", () => {
expect(
new Pharmacy([new Drug("Dafalgan", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("Dafalgan", 1, 1)]);
});
});

describe("When the Dafalgan is expired", () => {
it("Should decrease the benefit by 4 and decrease expiresIn by one", () => {
expect(
new Pharmacy([new Drug("Dafalgan", -2, 5)]).updateBenefitValue()
).toEqual([new Drug("Dafalgan", -3, 1)]);
});
});
});

describe("Herbal Tea", () => {
describe("When Herbal Tea is not expired", () => {
it("should decrease expiresIn and increase benefit", () => {
expect(
new Pharmacy([new Drug("Herbal Tea", 2, 3)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", 1, 4)]);
});
});

describe("When Herbal Tea is expired", () => {
it("Should decrease expiresIn and increase benefit by 2)", () => {
expect(
new Pharmacy([new Drug("Herbal Tea", -1, 3)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", -2, 5)]);
});
});
describe("When Herbal Tea is expired and benfit is at max (50)", () => {
it("Should decrease expiresIn and maintain benefit", () => {
expect(
new Pharmacy([new Drug("Herbal Tea", -1, 50)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", -2, 50)]);
});
});
describe("When Herbal Tea is expired and benfit is at 49", () => {
it("Should decrease expiresIn and increase benefit to 1 (to max)", () => {
expect(
new Pharmacy([new Drug("Herbal Tea", -1, 49)]).updateBenefitValue()
).toEqual([new Drug("Herbal Tea", -2, 50)]);
});
});
});

describe("Fervex", () => {
describe("When Fervex is not expired", () => {
it("Should decrease expiresIn and increase benefit", () => {
expect(
new Pharmacy([new Drug("Fervex", 20, 3)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 19, 4)]);
});
});

describe("When Fervex is below 10 days expiresIn", () => {
it("Should decrease expiresIn by one and increase benefit by 2", () => {
expect(
new Pharmacy([new Drug("Fervex", 10, 3)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 9, 5)]);
});
});

describe("When Fervex is below 5 days expiresIn", () => {
it("Should decrease expiresIn by one and increase benefit by 3", () => {
expect(
new Pharmacy([new Drug("Fervex", 5, 3)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 4, 6)]);
});
});

describe("When Fervex is below 0 days expiresIn", () => {
it("Should decrease expiresIn by one and set benefit to 0", () => {
expect(
new Pharmacy([new Drug("Fervex", -1, 3)]).updateBenefitValue()
).toEqual([new Drug("Fervex", -2, 0)]);
});
});
describe("When Fervex is below 0 days expiresIn and benefit at max (50)", () => {
it("Should decrease expiresIn and maintain benefit", () => {
expect(
new Pharmacy([new Drug("Fervex", 20, 50)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 19, 50)]);
});
});
describe("When Fervex is below 10 days expiresIn and benefit at max (50) ", () => {
it("Should decrease expiresIn and maintain benefit", () => {
expect(
new Pharmacy([new Drug("Fervex", 10, 50)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 9, 50)]);
});
});
describe("When Fervex is below 5 days expiresIn and benefit at max (50)", () => {
it("Should decrease expiresIn and maintain benefit", () => {
expect(
new Pharmacy([new Drug("Fervex", 5, 50)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 4, 50)]);
});
});
describe("When Fervex is below 10 days expiresIn and benefit is 49", () => {
it("should decrease expiresIn and increase benefit by 1 (to max)", () => {
expect(
new Pharmacy([new Drug("Fervex", 10, 49)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 9, 50)]);
});
});
describe("When Fervex is below 5 days expiresIn and benefit is 58", () => {
it("should decrease expiresIn and increase benefit by 2 (to max)", () => {
expect(
new Pharmacy([new Drug("Fervex", 5, 48)]).updateBenefitValue()
).toEqual([new Drug("Fervex", 4, 50)]);
});
});
});
});