Completed bonus-assignment.

This commit is contained in:
2025-11-12 10:01:38 +01:00
parent 2c291ba5ce
commit 68f668d80a

102
test.php
View File

@@ -1,50 +1,98 @@
<?php
$liter1 = 40.5;
$liter2 = 35.7;
$preis = 1.499;
$defaultVolume1 = 40.5;
$defaultVolume2 = 35.7;
$defaultPrice = 1.499;
$gesamtLiter = $liter1 + $liter2;
$kostenFuellung1 = $liter1 * $preis;
$kostenFuellung2 = $liter2 * $preis;
$gesamtKosten = $gesamtLiter * $preis;
$readNumber = function (string $key, $default) {
if (!isset($_REQUEST[$key]) || $_REQUEST[$key] === '') {
return $default;
}
$fmtK1 = number_format($kostenFuellung1, 2, ',', '.');
$fmtK2 = number_format($kostenFuellung2, 2, ',', '.');
$fmtKG = number_format($gesamtKosten, 2, ',', '.');
$raw = (string)$_REQUEST[$key];
$norm = str_replace([' ', ','], ['', '.'], $raw);
if (!is_numeric($norm)) {
return $default;
}
$val = (float)$norm;
if ($val < 0) {
$val = 0.0;
}
return $val;
};
$volume1 = $readNumber('volume1', $defaultVolume1);
$volume2 = $readNumber('volume2', $defaultVolume2);
$price = $readNumber('price', $defaultPrice);
$totalVolume = $volume1 + $volume2;
$costFill1 = $volume1 * $price;
$costFill2 = $volume2 * $price;
$totalCost = $totalVolume * $price;
$fmtCostFill1 = number_format($costFill1, 2, ',', '.');
$fmtCostFill2 = number_format($costFill2, 2, ',', '.');
$fmtTotalCost = number_format($totalCost, 2, ',', '.');
$fmtPrice = number_format($price, 3, ',', '.');
?>
<!DOCTYPE html>
<html lang="de">
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tankstellenberechnung (Aufgabe 3)</title>
<title>Fuel Calculation (Task 3)</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<h1>Tankstellenberechnung</h1>
<h1>Fuel Calculation</h1>
<nav>
<p><a href="index.php">Start</a> | <a href="info.php">PHP Info</a></p>
<p><a href="index.php">Home</a> | <a href="info.php">PHP Info</a></p>
</nav>
<p>Füllung 1: <?= $liter1 ?> L</p>
<p>Füllung 2: <?= $liter2 ?> L</p>
<p>Gesamtmenge: <?= $gesamtLiter ?> L</p>
<form action="test.php" method="get" class="calc-form" style="margin-bottom:1rem; display:grid; gap:0.5rem; max-width: 420px;">
<label>
Fill 1 (L)
<input type="number" name="volume1" step="0.1" min="0" value="<?= htmlspecialchars((string)$volume1, ENT_QUOTES) ?>">
</label>
<label>
Fill 2 (L)
<input type="number" name="volume2" step="0.1" min="0" value="<?= htmlspecialchars((string)$volume2, ENT_QUOTES) ?>">
</label>
<label>
Price per liter (€)
<input type="number" name="price" step="0.001" min="0" value="<?= htmlspecialchars((string)$price, ENT_QUOTES) ?>">
</label>
<div style="display:flex; gap:0.5rem;">
<button type="submit">Calculate</button>
<a href="test.php" style="display:inline-block; padding:0.25rem 0.5rem; border:1px solid #ccc; border-radius:4px; text-decoration:none;">Reset</a>
</div>
<small>Note: Comma as decimal separator is allowed (e.g., 40,5).</small>
</form>
<p>Kosten Füllung 1: <?= $fmtK1 ?> </p>
<p>Kosten Füllung 2: <?= $fmtK2 ?> </p>
<p>Gesamtkosten: <strong><?= $fmtKG ?> €</strong></p>
<p>Fill 1: <?= $volume1 ?> L</p>
<p>Fill 2: <?= $volume2 ?> L</p>
<p>Total volume: <?= $totalVolume ?> L</p>
<p>Price per liter: <?= $fmtPrice ?> €</p>
<p>Cost fill 1: <?= $fmtCostFill1 ?> €</p>
<p>Cost fill 2: <?= $fmtCostFill2 ?> €</p>
<p>Total cost: <strong><?= $fmtTotalCost ?> €</strong></p>
<table class="calc">
<thead>
<tr><th>Bezeichnung</th><th>Wert</th></tr>
<tr><th>Label</th><th>Value</th></tr>
</thead>
<tbody>
<tr><td>Füllung 1 (L)</td><td><?= $liter1 ?></td></tr>
<tr><td>Füllung 2 (L)</td><td><?= $liter2 ?></td></tr>
<tr><td>Gesamt (L)</td><td><?= $gesamtLiter ?></td></tr>
<tr><td>Kosten Füllung 1 (€)</td><td><?= $fmtK1 ?></td></tr>
<tr><td>Kosten Füllung 2 (€)</td><td><?= $fmtK2 ?></td></tr>
<tr><td>Gesamtkosten (€)</td><td><strong><?= $fmtKG ?></strong></td></tr>
<tr><td>Fill 1 (L)</td><td><?= $volume1 ?></td></tr>
<tr><td>Fill 2 (L)</td><td><?= $volume2 ?></td></tr>
<tr><td>Total (L)</td><td><?= $totalVolume ?></td></tr>
<tr><td>Price per liter (€)</td><td><?= $fmtPrice ?></td></tr>
<tr><td>Cost fill 1 (€)</td><td><?= $fmtCostFill1 ?></td></tr>
<tr><td>Cost fill 2 (€)</td><td><?= $fmtCostFill2 ?></td></tr>
<tr><td>Total cost (€)</td><td><strong><?= $fmtTotalCost ?></strong></td></tr>
</tbody>
</table>
</body>