|
| 1 | +import NumberOfSubscriptionsNotAllowed from "./NumberOfSubscriptionsNotAllowed"; |
| 2 | + |
1 | 3 | export default class GraduatedTieredPricing { |
2 | 4 | priceFor(subscriptions: number): number { |
| 5 | + if (subscriptions < 1) { |
| 6 | + throw new NumberOfSubscriptionsNotAllowed(); |
| 7 | + } |
| 8 | + |
3 | 9 | const step1Price = 299; |
4 | 10 | const step2Price = 239; |
| 11 | + const step3Price = 219; |
| 12 | + const step4Price = 199; |
| 13 | + const step5Price = 149; |
| 14 | + |
| 15 | + const step1Boundary = 2; |
| 16 | + const step2Boundary = 10; |
| 17 | + const step3Boundary = 25; |
| 18 | + const step4Boundary = 50; |
5 | 19 |
|
6 | | - if (subscriptions <= 2) { |
| 20 | + const numberOfSubscriptionsInStep1 = 2; |
| 21 | + const numberOfSubscriptionsInStep2 = step2Boundary - step1Boundary; // 8 |
| 22 | + const numberOfSubscriptionsInStep3 = step3Boundary - step2Boundary; // 15 |
| 23 | + const numberOfSubscriptionsInStep4 = step4Boundary - step3Boundary; // 25 |
| 24 | + |
| 25 | + if (subscriptions <= step1Boundary) { |
7 | 26 | return subscriptions * step1Price; |
8 | 27 | } |
9 | | - if (subscriptions <= 5) { |
10 | | - return 2 * step1Price + (subscriptions - 2) * step2Price; |
| 28 | + if (subscriptions <= step2Boundary) { |
| 29 | + return ( |
| 30 | + numberOfSubscriptionsInStep1 * step1Price + |
| 31 | + (subscriptions - numberOfSubscriptionsInStep1) * step2Price |
| 32 | + ); |
| 33 | + } |
| 34 | + if (subscriptions <= step3Boundary) { |
| 35 | + return ( |
| 36 | + numberOfSubscriptionsInStep1 * step1Price + |
| 37 | + numberOfSubscriptionsInStep2 * step2Price + |
| 38 | + (subscriptions - |
| 39 | + (numberOfSubscriptionsInStep1 + numberOfSubscriptionsInStep2)) * |
| 40 | + step3Price |
| 41 | + ); |
| 42 | + } |
| 43 | + if (subscriptions <= step4Boundary) { |
| 44 | + return ( |
| 45 | + numberOfSubscriptionsInStep1 * step1Price + |
| 46 | + numberOfSubscriptionsInStep2 * step2Price + |
| 47 | + numberOfSubscriptionsInStep3 * step3Price + |
| 48 | + (subscriptions - |
| 49 | + (numberOfSubscriptionsInStep1 + |
| 50 | + numberOfSubscriptionsInStep2 + |
| 51 | + numberOfSubscriptionsInStep3)) * |
| 52 | + step4Price |
| 53 | + ); |
11 | 54 | } |
12 | | - throw new Error("Not implemented yet"); |
| 55 | + return ( |
| 56 | + numberOfSubscriptionsInStep1 * step1Price + |
| 57 | + numberOfSubscriptionsInStep2 * step2Price + |
| 58 | + numberOfSubscriptionsInStep3 * step3Price + |
| 59 | + numberOfSubscriptionsInStep4 * step4Price + |
| 60 | + (subscriptions - |
| 61 | + (numberOfSubscriptionsInStep1 + |
| 62 | + numberOfSubscriptionsInStep2 + |
| 63 | + numberOfSubscriptionsInStep3 + |
| 64 | + numberOfSubscriptionsInStep4)) * |
| 65 | + step5Price |
| 66 | + ); |
13 | 67 | } |
14 | 68 | } |
0 commit comments