|
<?php |
|
/** |
|
|
|
evolvingweb question one(1), how you code and what drives your decisions while coding |
|
|
|
**/ |
|
|
|
|
|
|
|
// Interface for defining the structure of different discount strategies |
|
interface DiscountStrategy |
|
{ |
|
/** |
|
* Apply discount to the total amount. |
|
* |
|
* @param float $totalAmount The original total amount before discount. |
|
* @return float The total amount after discount is applied. |
|
*/ |
|
public function applyDiscount(float $totalAmount): float; |
|
} |
|
|
|
// Percentage-based discount strategy |
|
class PercentageDiscount implements DiscountStrategy |
|
{ |
|
protected $percentage; |
|
|
|
public function __construct($percentage) |
|
{ |
|
$this->percentage = $percentage; |
|
} |
|
|
|
/** |
|
* Apply a percentage discount to the total amount. |
|
* |
|
* @param float $totalAmount The original total amount before discount. |
|
* @return float The total amount after applying the percentage discount. |
|
*/ |
|
public function applyDiscount(float $totalAmount): float |
|
{ |
|
return $totalAmount - ($totalAmount * $this->percentage / 100); |
|
} |
|
} |
|
|
|
// Fixed amount discount strategy |
|
class FixedAmountDiscount implements DiscountStrategy |
|
{ |
|
protected $amount; |
|
|
|
public function __construct($amount) |
|
{ |
|
$this->amount = $amount; |
|
} |
|
|
|
/** |
|
* Apply a fixed amount discount to the total amount. |
|
* |
|
* @param float $totalAmount The original total amount before discount. |
|
* @return float The total amount after applying the fixed amount discount. |
|
*/ |
|
public function applyDiscount(float $totalAmount): float |
|
{ |
|
return max(0, $totalAmount - $this->amount); |
|
} |
|
} |
|
|
|
// Service to handle applying discounts |
|
class DiscountService |
|
{ |
|
protected $discount; |
|
|
|
public function __construct(DiscountStrategy $discount) |
|
{ |
|
$this->discount = $discount; |
|
} |
|
|
|
/** |
|
* Apply the discount strategy to the given total amount. |
|
* |
|
* @param float $totalAmount The original total amount before discount. |
|
* @return float The final amount after discount is applied. |
|
*/ |
|
public function apply(float $totalAmount): float |
|
{ |
|
return $this->discount->applyDiscount($totalAmount); |
|
} |
|
} |
|
|
|
// Example usage: |
|
// Assume the total amount is $100.00 and we're applying a 20% discount. |
|
|
|
$totalAmount = 100.0; |
|
$discountCode = 'SUMMER2024'; |
|
|
|
// Using the PercentageDiscount strategy |
|
$percentageDiscount = new PercentageDiscount(20); |
|
$discountService = new DiscountService($percentageDiscount); |
|
$finalAmount = $discountService->apply($totalAmount); |
|
echo "Total after 20% discount: $" . $finalAmount . "\n"; |
|
|
|
// Using the FixedAmountDiscount strategy |
|
$fixedDiscount = new FixedAmountDiscount(10); |
|
$discountService = new DiscountService($fixedDiscount); |
|
$finalAmount = $discountService->apply($totalAmount); |
|
echo "Total after $10 discount: $" . $finalAmount . "\n"; |