Why does not the toString method work for me? Poo Java

1

Good the problem is that I deposit 100, then remove 50, and when I want to check my balance (calling the method toString) it puts me 0.0, when I would have to say 60, since in the initial balance I put 10 and deposit 100, what could be the error?

Then I leave the code and the slogan of the exercise:

Implement the Account class, knowing that:

a. When you create an account, your balance is zero.

b. It is only possible to extract an amount less than or equal to the balance in the account.

    public class Cuenta {

    //Atributos

        private String titular;
        private double saldo;


   //Constructor

    public Cuenta() {

    }

    public Cuenta (String titular , double saldo_incial) {
        this.titular=titular;
        this.saldo = saldo; 

    }

    //Metodos

    public String getTitular() {

        return this.titular;
    }

    public void setTitular() {
        this.titular= titular;
    }

    public double getSaldo() {
        return this.saldo;
    }

    public String toString() { 

        return "Titular : "+ this.titular + " saldo : "+ this.saldo;
    }

    public void depositar(double cantidad) {

        System.out.println("Se deposito : "+cantidad);

    }


    public void retirar(double cantidad) {
        if(cantidad>=saldo) {

        System.out.println("Se extrae : "+cantidad);

        }

        else {

System.out.println("Solo es posible extraer un importe menor o igual al saldo que se tenga en la cuenta" );


    }


}


}

-------------

    public class PruebaCuenta {

    public static void main(String[] args) {

        Cuenta cuenta1 = new Cuenta();
        Cuenta cuenta2 = new Cuenta ("jose" , 10);
        cuenta1.getSaldo();
        cuenta2.getSaldo();
        System.out.println(cuenta2.toString());
        cuenta2.depositar(100);
        cuenta2.retirar(50);
        System.out.println(cuenta2.toString());
    }

}
    
asked by computer96 30.10.2018 в 20:29
source

2 answers

4

Neither your method to deposit, nor your method to withdraw do anything.

I would recommend that you start looking for a bit of inforamation about objects, and about variables (or properties) of objects, and language in general.

Your class has a Balance property, which is where you should keep the amount that the account has.

In your constructor, by doing this: this.saldo = saldo; , you are assigning the balance to that account, passing the value that comes as a parameter, to the internal variable (or property) of the account.

Now, that, in your code, you do not do it anymore.

Your method to deposit, or your method to extract, does not do that. That is, they do not update the value of that property.

Your deposit method (after we fix it from the previous question), do the following:

public void depositar(double cantidad) {
    System.out.println("Se deposito : "+cantidad);
}

But, where in all that code are we telling the account to update with the new value ???

As it is to deposit, I should say something like this:

this.saldo = this.saldo + cantidad;

This line tells you that the balance of that object must be equal to the balance you currently have, plus what you have just deposited (the value contained in quantity).

    
answered by 30.10.2018 / 20:38
source
2
  

Why does not the toString method work for me?

Precisely the toString method works perfectly for you.

Where you have the problem is when you assign a value to your variable double saldo; in the constructor with 2 parameters of your class Account .

public Cuenta (String titular , double saldo_incial) {
        this.titular=titular;
        this.saldo = saldo; 

    }

Where you have this.saldo = saldo ; it should be this.saldo = saldo_incial , which is what you're going through in the parameter of this constructor.

Leaving it like that, it works without problems what you want to do ..

public Cuenta(String titular, double saldo_incial) {
        this.titular = titular;
        //this.saldo = saldo;
        this.saldo = saldo_incial;
    }

And also to say, that in System.out.println , the implícita method is already implemented in toString , and it is not necessary to call them ..

System.out.println(cuenta2);
    
answered by 30.10.2018 в 21:58