Quick actions

cmd+k|ctrl+k

Navigation

Languages

Visibility

Snippet info

Language

Php

Visibility

public

Author

alfrenzaburhannudin

Created

2024-02-26T16:27:07.012548Z

Updated

2024-02-27T14:39:41.348948Z

<?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();
INFO