Skip to content

Commit 312ceeb

Browse files
committed
feat(tiered_pricing): 🟢 add GraduatedTier named constructors
1 parent f9eb4e2 commit 312ceeb

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ export default class GraduatedTier {
77
private readonly to: number,
88
private readonly singleSubscriptionPrice: number
99
) {
10-
if (to > Number.MAX_SAFE_INTEGER) {
11-
throw new InvalidTier();
12-
}
10+
GraduatedTier.validate(from, to, singleSubscriptionPrice);
1311
}
1412

1513
static firstTier(
@@ -44,6 +42,28 @@ export default class GraduatedTier {
4442
);
4543
}
4644

45+
private static validate(
46+
from: number,
47+
to: number,
48+
singleSubscriptionPrice: number
49+
): void {
50+
if (singleSubscriptionPrice <= 0) {
51+
throw new InvalidTier();
52+
}
53+
54+
if (from < 1) {
55+
throw new InvalidTier();
56+
}
57+
58+
if (from > to) {
59+
throw new InvalidTier();
60+
}
61+
62+
if (to > Number.MAX_SAFE_INTEGER) {
63+
throw new InvalidTier();
64+
}
65+
}
66+
4767
private total(): number {
4868
return this.size() * this.singleSubscriptionPrice;
4969
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@ import GraduatedTier from "../src/GraduatedTier";
22
import InvalidTier from "../src/InvalidTier";
33

44
describe("Graduated tiered should", () => {
5-
it("throw number of subscriptions not allowed for 0 subscriptions", () => {
5+
it("throw invalid tier for 0 subscriptions in tier", () => {
66
expect(() => GraduatedTier.firstTier(0, 199)).toThrow(InvalidTier);
77
});
8+
9+
it("throw invalid tier for -100 subscriptions in tier", () => {
10+
const firstTier = GraduatedTier.firstTier(2, 199);
11+
expect(() => GraduatedTier.fromTier(firstTier, -100, 150)).toThrow(
12+
InvalidTier
13+
);
14+
});
15+
16+
it("throw invalid tier for single subscription price of 0 in tier", () => {
17+
expect(() => GraduatedTier.firstTier(2, 0)).toThrow(InvalidTier);
18+
});
19+
20+
it("work for 1 subscription in tier", () => {
21+
const firstTier = GraduatedTier.firstTier(1, 199);
22+
GraduatedTier.fromTier(firstTier, 1, 150);
23+
});
824
});

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ describe("Graduated tiered pricing should", () => {
1515
return [firstTier, secondTier, thirdTier, fourthTier, lastTier];
1616
};
1717

18+
const tiersWithOneSubscription = (): GraduatedTier[] => {
19+
const firstTier = GraduatedTier.firstTier(1, 299);
20+
const secondTier = GraduatedTier.fromTier(firstTier, 1, 239);
21+
const thirdTier = GraduatedTier.fromTier(secondTier, 1, 219);
22+
const fourthTier = GraduatedTier.fromTier(thirdTier, 1, 199);
23+
const lastTier = GraduatedTier.lastTier(fourthTier, 149);
24+
return [firstTier, secondTier, thirdTier, fourthTier, lastTier];
25+
};
26+
1827
it("throw number of subscriptions not allowed for 0 subscriptions", () => {
1928
const tiers = defaultTiers();
2029
const pricing = new GraduatedTieredPricing(tiers);
@@ -41,4 +50,22 @@ describe("Graduated tiered pricing should", () => {
4150
);
4251
}
4352
);
53+
54+
each([
55+
[1, 299],
56+
[2, 538],
57+
[3, 757],
58+
[4, 956],
59+
[5, 1105],
60+
[100, 15260],
61+
]).it(
62+
"calculate the price for %i subscriptions with tiers with only one subscription",
63+
(subscriptions: number, expectedPrice: number) => {
64+
const tiers = tiersWithOneSubscription();
65+
const pricing = new GraduatedTieredPricing(tiers);
66+
expect(pricing.priceFor(new Subscriptions(subscriptions))).toBe(
67+
expectedPrice
68+
);
69+
}
70+
);
4471
});

0 commit comments

Comments
 (0)