Create function remove () [closed]

1

I have a problem with my logic here, I need to create a sacar () function, but it has the following requirements:

• Account needs to exist

• She needs to have a balance greater than 0

• The saqui can not be greater than the value in the account

How do I do this? I tried to do but I'm having a hard time in that part.

My code:

<?php


class ContaNoBanco {
    private $numConta;
    public $tipoConta;
    public $donoConta;
    private $saldoConta;
    private $statusConta;

    private function abrirConta(){
        if($this->statusConta) {
             echo'Você ja tem uma conta aberta, de número: ' . $this->getNumConta();
        }
        else {
            $this->statusConta = true;
        }
        switch ($this->tipoConta) {
            case 0:
                $this->setTipoConta('CC');
                $this->setSaldoConta($this->saldoConta + 50);
            case 1:
                $this->setTipoConta('CP');
                $this->setSaldoConta($this->saldoConta + 150);
            break;
        }
    }

    private function fecharConta(){
        if($this->saldoConta === 0) {
        $this->statusConta = false;
        }
        else if($this->statusConta < 0){
            echo'Você tem R$' . $this->getSaldoConta() . ' débitos a pagar, se regularize antes de fechar sua conta!';
        }
        else{
            echo'Você tem o saldo de R$' . $this->getSaldoConta() . ' saque o dinheiro antes de fechar sua conta!';
        }

    }


    public function depositar() {

    }

    /*public function sacar(){
        if($this->statusConta) {
            if($this->saldoConta > 0){
               if($this->setSaldoConta() > $this->saldoConta){
                   echo'Você não pode sacar mais do que ' . $this->getSaldoConta();
               }
               else {
                   $this->setSaldoConta($this->saldoConta - 50);
               }
            }
        }
        else{
            echo'A conta está fechada!';
        }
    }*/
    public function pagarMensal(){

    }

    function getNumConta() {
        return $this->numConta;
    }

    function getTipoConta() {
        return $this->tipoConta;
    }

    function getDonoConta() {
        return $this->donoConta;
    }

    function getSaldoConta() {
        return $this->saldoConta;
    }

    function getStatusConta() {
        return $this->statusConta;
    }

    function setNumConta($numConta) {
        $this->numConta = $numConta;
    }

    function setTipoConta($tipoConta) {
        $this->tipoConta = $tipoConta;
    }

    function setDonoConta($donoConta) {
        $this->donoConta = $donoConta;
    }

    function setSaldoConta($saldoConta) {
        $this->saldoConta = $saldoConta;
    }

    function setStatusConta($statusConta) {
        $this->statusConta = $statusConta;
    }


}

EDITED

Code 100%

<?php


class ContaNoBanco {

    //Atributos
    public $numConta;
    protected $tipoConta;
    private $donoConta;
    private $saldoConta;
    private $statusConta;

    //Métodos

    public function abrirConta($t){
        $this->setTipoConta($t);
        $this->setStatusConta(true);
        if($t == "CC"){
            $this->setSaldoConta(50);
        }
        else if($t == "CP"){
            $this->setSaldoConta(150);
        }
    }

    public function fecharConta(){
        if($this->getSaldoConta() === 0) {
            $this->setStatusConta(false);
            echo"<p>Conta de {$this->getDonoConta()} fechada com sucesso!</p>";
        }
        else if($this->getSaldoConta() < 0){
            echo"Você tem R$' . $this->getSaldoConta() . ' débitos a pagar, se regularize antes de fechar sua conta!";
        }
        else {
            echo"<p>Voce tem o saldo de R$" . $this->getSaldoConta() . "saque o dinheiro antes de fechar sua conta!<p/>";
        }

    }

    public function depositar($v) {
        if ($this->getStatusConta()){
            $this->setSaldoConta($this->getSaldoConta() + $v);
            echo"<p>Deposito de R$ $v na conta de " . $this->getDonoConta() . "</p>";
        }
        else {
            echo"<p>Você não tem uma conta no banco!</p>";
        }
    }

