Skip to content

Commit 2478c8d

Browse files
committed
feat(tiered_pricing): 🔵 refactoring implementation second pricing range
1 parent 729aa4a commit 2478c8d

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ public class TieredPricing {
66
public static final int FIRST_RANGE_LOWER_LIMIT = 1;
77
public static final int FIRST_RANGE_UPPER_LIMIT = 2;
88

9+
public static final int SECOND_RANGE_UNIT_PRICE = 239;
10+
public static final int SECOND_RANGE_LOWER_LIMIT = 3;
11+
public static final int SECOND_RANGE_UPPER_LIMIT = 10;
12+
913
public int calculateTotalPrice(int amount_subscriptions) {
1014
if (amount_subscriptions >= FIRST_RANGE_LOWER_LIMIT && amount_subscriptions<= FIRST_RANGE_UPPER_LIMIT) return amount_subscriptions * FIRST_RANGE_UNIT_PRICE;
1115

12-
if (amount_subscriptions == 3) return 717;
13-
14-
if (amount_subscriptions == 4) return 956;
16+
if (amount_subscriptions >= SECOND_RANGE_LOWER_LIMIT && amount_subscriptions<= SECOND_RANGE_UPPER_LIMIT) return amount_subscriptions * SECOND_RANGE_UNIT_PRICE;
1517

1618
return 0;
1719
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@ public class TieredPricingShould {
1212
@Test
1313
void calculate_total_value_for_first_pricing_range() {
1414
var pricing = new TieredPricing();
15-
var subscription = IntStream.rangeClosed(FIRST_RANGE_LOWER_LIMIT, FIRST_RANGE_UPPER_LIMIT).findAny().getAsInt();
16-
assertEquals(FIRST_RANGE_UNIT_PRICE * subscription, pricing.calculateTotalPrice(subscription));
15+
var subscription = retrieveSubscriptionGivenRange(FIRST_RANGE_LOWER_LIMIT, FIRST_RANGE_UPPER_LIMIT);
16+
assertEquals(calculateTotalPriceExpected(FIRST_RANGE_UNIT_PRICE, subscription), pricing.calculateTotalPrice(subscription));
1717
}
1818

1919
@Test
20-
void calculate_total_value_for_3_subscription() {
20+
void calculate_total_value_for_second_pricing_range() {
2121
var pricing = new TieredPricing();
22-
assertEquals(717, pricing.calculateTotalPrice(3));
22+
var subscription = retrieveSubscriptionGivenRange(SECOND_RANGE_LOWER_LIMIT,SECOND_RANGE_UPPER_LIMIT);
23+
assertEquals(calculateTotalPriceExpected(SECOND_RANGE_UNIT_PRICE, subscription), pricing.calculateTotalPrice(subscription));
2324
}
2425

25-
@Test
26-
void calculate_total_value_for_4_subscription() {
27-
var pricing = new TieredPricing();
28-
assertEquals(956, pricing.calculateTotalPrice(4));
26+
private int retrieveSubscriptionGivenRange(int lowerLimit, int upperLimit) {
27+
return IntStream.rangeClosed(lowerLimit, upperLimit).findAny().getAsInt();
28+
}
29+
30+
private int calculateTotalPriceExpected(int unitPrice, int subscription) {
31+
return unitPrice * subscription;
2932
}
3033
}

0 commit comments

Comments
 (0)