Skip to content

Commit 8b0c21b

Browse files
committed
feat: add PriceRange concept
TieredPricing constructor parallel change
1 parent a032f89 commit 8b0c21b

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tv.codely.checkout;
2+
3+
public final class PriceRange {
4+
5+
private final int numberOfSubscriptionsFrom;
6+
private final int numberOfSubscriptionsTo;
7+
private final double unitPrice;
8+
9+
public PriceRange(int numberOfSubscriptionsFrom, int numberOfSubscriptionsTo,
10+
double unitPrice) {
11+
this.numberOfSubscriptionsFrom = numberOfSubscriptionsFrom;
12+
this.numberOfSubscriptionsTo = numberOfSubscriptionsTo;
13+
this.unitPrice = unitPrice;
14+
}
15+
16+
public boolean isInRange(int numberOfSubscriptions) {
17+
return numberOfSubscriptions >= numberOfSubscriptionsFrom
18+
&& numberOfSubscriptions <= numberOfSubscriptionsTo;
19+
}
20+
21+
public double unitPrice() {
22+
return unitPrice;
23+
}
24+
25+
public double getTotalPrice(int numberOfSubscriptions) {
26+
return unitPrice * numberOfSubscriptions;
27+
}
28+
}
Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
package tv.codely.checkout;
22

3+
import java.util.List;
4+
35
public class TieredPricing {
46

5-
public int getTotalPrice(int subscriptions) {
6-
switch (subscriptions) {
7-
case 1, 2:
8-
return 299 * subscriptions;
9-
case 3, 4, 5, 6, 7, 8, 9, 10:
10-
return 239 * subscriptions;
11-
case 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25:
12-
return 219 * subscriptions;
13-
case 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50:
14-
return 199 * subscriptions;
15-
default:
16-
return 149 * subscriptions;
17-
}
7+
private final List<PriceRange> priceRanges;
8+
9+
public TieredPricing(final List<PriceRange> priceRanges) {
10+
this.priceRanges = priceRanges;
11+
}
12+
13+
public TieredPricing() {
14+
this(List.of(
15+
new PriceRange(1, 2, 299),
16+
new PriceRange(3, 10, 239),
17+
new PriceRange(11, 25, 219),
18+
new PriceRange(26, 50, 199),
19+
new PriceRange(51, 100, 149)));
20+
}
21+
22+
public double getTotalPrice(int subscriptions) {
23+
return priceRanges.stream().filter(priceRange -> priceRange.isInRange(subscriptions))
24+
.findFirst()
25+
.map(priceRange -> priceRange.getTotalPrice(subscriptions))
26+
.orElse(0D);
1827
}
1928
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

6+
import java.util.List;
67
import org.junit.jupiter.api.Test;
78

89
public class TieredPricingShould {
@@ -12,6 +13,22 @@ void unit_test_example() {
1213
assertTrue(true);
1314
}
1415

16+
@Test
17+
void return_total_price_based_on_number_of_subscriptions() {
18+
final var priceRanges =
19+
List.of(
20+
new PriceRange(1, 2, 299),
21+
new PriceRange(3, 10, 239),
22+
new PriceRange(11, 25, 219),
23+
new PriceRange(26, 50, 199),
24+
new PriceRange(51, 100, 149));
25+
final var tieredPricing = new TieredPricing(priceRanges);
26+
final var expectedPrice = priceRanges.get(1).unitPrice() * 3;
27+
28+
final var totalPrice = tieredPricing.getTotalPrice(3);
29+
assertEquals(expectedPrice, totalPrice);
30+
}
31+
1532
@Test
1633
void return_total_price_for_1_subscription() {
1734
final var tieredPricing = new TieredPricing();

0 commit comments

Comments
 (0)