Skip to content

Commit edd0a89

Browse files
committed
💥!: Drug is now abstract
Refactored the pharmacy class to remove all the logic from it. The drugs containing their own logic, they are in charge of updating themselves. Drug is made abstract because instantiating a drug with its name and expecting the logic to remain does not work anymore. The appropriate class should be used instead.
1 parent 04cffc1 commit edd0a89

File tree

9 files changed

+38
-158
lines changed

9 files changed

+38
-158
lines changed

‎src/index.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import fs from 'fs';
2-
import { Drug } from './model/drug/drug';
2+
import { Doliprane } from './model/drug/doliprane';
3+
import { Fervex } from './model/drug/fervex';
4+
import { HerbalTea } from './model/drug/herbalTea';
5+
import { MagicPill } from './model/drug/magicPill';
36
import { Pharmacy } from './model/pharmacy/pharmacy';
47

58
const drugs = [
6-
new Drug('Doliprane', 20, 30),
7-
new Drug('Herbal Tea', 10, 5),
8-
new Drug('Fervex', 5, 40),
9-
new Drug('Magic Pill', 15, 40),
9+
new Doliprane(20, 30),
10+
new HerbalTea(10, 5),
11+
new Fervex(5, 40),
12+
new MagicPill(15, 40),
1013
];
11-
const trial = new Pharmacy(drugs);
14+
const pharmacy = new Pharmacy(drugs);
1215

1316
const log = [];
1417

1518
for (let elapsedDays = 0; elapsedDays < 30; elapsedDays++) {
16-
log.push(JSON.stringify(trial.updateBenefitValue()));
19+
log.push(JSON.stringify(pharmacy.updateBenefitValue()));
1720
}
1821

1922
/* eslint-disable no-console */

‎src/model/drug/drug.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { DrugName } from '../../type/DrugName';
22
import { Drug } from './drug';
33

4+
class TestDrug extends Drug {
5+
constructor(expiresIn: number, benefit: number) {
6+
super(DrugName.TEST, expiresIn, benefit);
7+
}
8+
}
9+
410
describe('Drug', () => {
511
let drug: Drug;
612

713
beforeEach(() => {
8-
drug = new Drug(DrugName.TEST, 30, 5);
14+
drug = new TestDrug(30, 5);
915
});
1016

1117
it('should not allow for a negative benefit', () => {

‎src/model/drug/drug.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { DrugName, DrugNameType } from '../../type/DrugName';
1+
import { boundNumber } from '../../tools/boundNumber';
2+
import { DrugName } from '../../type/DrugName';
23

3-
export class Drug {
4+
export abstract class Drug {
45
private _internalBenefit: number;
56

6-
constructor(
7-
public readonly name: DrugName | DrugNameType,
7+
protected constructor(
8+
public readonly name: DrugName,
89
public expiresIn: number,
910
benefit: number
1011
) {
@@ -20,7 +21,7 @@ export class Drug {
2021
* @param value
2122
*/
2223
public set benefit(value: number) {
23-
this._internalBenefit = Math.max(0, value);
24+
this._internalBenefit = boundNumber(value, 0, 50);
2425
}
2526

2627
/**
@@ -33,9 +34,11 @@ export class Drug {
3334
/**
3435
* Update internal expiration and benefit values according to drug's rules
3536
*/
36-
public updateValues() {
37+
public updateValues(): this {
3738
this.benefit -= this.isExpired() ? 2 : 1;
3839
this.expiresIn -= 1;
40+
41+
return this;
3942
}
4043

4144
/**

‎src/model/drug/fervex.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ export class Fervex extends Drug {
1717
this.benefit = 0;
1818
}
1919
this.expiresIn -= 1;
20+
21+
return this;
2022
}
2123
}

‎src/model/drug/herbalTea.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ export class HerbalTea extends Drug {
1313
this.benefit += 1;
1414
}
1515
this.expiresIn -= 1;
16+
17+
return this;
1618
}
1719
}

‎src/model/drug/magicPill.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class MagicPill extends Drug {
1010
return false;
1111
}
1212

13-
// eslint-disable-next-line @typescript-eslint/no-empty-function
14-
public updateValues() {}
13+
public updateValues() {
14+
return this;
15+
}
1516
}

‎src/model/pharmacy/pharmacy.test.ts‎

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,9 @@
1-
export class Pharmacy {
2-
constructor(private readonly drugs = []) {}
1+
import { Drug } from '../drug/drug';
32

4-
updateBenefitValue() {
5-
for (let i = 0; i < this.drugs.length; i++) {
6-
if (
7-
this.drugs[i].name != 'Herbal Tea' &&
8-
this.drugs[i].name != 'Fervex'
9-
) {
10-
if (this.drugs[i].benefit > 0) {
11-
if (this.drugs[i].name != 'Magic Pill') {
12-
this.drugs[i].benefit = this.drugs[i].benefit - 1;
13-
}
14-
}
15-
} else {
16-
if (this.drugs[i].benefit < 50) {
17-
this.drugs[i].benefit = this.drugs[i].benefit + 1;
18-
if (this.drugs[i].name == 'Fervex') {
19-
if (this.drugs[i].expiresIn < 11) {
20-
if (this.drugs[i].benefit < 50) {
21-
this.drugs[i].benefit = this.drugs[i].benefit + 1;
22-
}
23-
}
24-
if (this.drugs[i].expiresIn < 6) {
25-
if (this.drugs[i].benefit < 50) {
26-
this.drugs[i].benefit = this.drugs[i].benefit + 1;
27-
}
28-
}
29-
}
30-
}
31-
}
32-
if (this.drugs[i].name != 'Magic Pill') {
33-
this.drugs[i].expiresIn = this.drugs[i].expiresIn - 1;
34-
}
35-
if (this.drugs[i].expiresIn < 0) {
36-
if (this.drugs[i].name != 'Herbal Tea') {
37-
if (this.drugs[i].name != 'Fervex') {
38-
if (this.drugs[i].benefit > 0) {
39-
if (this.drugs[i].name != 'Magic Pill') {
40-
this.drugs[i].benefit = this.drugs[i].benefit - 1;
41-
}
42-
}
43-
} else {
44-
this.drugs[i].benefit =
45-
this.drugs[i].benefit - this.drugs[i].benefit;
46-
}
47-
} else {
48-
if (this.drugs[i].benefit < 50) {
49-
this.drugs[i].benefit = this.drugs[i].benefit + 1;
50-
}
51-
}
52-
}
53-
}
3+
export class Pharmacy {
4+
constructor(private readonly drugs: Drug[] = []) {}
545

55-
return this.drugs;
6+
public updateBenefitValue() {
7+
return this.drugs.map((drug) => drug.updateValues());
568
}
579
}

‎src/type/DrugName.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,3 @@ export enum DrugName {
55
MAGIC_PILL = 'Magic Pill',
66
TEST = 'test',
77
}
8-
9-
export type DrugNameType = `${DrugName}`;

0 commit comments

Comments
 (0)