04. SOLID Principles: Order Discount Calculator 04. SOLID Principles: Order Discount Calculator
Medium
+25 Points
Deskripsi Tantangan Problem Description
Buat fungsi `calculateFinalPrice($subtotal, $membership)` dengan aturan: Jika membership `"PREMIUM"` berikan diskon 20%, jika `"MEMBER"` berikan diskon 10%, jika `"GUEST"` diskon 0%. Jika total setelah diskon >= 500000, berikan potongan tambahan flat Rp 25.000.
Create a function `calculateFinalPrice($subtotal, $membership)` with rules: PREMIUM gets 20% discount, MEMBER gets 10%, GUEST gets 0%. If total after percentage discount is >= 500,000, apply an extra flat 25,000 discount.
Example Test Cases
Input: [100000,"PREMIUM"]
Expected Output: 80000
Input: [600000,"MEMBER"]
Expected Output: 515000
- Hitung persentase diskon terlebih dahulu.
- Cek apakah subtotal setelah persentase diskon >= 500000 untuk memberikan potongan ekstra 25000.
- Calculate percentage discount first.
- Check if subtotal after percentage discount is >= 500000 to apply extra 25000 deduction.
<?php
function calculateFinalPrice(float $subtotal, string $membership): float {
$rate = match(strtoupper($membership)) {
'PREMIUM' => 0.20,
'MEMBER' => 0.10,
default => 0.0,
};
$discounted = $subtotal - ($subtotal * $rate);
if ($discounted >= 500000) {
$discounted -= 25000;
}
return $discounted;
}
solution.php
Test Case #
Input:
Expected:
Actual Output:
Error: