Skip to content

Commit 8486217

Browse files
committed
feat(tiered_pricing): 🔴 push logic to Subscriptions
1 parent 6deb8d1 commit 8486217

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

exercises/graduated_tiered_prices/solutions/adrianliz/src/GraduatedTier.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Subscriptions from "./Subscriptions";
2+
13
export default class GraduatedTier {
24
constructor(
35
private readonly from: number,
@@ -28,4 +30,16 @@ export default class GraduatedTier {
2830

2931
return (subscriptions - this.from + 1) * this.price;
3032
}
33+
34+
totalFor2(subscriptions: Subscriptions): number {
35+
if (subscriptions.covers(this)) {
36+
return this.total();
37+
}
38+
39+
if (!subscriptions.reaches(this)) {
40+
return 0;
41+
}
42+
43+
return subscriptions.numberOfSubscriptionsInTier(this) * this.price;
44+
}
3145
}
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import GraduatedTier from "./GraduatedTier";
2-
import NumberOfSubscriptionsNotAllowed from "./NumberOfSubscriptionsNotAllowed";
32
import Subscriptions from "./Subscriptions";
43

54
export default class GraduatedTieredPricing {
@@ -13,23 +12,7 @@ export default class GraduatedTieredPricing {
1312
];
1413
}
1514

16-
priceFor(subscriptions: number): number {
17-
if (subscriptions < 1) {
18-
throw new NumberOfSubscriptionsNotAllowed();
19-
}
20-
21-
const tiers = this.tiers();
22-
23-
return (
24-
tiers[0].totalFor(subscriptions) +
25-
tiers[1].totalFor(subscriptions) +
26-
tiers[2].totalFor(subscriptions) +
27-
tiers[3].totalFor(subscriptions) +
28-
tiers[4].totalFor(subscriptions)
29-
);
30-
}
31-
32-
priceFor2(subscriptions: Subscriptions): number {
15+
priceFor(subscriptions: Subscriptions): number {
3316
return subscriptions.priceFor(this.tiers());
3417
}
3518
}

exercises/graduated_tiered_prices/solutions/adrianliz/src/Subscriptions.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@ export default class Subscriptions {
99
}
1010

1111
priceFor(tiers: GraduatedTier[]): number {
12-
return tiers.reduce(
13-
(total, tier) => total + tier.totalFor(this.subscriptions),
14-
0
15-
);
12+
// return tiers.reduce(
13+
// (total, tier) => total + tier.totalFor(this.subscriptions),
14+
// 0
15+
// );
16+
17+
return tiers.reduce((total, tier) => total + tier.totalFor2(this), 0);
18+
}
19+
20+
covers(tier: GraduatedTier): boolean {
21+
return false;
22+
}
23+
24+
reaches(tier: GraduatedTier): boolean {
25+
return false;
26+
}
27+
28+
numberOfSubscriptionsInTier(tier: GraduatedTier): number {
29+
return 0;
1630
}
1731
}

exercises/graduated_tiered_prices/solutions/adrianliz/tests/GraduatedTieredPricing.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Subscriptions from "../src/Subscriptions";
77
describe("Graduated tiered pricing should", () => {
88
it("throw number of subscriptions not allowed for 0 subscriptions", () => {
99
const pricing = new GraduatedTieredPricing();
10-
expect(() => pricing.priceFor2(new Subscriptions(0))).toThrow(
10+
expect(() => pricing.priceFor(new Subscriptions(0))).toThrow(
1111
NumberOfSubscriptionsNotAllowed
1212
);
1313
});
@@ -24,7 +24,7 @@ describe("Graduated tiered pricing should", () => {
2424
"calculate the price for %i subscriptions",
2525
(subscriptions: number, expectedPrice: number) => {
2626
const pricing = new GraduatedTieredPricing();
27-
expect(pricing.priceFor2(new Subscriptions(subscriptions))).toBe(
27+
expect(pricing.priceFor(new Subscriptions(subscriptions))).toBe(
2828
expectedPrice
2929
);
3030
}

0 commit comments

Comments
 (0)