Setter Getter

Run Settings
LanguagePHP
Language Version
Run Command
<?php class Produk { private $judul; private $penulis; private $penerbit; private $harga; private $diskon = 0; // 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 setJudul($judul){ // if(!is_string($judul)){ // throw new exception("Judul harus string"); // } $this->judul = $judul; } public function getJudul(){ return $this->judul; } public function setPenulis($penulis){ $this->penulis = $penulis; } public function getPenulis(){ return $this->penulis; } public function setPenerbit($penerbit){ $this->penerbit = $penerbit; } public function getPenerbit(){ return $this->penerbit; } public function setDiskon($diskon){ $this->diskon = $diskon; } public function getDiskon(){ return $this->diskon; } public function setHarga($harga){ $this->harga = $harga; } 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 : ". parent::getInfoProduk() . " - {$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(); $produk1->setPenulis("Alfrenza"); echo $produk1->getPenulis();
Editor Settings
Theme
Key bindings
Full width
Lines