PHP, UML, class diagrams: I do not know if I'm getting it right ... or maybe I do not understand my teacher

1

I am making a list of exercises that my teacher asked for. But I do not know if I'm correctly interpreting the diagrams that it passes.

I can put methods other than those that are already described in the diagram, I can put attributes, I can put other classes, the constructors are in the way that I think most convenient considering that the teacher did not determine in the diagram?

Finally, one of the teacher's questions and my resolution follows:

index.php

require_once'Contatinho.php';require_once'MsgTexto.php';require_once'Whatsapp.php';require_once'MsgAudio.php';$contatos=array();$contatos[]=newContatinho("Pedro", "(00) 8888-7777");
$contatos[] = new Contatinho("Fabricio", "(00) 7777-6666");
$contatos[] = new Contatinho("Leonardo", "(00) 5555-4444");

$mensagens = array();
$mensagens[] = new MsgTexto("1111", $contatos[0], "13:56", "Olá pessoa");
$mensagens[] = new MsgAudio(180, $contatos[1], "21:06", "Olá ser humano");
$mensagens[] = new MsgAudio(2400, $contatos[2], "08:20", "Tchau");

$whatsapp1 = new Whatsapp($contatos, $mensagens);

$whatsapp1->listarContatos();
$whatsapp1->listarMensagens();

Contatinho.php

class Contatinho {
    private $nome;
    private $celular;

    function __construct($nome, $celular) {
        $this->nome = $nome;
        $this->celular = $celular;
    }

    public function getNome() {
        return $this->nome;
    }
    public function getCelular() {
        return $this->celular;
    }
}

Message.php

abstract class Mensagem {
    protected $destinatario;
    protected $horaEnvio;
    protected $conteudo;

    function __construct($destinatario, $horaEnvio, $conteudo) {
        $this->destinatario = $destinatario;
        $this->horaEnvio = $horaEnvio;
        $this->conteudo = $conteudo;
    }

    function toString(){
        $msgString = "";
        $msgString .= "<br>Tipo: ";
        if($this instanceof MsgTexto){
            $msgString .= "Texto";
        }
        if($this instanceof MsgAudio){
            $msgString .= "Audio";
        }
        $msgString .= "<br>Destinatário nome: ".$this->destinatario->getNome();
        $msgString .= "<br>Destinatário celular: ".$this->destinatario->getCelular();
        $msgString .= "<br>Hora de envio: ".$this->horaEnvio;
        $msgString .= "<br>Hora de envio: ".$this->conteudo;

        return $msgString;
    }
}

MsgAudio.php

require_once 'Mensagem.php';

class MsgAudio extends Mensagem {
    private $duracao;

    function __construct($duracao, $destinatario, $horaEnvio, $conteudo){
        parent::__construct($destinatario, $horaEnvio, $conteudo);
        $this->duracao = $duracao;
    }

    public function __toString(){
        $msgAudioString = "";
        $msgAudioString .= parent::toString();
        $msgAudioString .= "<br>Duração (segundos): ".$this->duracao;

        return $msgAudioString;
    }
}

MsgTexto.php

require_once 'Mensagem.php';

class MsgTexto extends Mensagem {
    private $numchar;

    function __construct($numchar, $destinatario, $horaEnvio, $conteudo){
        parent::__construct($destinatario, $horaEnvio, $conteudo);
        $this->numchar = $numchar;
    }

    public function __toString(){
        $msgTextoString = "";
        $msgTextoString .= parent::toString();
        $msgTextoString .= "<br>Número de caracteres: ".$this->numchar;

        return $msgTextoString;
    }
}

Whatsapp.php

require_once 'Contatinho.php';
require_once 'MsgTexto.php';
require_once 'Mensagem.php';

class Whatsapp {
    private $contatos;
    private $mensagens;

    function __construct($contatos, $mensagens) {
        $this->contatos = $contatos;
        $this->mensagens = $mensagens;
    }

    public function listarContatos(){
        echo "<br><br>----- CONTATOS -----";
        foreach($this->contatos as $contato){
            echo "<br>";
            echo "<br>Nome: ".$contato->getNome();
            echo "<br>Celular: ".$contato->getCelular();
        }
    }

    public function listarMensagens(){
        echo "<br><br>----- MENSAGENS -----";
        foreach($this->mensagens as $mensagem){
            echo "<br>";
            echo $mensagem->__toString();
        }
    }
}

Note: I did not put the MsgFoto code, because it follows the same pattern as other messages.

    
asked by anonymous 30.09.2018 / 01:51

0 answers