<?php
class Produk {
public $judul;
public $penulis;
public $penerbit;
protected $diskon = 0;
private $harga;
// public $tipe;
public function __construct($judul = "judul", $penulis = "penulis", $penerbit = "penerbit", $harga = 0){
$this->judul = $judul;
$this->penulis = $penulis;
$this->penerbit = $penerbit;
$this->harga = $harga;
// $this->jumlahHalaman = $jumlahHalaman;
// $this->waktuMain = $waktuMain;
// $this->tipe = $tipe;
}
public function setDiskon($diskon){
$this->diskon = $diskon;
}
public function getHarga(){
return $this->harga - ($this->harga * $this->diskon / 100) ;
}
public function getLable(){
return "$this->penulis, $this->penerbit";
}
public function getInfoProduk(){
$string = "{$this->judul} | {$this->getLable()} ({$this->harga})";
// if($this->tipe == "komik"){
// $string .= "{$this->jumlahHalaman} Halaman.";
// }elseif($this->tipe == "game"){
// $string .= "{$this->waktuMain} Jam.";
// }
return $string;
}
}
class Komik extends Produk{
public $jumlahHalaman;
public function __construct($judul = "judul", $penulis = "penulis", $penerbit = "penerbit", $harga = 0,
$jumlahHalaman = 0,) {
// $this->judul = $judul;
// $this->penulis = $penulis;
parent::__construct($judul, $penulis, $penerbit, $harga);
$this -> jumlahHalaman = $jumlahHalaman;
}
public function getInfoProduk(){
$string = "Komik : " . parent::getInfoProduk() . " - {$this->jumlahHalaman} Halaman";
return $string;
}
}
class Game extends Produk{
public $waktuMain;
public function __construct($judul = "judul", $penulis = "penulis", $penerbit = "penerbit", $harga = 0,
$waktuMain = 0) {
parent::__construct($judul, $penulis, $penerbit , $harga);
$this -> waktuMain = $waktuMain;
}
public function getInfoProduk(){
$string = "Game : {$this->judul} | {$this->getLable()} ({$this->harga}) ~ {$this->waktuMain} Jam";
return $string;
}
}
class CetakInfoProduk{
public function cetak(Produk $produk){
$string = "{$produk->judul} | {$produk->getLable()}, harga = {$produk->harga}";
return $string;
}
}
$produk1 = new Komik("Naruto", "Masashi Kihsimoto", "Shonen Jump", 35000, 100);
$produk2 = new Game("Harry Potter", "Jk Rowling", "Warner Bross", 250000, 200);
// echo $produk1->getLable();
// echo $produk2->getLable();
$cetakInfoProduk = new CetakInfoProduk();
// echo $cetakInfoProduk->cetak($produk1);
// echo $cetakInfoProduk->cetak($produk2);
// echo $produk1->getInfoProduk();
// echo $produk2->getInfoProduk();
$produk2->setDiskon(123);
echo $produk2->getHarga();