Quick actions

cmd+k|ctrl+k

Navigation

Languages

Inheritance

Snippet info

Language

Php

Visibility

public

Author

alfrenzaburhannudin

Created

2024-02-15T01:40:56.152287Z

Updated

2024-02-20T23:19:40.076668Z

<?php

class Produk {
    public $judul;
    public $penulis;
    public $penerbit;
    public $harga;
    public $jumlahHalamn;
    public $waktuMain;
    // public $tipe;
    
    public function __construct($judul = "judul", $penulis = "penulis", $penerbit = "penerbit", $harga = 0, $jumlahHalaman = 0,
    $waktuMain = 0){
        $this->judul = $judul; 
        $this->penulis = $penulis; 
        $this->penerbit = $penerbit; 
        $this->harga = $harga;
        $this->jumlahHalaman = $jumlahHalaman;
        $this->waktuMain = $waktuMain;
        // $this->tipe = $tipe;
    }
    
    public function getLable(){
        return "$this->penulis, $this->penerbit";
    }
    
    public function getInfoProduk(){
        $string = "{$this->tipe} : {$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 function getInfoProduk(){
        $string = "Komik : {$this->judul} | {$this->getLable()} ({$this->harga}) - {$this->jumlahHalaman} Halaman";
        
        return $string;
    }
}

class Game extends Produk{
    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, 0);
$produk2 = new Game("Harry Potter", "Jk Rowling", "Warner Bross", "250000", 0, 200);


// echo $produk1->getLable();
// echo $produk2->getLable();

$cetakInfoProduk = new CetakInfoProduk();
// echo $cetakInfoProduk->cetak($produk1);
// echo $cetakInfoProduk->cetak($produk2);

// echo $produk1->getInfoProduk();
echo $produk2->getInfoProduk();
INFO