ph4x35ccb

POO encapsulamento

Mar 17th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Aula 06b-Encapsulamento</title>
  5. </head>
  6. <body>
  7. <h1>Curso POO - 06b - Encapsulamento</h1>
  8. <h2>Projeto Controle Remoto</h2>
  9. <?php
  10. echo "<pre><b>";
  11. require_once 'controleremoto.php';
  12. $c= new ControleRemoto();
  13. $c->ligar();
  14. $c->maisVolume();
  15. $c->abrirMenu();
  16. echo "<br><br>";
  17. //print_r($c);
  18. echo "</b></pre>";
  19. ?>
  20.  
  21. </body>
  22. </html>
  23. funcoes da interface
  24. salvar controlador.php
  25. <?php
  26.  
  27. interface Controlador{
  28.     public function ligar();
  29.     public function desligar();
  30.     public function abrirMenu();
  31.     public function fecharMenu();
  32.     public function maisVolume();
  33.     public function menosVolume();
  34.     public function ligarMudo();
  35.     public function desligarMudo();
  36.     public function play();
  37.     public function pause();
  38. }
  39.  
  40. ?>
  41.  
  42. classe salvar controleremoto.php
  43.  
  44. <?php
  45. require_once 'controlador.php';
  46.  
  47. class ControleRemoto implements Controlador{
  48.     private $volume;
  49.     private $ligado;
  50.     private $tocando;
  51.  
  52.  
  53.  
  54.     public function __construct(){
  55.         $this->volume=50;
  56.         $this->ligado=false;
  57.         $this->tocando=false;
  58.     }
  59.    
  60.  
  61.  
  62.     private function getVolume(){
  63.         return $this->volume;
  64.     }
  65.     private function setVolume($volume){
  66.         $this->volume=$volume;
  67.     }
  68.     private function getLigado(){
  69.         return $this->ligado;
  70.     }
  71.     private function setLigado($ligado){
  72.         $this->ligado=$ligado;
  73.     }
  74.     private function getTocando(){
  75.         return $this->tocando;
  76.     }
  77.     private function setTocando($tocando){
  78.         $this->tocando=$tocando;
  79.     }
  80.  
  81.  
  82.     public function ligar(){
  83.         $this->setLigado(true);
  84.     }
  85.     public function desligar(){
  86.         $this->setLigado(false);
  87.     }
  88.     public function abrirMenu(){
  89.         echo "[---MENU--]";
  90.         echo "<br>Esta Ligado?:".($this->getLigado()?"SIM":"NÃO");
  91.         echo "<br>Esta Tocando?:".($this->getTocando()?"SIM":"NÃO");
  92.         echo "<br>Volume:".$this->getVolume();
  93.         for($i=0;$i<=$this->getVolume();$i++){
  94.             echo "|";
  95.         }
  96.     }
  97.     public function fecharMenu(){
  98.         echo "Fechando Menu";
  99.     }
  100.     public function maisVolume(){
  101.         if($this->getLigado()){
  102.             $this->setVolume($this->getVolume()+5);
  103.         }
  104.     }
  105.     public function menosVolume(){
  106.         if($this->getLigado()){
  107.             $this->setVolume($this->getVolume()-5);
  108.         }
  109.     }
  110.     public function ligarMudo(){
  111.         if($this->getLigado() && $this->getVolume()>0){
  112.             $this->setVolume(0);
  113.         }  
  114.     }
  115.     public function desligarMudo(){
  116.         if($this->getLigado() && $this->getVolume()==0){
  117.             $this->setVolume(50);
  118.         }
  119.     }
  120.     public function play(){
  121.         if($this->getLigado() && !($this->getTocando())){
  122.             $this->setTocando(true);
  123.         }
  124.     }
  125.     public function pause(){
  126.          if($this->setLigado() && $this->setTocando()){
  127.             $this->setTocando(false);
  128.          }
  129.     }
  130. }
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment