Skip to content

Commit dfcb515

Browse files
committed
finish
1 parent 30f5d44 commit dfcb515

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class InvalidUnitException extends Error {
2+
constructor(message: string) {
3+
super(message);
4+
}
5+
}

exercises/tiered_pricing/solutions/aortiz_typescript/src/Subscription.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {InvalidUnitException} from "./InvalidUnitException";
2+
13
export class Subscription {
24
private tieredPrice!: number;
35

@@ -6,7 +8,9 @@ export class Subscription {
68
}
79

810
private setTieredPrice() {
9-
if (this.units < 3) {
11+
if (this.units <= 0) {
12+
throw new InvalidUnitException("Unit should be greater than zero");
13+
} else if (this.units < 3) {
1014
this.tieredPrice = 299;
1115
} else if (this.units < 11) {
1216
this.tieredPrice = 239;

exercises/tiered_pricing/solutions/aortiz_typescript/tests/Subscription.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { InvalidUnitException } from "../src/InvalidUnitException";
12
import { Subscription } from "../src/Subscription";
23

34
describe("Subscription", () => {
@@ -12,7 +13,7 @@ describe("Subscription", () => {
1213
});
1314

1415
it("can be calculate subscription price of 2 unit", () => {
15-
const subscription = new Subscription(1);
16+
const subscription = new Subscription(2);
1617

1718
expect(subscription.getTotalPrice()).toBe(598);
1819
});
@@ -40,4 +41,8 @@ describe("Subscription", () => {
4041

4142
expect(subscription.getTotalPrice()).toBe(10132);
4243
});
44+
45+
it("should be throw InvalidUnitException", () => {
46+
expect(() => new Subscription(-1)).toThrow(InvalidUnitException);
47+
});
4348
});

0 commit comments

Comments
 (0)