Skip to content

Instantly share code, notes, and snippets.

@praswicaksono
Created June 7, 2016 10:00
Show Gist options
  • Select an option

  • Save praswicaksono/316c9628378e06c8b5a4cd42999982cb to your computer and use it in GitHub Desktop.

Select an option

Save praswicaksono/316c9628378e06c8b5a4cd42999982cb to your computer and use it in GitHub Desktop.
<?php
class Pelanggan
{
}
class Roti
{
private $jumlah;
private $harga = 2000;
public function __construct(int $jumlah)
{
$this->jumlah = $jumlah;
}
public function beli() : Invoice
{
return Invoice::create($this);
}
public function jumlah() : int
{
return (int) $this->jumlah;
}
public function harga() : int
{
return (int) $this->harga;
}
}
class Invoice
{
private $total;
private $diskon = 10;
public static function create(Roti $roti)
{
$object = new self();
$object->total = $roti->harga() * $roti->jumlah();
if ($roti->jumlah() > 5) {
$object->total -= $object->total * ($object->diskon/100);
}
return $object;
}
public function total() : int
{
return $this->total;
}
}
$pelanggan = new Pelanggan();
// Skenario 2: Beli 2 roti total invoice harusnya 4000 tidak mendapat diskon
$roti = new Roti(2);
$invoice = $roti->beli();
if ($invoice->total() === 4000) {
echo "Skenario 1 berhasil". PHP_EOL;
}
// Skenario 2: Beli 10 roti total invoice harusnya 18000 (sudah didiskon 10%) jika beli roti > 5
$roti = new Roti(10);
$invoice = $roti->beli();
if ($invoice->total() === 18000) {
echo "Skenario 2 berhasil". PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment