Skip to content

Commit 9247c2b

Browse files
committed
Add last tier
1 parent bf50497 commit 9247c2b

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

exercises/tiered_pricing/solutions/mperezi/src/main/java/tv/codely/checkout/TieredPricing.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ private enum Tier {
88
FIRST(299.0, 1, 2),
99
SECOND(239.0, 3, 10),
1010
THIRD(219.0, 11, 25),
11-
FOURTH(199.0, 26, 50);
11+
FOURTH(199.0, 26, 50),
12+
LAST(149.0, 51);
1213

1314
final double unitPrice;
1415
final int startRange;
1516
final int endRange;
1617

18+
Tier(double unitPrice, int startRange) {
19+
this(unitPrice, startRange, -1);
20+
}
21+
1722
Tier(double unitPrice, int startRange, int endRange) {
1823
this.unitPrice = unitPrice;
1924
this.startRange = startRange;
@@ -24,7 +29,7 @@ static Tier of(int subscriptions) {
2429
return Arrays.stream(values())
2530
.filter(tier -> subscriptions >= tier.startRange && subscriptions <= tier.endRange)
2631
.findFirst()
27-
.orElseThrow(() -> new IllegalArgumentException("Invalid number of subscriptions: " + subscriptions));
32+
.orElse(LAST);
2833
}
2934
}
3035

exercises/tiered_pricing/solutions/mperezi/src/test/java/tv/codely/checkout/TieredPricingShould.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.jupiter.api.BeforeEach;
44
import org.junit.jupiter.params.ParameterizedTest;
55
import org.junit.jupiter.params.provider.MethodSource;
6+
import org.junit.jupiter.params.provider.ValueSource;
67

78
import java.util.stream.IntStream;
89
import java.util.stream.Stream;
@@ -50,6 +51,14 @@ void returnTotalPriceForFourthTier(int subscriptions) {
5051
assertThat(totalPrice).isEqualTo(subscriptions * 199.0);
5152
}
5253

54+
@ParameterizedTest
55+
@ValueSource(ints = {51, 1000})
56+
void returnTotalPriceForLastTier(int subscriptions) {
57+
double totalPrice = tieredPricing.totalPrice(subscriptions);
58+
59+
assertThat(totalPrice).isEqualTo(subscriptions * 149.0);
60+
}
61+
5362
private static Stream<Integer> rangeFirstTier() {
5463
return IntStream.rangeClosed(1, 2).boxed();
5564
}

0 commit comments

Comments
 (0)