    public function sacar($v){
        if($this->getStatusConta()) {
            if($this->getSaldoConta() >= $v){
                $this->setSaldoConta($this->getSaldoConta() - $v);
                echo"<p>Saque de R$$v autorizado na conta de " . $this->getDonoConta() . "</p>";
            }
            else {
                echo"<p>Saldo insuficiente!</p>";
            }
        }
        else{
            echo"Essa conta não existe!";
        }
    }

    public function pagarMensal(){
        if($this->getStatusConta() === false) {
            echo"Conta não existe!";
        }
        if($this->getTipoConta() == "CC") {
            $v = 12;
        }
        else if ($this->getTipoConta() == "CP"){
            $v = 20;
        }
        if ($this->getStatusConta()){
            $this->setSaldoConta($this->getSaldoConta() - $v);
            echo"<p>Mensalidade de $v debitada na conta de " .$this->getDonoConta() . "</p>";
        }
        else {
            echo'<p>Problemas com a conta!</p>';
        }
    }

    //Métodos Especiais

    function __construct(){
        $this->setSaldoConta(0);
        $this->setStatusConta(false);
        echo'<p>Conta criada com sucesso!</p>';
    }

    function getNumConta() {
        return $this->numConta;
    }

    function getTipoConta() {
        return $this->tipoConta;
    }

    function getDonoConta() {
        return $this->donoConta;
    }

    function getSaldoConta() {
        return $this->saldoConta;
    }

    function getStatusConta() {
        return $this->statusConta;
    }

    function setNumConta($numConta) {
        $this->numConta = $numConta;
    }

    function setTipoConta($tipoConta) {
        $this->tipoConta = $tipoConta;
    }

    function setDonoConta($donoConta) {
        $this->donoConta = $donoConta;
    }

    function setSaldoConta($saldoConta) {
        $this->saldoConta = $saldoConta;
    }

    function setStatusConta($statusConta) {
        $this->statusConta = $statusConta;
    }
}
    
asked by anonymous 13.06.2017 / 23:01

1 answer

6

Just give you an initial tip. This looks like exercise and it's okay to do so. In real applications that need OOP in general the problem is much more complex than this and the way to do it is quite different.

Even using previous OOP question , this code is conceptually bad because instead of asking to see if each condition is valid ( see ), it keeps calling simple getters methods that do not any gains in this code, worse, mixes the method and the attribute. Internally, there is usually no advantage in accessing attributes by getters and setters methods. As I've spoken before , for public access to scripts the advantage is little, but internally it's zero at all.

Another tip is that 90% of the times we program imperatively, and we must master this very well before leaving for OOP. You can simplify this code when it is prevented for some reason and let it do only if it passes through all the filters. Again the form used works in simple codes, in others it can be very complicated to give maintenance. Curiously, people try to do OOP and can not get benefit from using it because they do not understand what to do, because to use it like that.

I imagine the first requirement is if the account is active. It looks ok in the code.

It seems to me that the comparison if you have balance should be with the amount to be taken out. Where is this value? Should it be a parameter in the sacar() method? And this parameter that should be subtracted from the balance to see if it is greater than 0?

Have you ever thought that this amount of service could be negative and would it be a mistake? You have to check this out too.

Have you also noticed that it does not matter if the balance is greater than 0? If the balance minus the amount to be withdrawn is negative, it is already a sufficient condition to prevent withdrawal.

I do not understand the line $this->setSaldoConta($this->saldoConta - 50); , can only draw 50?

As a last tip, in real application you can not treat monetary value as a normal PHP number that has floating point and generates calculation inaccuracy.

The problem of fictitious exercises is that they do not usually teach anything useful in fact other than some very specific language construction, which does not seem to be the purpose of what was proposed. Outside of this or anything serves or needs to be clear about the requirements.

It could look like this:

public function sacar(){
    if ($this->statusConta) {
        echo 'A conta está fechada!';
        return;
    }
    if ($this->saldoConta - 50 < 0) {
        echo'Você não pode sacar mais do que ' . $this->$saldoConta;
        return;
    }
    $this.saldoConta -= 50;
}
    
13.06.2017 / 23:27