Kealin's Custom Coffee Builder
Base Price: $4.5
Toppings Cost: $2
Your Coffee Receipt
Drink: MEDIUM Latte
Toppings: caramel, extra shot
Total: $6.50
CODE FOLLOWS
<?php
include('includes/header.php');
error_reporting(E_ALL);
echo("<h1>Kealin's Custom Coffee Builder </h1>");
/* FUNCTION 1: Base price calculator */
function calculateBasePrice($size) {
if ($size == "small") return 3.00;
if ($size == "medium") return 4.50;
if ($size == "large") return 6.00;
return 0;
}
/* FUNCTION 2: Add toppings cost */
function addToppings($toppings) {
$cost = 0;
foreach ($toppings as $t) {
if ($t == "caramel") $cost += 0.75;
if ($t == "whipped cream") $cost += 0.50;
if ($t == "extra shot") $cost += 1.25;
}
return $cost;
}
/* FUNCTION 3: Format drink name */
function formatDrink($size, $name) {
return strtoupper($size) . " " . ucfirst($name);
}
/* FUNCTION 4: Generate receipt (HTML output) */
function buildReceipt($drink, $toppings, $total) {
$toppingList = implode(", ", $toppings);
return "
<div style='border:2px solid #6b4f4f; padding:15px; width:300px; background:#f8f4f0;'>
<h3>Your Coffee Receipt</h3>
<p><strong>Drink:</strong> $drink</p>
<p><strong>Toppings:</strong> $toppingList</p>
<p><strong>Total:</strong> $" . number_format($total, 2) . "</p>
</div>
";
}
/* SAMPLE DATA (you can turn this into a form later if you want) */
$size = "medium";
$drinkName = "latte";
$toppings = array("caramel", "extra shot");
/* USE FUNCTIONS */
$base = calculateBasePrice($size);
$toppingCost = addToppings($toppings);
$total = $base + $toppingCost;
$finalDrink = formatDrink($size, $drinkName);
echo("<p><strong>Base Price:</strong> $$base</p>");
echo("<p><strong>Toppings Cost:</strong> $$toppingCost</p>");
echo buildReceipt($finalDrink, $toppings, $total);
/* Back link (IMPORTANT for your grade) */
echo('<p><a href="index.php">Back to Tedd\'s Work</a></p>');
include('includes/footer.php');
?>