Skip to content

Instantly share code, notes, and snippets.

@donejeh
Last active August 23, 2024 20:03
Show Gist options
  • Save donejeh/16ae7ed69a8cee063a50c0085d0c8557 to your computer and use it in GitHub Desktop.
Save donejeh/16ae7ed69a8cee063a50c0085d0c8557 to your computer and use it in GitHub Desktop.

Evolvingweb

question one(1) how you code and what drives your decisions while coding.

Discount Service Example

This code demonstrates a simple discount system for an eCommerce platform using the Strategy Pattern in PHP.

Features:

  • Flexible Discount Strategies: The code supports both percentage-based and fixed amount discounts.
  • Extensible Design: New discount types can be added without modifying existing code, adhering to the Open/Closed Principle of SOLID.
  • Usage Examples: The code includes example usage of the DiscountService class with different discount strategies.

Why I Coded It This Way:

  • Strategy Pattern: I chose this pattern to easily swap out discount strategies, making the system more adaptable to future requirements.
  • Best Practices: The code is structured to follow PSR standards and includes clear documentation to ensure maintainability.
<?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";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment