Create pull function()

Asked

Viewed 339 times

1

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

• Account needs to exist

• She needs to have balance greater than 0

• The sake cannot be greater than the amount in the account

How do I do that? I tried to but I’m having a hard time with this 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;
    }
}
  • And what’s the problem, what the doubt?

  • I’m sorry. I just don’t know how to check, when it’s time to draw, to block if it’s bigger than the balance.

  • What is this? $this->setSaldoConta($this->saldoConta - 50); Can you only draw 50? You can’t draw an arbitrary amount?

  • That’s when I open the account. I just forgot +=. It’s just that when I open, the CC already starts with 50 and the CP with 100. Instead of + there is +=

1 answer

6


Just give you an initial hint: this looks like exercise and is ok 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 the previous question about OOP, this code is conceptually bad because instead of asking to see if each condition is valid (see), he keeps calling methods getters simple that do not bring any gains in this code, worse, mix the method and attribute. Internally it doesn’t usually have any advantage of accessing attributes by methods getters and setters. Like I told you before, for public access in scripts the advantage is little, but internally is zero even.

Another tip is that 90% of the time we must program, and you have to master this very well before you leave for OOP. You can simplify that code when it’s stopped for some reason and just let it go through all the filters. Again the form used works in simple codes, in others it can be quite complicated to give maintenance. Interestingly people try to do OOP and fail to get benefit from its use by not understanding what to do, why use so.

As a last hint, in real application you cannot treat monetary value as a normal PHP number that has floating point and generates calculation inaccuracies.

The problem with fictitious exercises is that they don’t usually teach anything useful in fact other than some very specific language construction, which doesn’t seem to be the goal of what was proposed. Apart from this or anything it serves or needs to be clear the requirements.

The problem

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

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

Have you ever thought that this loot value might come negative and this would be a mistake? You have to check this too.

Did you also notice that it doesn’t matter if the balance is greater than 0? If the balance less the amount to be drawn is negative it is already sufficient condition to prevent the withdrawal.

I didn’t understand the line $this->setSaldoConta($this->saldoConta - 50);, can only draw 50?

It could be so:

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;
}

I put in the Github for future reference.

  • Yes, it’s a video course exercise by Professor Guanabara. It’s a theoretical class, he was talking and I was typing the code. But I’m learning the Getters, Setters and Constructor, so I did the job, put is what he’s taught so far. About checking if it is greater than 0, etc., I even tried, it is commented, but it did not work very well.

  • 1

    That’s what I said, to wear something you don’t need, is to learn wrong.

  • Arriving at my work tomorrow, I will see by computer, your moustache code, but from now, thank you very much!!!

  • Yes, he did not teach to do so in the case, he is teaching about the methods. So I put, but thanks, again, tomorrow I can see!

  • Good morning bigown, now I’m on the computer. Your code worked, thank you!!

  • 2

    @Lucascarvalho great, just note that to succeed and be right is something very different. I still think it’s not right, just gave the result you expect :)

  • Could you give an example of what it would be like to be right? It doesn’t have to be my big code no, just some part of it, just so I understand before I continue the course. Please, if it’s no bother.

  • 1

    The problem is, you have to rewrite everything, and I’ve realized that continuing the course that will teach you to do wrong won’t help you, you have to start at the beginning.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.