<?php
// Array untuk menyimpan data beberapa barang (nama, harga, jumlah)
$barang = [
[ "nama" => "Pc" , "harga" => 5000000 , "jumlah" => 1 ] ,
[ "nama" => "Monitor" , "harga" => 3000000 , "jumlah" => 1 ] ,
[ "nama" => "Joystick" , "harga" => 100000 , "jumlah" => 2 ] ,
[ "nama" => "Mobil" , "harga" => 100000000 , "jumlah" => 1 ]
] ;
// Fungsi untuk menghitung total harga barang
function hitungTotalHarga( $harga , $jumlah , $diskon = 0 , $pajak = 0 ) {
$subtotal = $harga * $jumlah ; // Menghitung subtotal
$potonganDiskon = $subtotal * ( $diskon / 100 ) ; // Diskon
$setelahDiskon = $subtotal - $potonganDiskon ; // Setelah diskon
$jumlahPajak = $setelahDiskon * ( $pajak / 100 ) ; // Pajak
return $setelahDiskon + $jumlahPajak ; // Total
}
// Variabel untuk menyimpan total keseluruhan
$totalKeseluruhan = 0 ;
// Perulangan untuk memproses setiap barang
foreach ( $barang as $item ) {
$nama = $item [ "nama" ] ;
$harga = $item [ "harga" ] ;
$jumlah = $item [ "jumlah" ] ;
// Diskon: Jika harga barang lebih dari 50.000, diskon 10%
$diskon = ( $harga > 50000 ) ? 10 : 0 ;
// Hitung total harga untuk barang ini
$totalBarang = hitungTotalHarga( $harga , $jumlah , $diskon , 5 ) ; // Pajak 5%
$totalKeseluruhan += $totalBarang ; // Tambahkan ke total keseluruhan
// Tampilkan hasil untuk setiap barang
echo "Nama Barang: $nama \n " ;
echo "Jumlah: $jumlah \n " ;
echo "Diskon: $diskon %\n " ;
echo "Total Harga (setelah diskon dan pajak): Rp " . number_format ( $totalBarang , 0 , ',' , '.' ) . "\n \n " ; }
// Tampilkan total keseluruhan
echo "Total Keseluruhan: Rp " . number_format ( $totalKeseluruhan , 0 , ',' , '.' ) . "\n " ; ?>
PD9waHAKLy8gQXJyYXkgdW50dWsgbWVueWltcGFuIGRhdGEgYmViZXJhcGEgYmFyYW5nIChuYW1hLCBoYXJnYSwganVtbGFoKQokYmFyYW5nID0gWw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