Challenge 1: Remove Temporal Coupling

Test your knowledge by writing code for a non-temporally coupled version of the PriceCalculator class.

Problem statement

In the “Temporal Coupling” lesson of this chapter, a PriceCalculator class was discussed to explain the concept. Later in the lesson, we resolved the issue of temporal coupling by immutability and showed how the constructor of the PriceCalculator and computePrice methods could be rewritten.

Now write the complete implementation of the non-temporally coupled version of the same class.

Requirements

The class’s skeleton, along with its constructor’s implementation, is given below. You only have to write code for the computePrice method using the array_merge and array_map functions.

Note: To test your code, an object of the PriceCalculator class is instantiated with $rates = ['USD' => 3 ,'EURO' => 6,'INR' => 10]. Your provided implementation of the method will be tested against the object.

Sample input

$rates = ['USD' => 3 ,'EURO' => 6,'INR' => 10]
$items = [1,2,3,5]

Expected output

$prices = [  [3,6,10],
             [6,12,20],
             [9,18,30],
             [15,18,50]
          ]

Try it yourself

Get hands-on with 1200+ tech skills courses.