Membuat Program Kasir Sederhana Menggunakan PHP OOP
Pada Postingan sebelum-sebelumnya kita telah membahas mengenai berbagai macam tentang OOP di PHP. Seperti mendifinisikan Class, membuat Function/Method, Mengakses Properties, Instantisasi Object atau Pemanggilan Class, menjalanakan Method, membuat Modularitas Class, membuat Konstruktor, dan lain-lain.
Baiklan langsung saja kita masuk kepembahasan. Disini kita akan membuat sebuah program kasir sederhana menggunakan pemrograman PHP berbasis OOP. Disini kita belum menggunakan database karena kita belum membahas materi sampai ke database menggunakan OOP.
Pertama buatlah terlebih dahulu sebuah file php dengan nama function.php dan index.php. Selanjutnya buka file function.php tadi, disana kita akan membuat coding untuk class dan method-methodnya.
Silahkan masukkan coding berikut ke dalam file function.php.
<?php
class Produk {
public $pc;
public $laptop;
public $jmlLaptop;
public $jmlPc;
public $hargaLaptop;
public $hargaPc;
public $totalSeluruh;
public $totalHargaPC;
public $totalHargaLaptop;
function __construct() {
$this->hargaPc = 5000000;
$this->hargaLaptop = 7000000;
}
}
// Copyright © 2019. Erji Ridho Lubis(www.portalcoding.com)
class Jumlah extends Produk {
public function getJumlah($jmlLaptop,$jmlPc){
$this->jmlLaptop = $jmlLaptop;
$this->jmlPC = $jmlPc;
}
public function setHarga() {
$this->totalHargaPC = $this->hargaPc * $this->jmlPC;
$this->totalHargaLaptop = $this->hargaLaptop * $this->jmlLaptop;
$this->totalSeluruh = $this->totalHargaPC + $this->totalHargaLaptop;
}
public function Finaltotal() {
echo "=================&& Struk Total Pembelian $$=================";
echo "<br>";
echo "Harga 1 PC = Rp. ".$this->hargaPc.",-";
echo "<br>";
echo "Jumlah PC yang di beli = ".$this->jmlPC." Set";
echo "<br>";
echo "Total Harga PC = Rp. ".$this->totalHargaPC.",-";
echo "<br>";
echo "<br>";
echo "Harga 1 Laptop = Rp. ".$this->hargaLaptop.",-";
echo "<br>";
echo "Jumlah Laptop yang di beli = ".$this->jmlLaptop." Set";
echo "<br>";
echo "Total Harga Laptop = Rp. ".$this->totalHargaLaptop.",-";
echo "<br><br>";
echo "Total Keseluruhan = Rp. ".$this->totalSeluruh.",-";
echo "<br>";
echo "========================================================";
echo "<br>";
echo "<br>";
echo "Copyright © 2019. Erji Ridho Lubis";
}
}
?>
Pada coding diatas kita membuat 2 buat Class. Selanjutnya silahkan buka file index.php tadi. Di file ini kita gunakan untuk tampilan atau form untuk input nilai barangnya. Dan di file ini juga kita memangil Class Class yang kita gunakan tadi untuk menjalankan method nya.
<?php
include 'function.php';
$jumlah = new Jumlah();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Kasir Indomaret</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.2/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar-inverse" role="navigation" >
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
<span class="sr-only"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://www.portalcoding.com"><i class="fa fa-shopping-cart"></i> IndomaretKu</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="navbar">
<ul class="nav navbar-nav">
<li class="active"><a href="index.php"><i class="fa fa-home"></i> Beranda</a></li>
<li><a href="#" data-toggle="modal" data-target="#buy"><i class="fa fa-shopping-cart"></i> Buy</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"></a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container" style="margin-top:50px;">
<div class=" panel-success">
<div class="panel-body bg-primary">
<div class="container">
<h1><i class="fa fa-shopping-cart"></i> Selamat datang di Indomaret</h1>
<h2> Selamat berbelanja :)</h2>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<div class="container">
<div class="col-md-10">
<h4>Klik disini untuk pembelian.</h4>
</div>
<div class="col-md-3 ">
<button type="button" class="btn btn-success" name="button" data-toggle="modal" data-target="#buy"><i class="fa fa-shopping-cart"></i> Beli</button>
</div>
</div>
</div>
</div>
<!-- Copyright © 2019. Erji Ridho Lubis(www.portalcoding.com) -->
<div class="panel panel-default">
<div class="panel-body">
<div class="container">
<?php
if (isset($_POST['check'])) {
$jmlLaptop = $_POST['laptop'];
$jmlPc = $_POST['komputer'];
if ($jmlLaptop == null) {
$jumlah->getJumlah(0,$jmlPc);
} elseif ($jmlPc == null) {
$jumlah->getJumlah($jmlLaptop,0);
} else {
$jumlah->getJumlah($jmlLaptop,$jmlPc);
}
$jumlah->setHarga();
$jumlah->Finaltotal();
}
?>
</div>
</div>
</div>
</div>
<br>
<br>
<!-- [Modal Form] -->
<div class="modal fade" id="buy" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header bg-danger" style="border-radius: 5px 5px 0px 0px;">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="">Form pembelian</h4>
</div>
<div class="modal-body">
<form class="form-group" action="" method="post">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-desktop"></i></span>
<input type="number" class="form-control" name="komputer" id="komputer" placeholder="Masukkan Jumlah PC Yang Dibeli *" readOnly>
</div>
<br>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-laptop"></i></span>
<input type="number" class="form-control" name="laptop" id="laptop" placeholder="Masukkan Jumlah Laptop Yang Dibeli *" readOnly>
</div>
</div>
<div class="modal-footer">
<button type="button" id="btnlaptop" onclick="OnlyLaptop()" class="btn btn-success" style="float:left;">Hanya Laptop</button>
<button type="button" id="btnkomputer" onclick="OnlyKomputer()" class="btn btn-success" style="float:left;"> Hanya PC</button>
<button type="button" onclick="Keduanya()" class="btn btn-success" style="float:left;"> Laptop & PC</button>
<button type="button" onclick="exit()" class="btn btn-default" data-dismiss="modal"><i class="fa fa-close"></i> Close</button>
<button type="submit" class="btn btn-primary" name="check"><i class="fa fa-check"></i> Cek Total</button>
</form>
</div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>
<script type="text/javascript">
function OnlyLaptop() {
document.getElementById("laptop").readOnly = false;
document.getElementById("komputer").readOnly = true;
document.getElementById("btnkomputer").disabled = false;
document.getElementById("btnlaptop").disabled = true;
}
function OnlyKomputer() {
document.getElementById("laptop").readOnly = true;
document.getElementById("komputer").readOnly = false;
document.getElementById("btnkomputer").disabled = true;
document.getElementById("btnlaptop").disabled = false;
}
function Keduanya() {
document.getElementById("laptop").readOnly = false;
document.getElementById("komputer").readOnly = false;
document.getElementById("btnkomputer").disabled = false;
document.getElementById("btnlaptop").disabled = false;
}
function exit() {
document.getElementById("laptop").readOnly = true;
document.getElementById("komputer").readOnly = true;
}
</script>
Berikut ini adalah tampilan dari coding diatas.
Kalian bisa mendownload file dari coding diatas. Klik disini.
Sekian dulu untuk pembahasan kali ini,jangan lupa dipelajari dan di praktekkan agar lebih menguasai materinya. Jangan lupa komentar dan shere artikel ini agar lebih bermanfaat, terima kasih.
0 Response to "Membuat Program Kasir Sederhana Menggunakan PHP OOP"
Post a Comment