Back to Community
Module 2025-12-17 23:36:21

Mesin Kasir - Pemula

Language: PHP


Description

User di latih untuk memahami konteks coding dan membuat struktur yang tepat

Code Snippet
// File Pertama, // di namakan latihan_mesin.php : 

<!DOCTYPE html>
<html lang="">
<head>
    <title>Mesin Kasir Sederhana</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        .box { border: 1px solid #ccc; padding: 20px; width: 300px; border-radius: 8px; }
        input { width: 100%; margin-bottom: 10px; padding: 5px; }
        button { width: 100%; padding: 10px; background-color: #28a745; color: white; border: none; cursor: pointer; }
    </style>
</head>
<body>

<div class="box">
    <h3>Mesin Kasir PHP</h3>
    <form action="hitung.php" method="">
        <label>Nama Barang:</label>
        <input type="text" name="nama_barang" required>
        
        <label>Harga Satuan:</label>
        <input type="number" name="harga" required>
        
        <label>Jumlah Beli:</label>
        <input type="" name="" required>
        
        <button type="submit">Hitung Total</button>
    </form>
</div>

// master : file di namakan sebagai hitung.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 1. Mengambil data dari POST
    $nama_barang = $_POST['nama_barang'];
    $harga = $_POST['harga'];
    $jumlah = $_POST['jumlah'];

    $total_bayar = $"" * $"";

    $diskon = 0;
    if ($total_bayar > 100000) {
        $diskon = 0.1 * $total_bayar;
    }

    $total_akhir = $total_bayar - $diskon;

    echo "<h2>Struk Pembayaran</h2>";
    echo "Nama Barang: " . htmlspecialchars($nama_barang) . "<br>";
    echo "Harga Satuan: Rp " . number_format($harga, 0, ',', '.') . "<br>";
    echo "Jumlah Beli: " . $jumlah . "<br>";
    echo "--------------------------- <br>";
    echo "Total Kotor: Rp " . number_format($total_bayar, 0, ',', '.') . "<br>";
    echo "Diskon (10% jika > 100rb): Rp " . number_format($diskon, 0, ',', '.') . "<br>";
    echo "<strong>Total yang Harus Dibayar: Rp " . number_format($total_akhir, 0, ',', '.') . "</strong><br>";
    echo "<br><a href=''>Kembali</a>";
} else {
    // Jika akses file ini langsung tanpa POST, arahkan kembali
    header("Location: latihan_mesin.php");
}
?>