What should I do to make this program work? POO JAVA

0

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.

I must do it following this annex:

The problem is that when I follow the instructions of the exercise it does not work, or it goes empty for example when I call the deposit method, I do not get what I deposit, it appears "I deposit:".

This is the code you made:

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 = 0.0; // Cuando se crea una cuenta, su saldo es cero.

}

//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 : ");

}


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

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

    }

    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.depositar(14);

}

}
    
asked by computer96 30.10.2018 в 19:06
source

2 answers

3

Your method of depositing effectively does not print any value, because you are simply not telling it to print it.

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

Notice that your println, prints what you decide that prints, but you need to pass more things if you want to print.

For example, if you want to print the amount you are spending, you should do something like:

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

In that line, Java will make an implicit conversion of your value, and add it to the string to be printed.

Do not check the rest of your code, just look at the part you marked as an error.

    
answered by 30.10.2018 в 19:14
1

I am going to leave you the 2 Classes implemented in my own way based on the scheme you have presented. Which by the way, you should review better, since I see that you do not pass required parameters.

Class Account ..

public class Cuenta {
    private String titular;
    private double saldo;

    public Cuenta(String titular){
        this.titular = titular;
        this.saldo = 0.0;
    }

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

    public String getTitular() {
        return titular;
    }

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

    public double getSaldo() {
        return saldo;
    }

    @Override
    public String toString() {
        return "Cuenta{" + "titular=" + titular + ", saldo=" + saldo + '}';
    }

    public void depositar(double cantidad) {
        saldo += cantidad;
    }

    public void retirar(double cantidad) {
        if (cantidad <= saldo) {
            saldo -= cantidad;
            System.out.print("Retiraste " + cantidad + " de tu cuenta. ");
            System.out.println("Tu saldo actual es de: " + saldo);
        } else {
            System.out.println("Quieres retirar mas dinero de lo permitido..");
        }

    }

}

Class Test Account.

public class PruebaCuenta {
    public static void main(String[] args) {
        Cuenta cuenta1 = new Cuenta("El Nombre");
        Cuenta cuenta2 = new Cuenta("José", 10);
        cuenta1.getSaldo();
        System.out.println(cuenta1);
        cuenta2.depositar(14);
        System.out.println(cuenta2);
        cuenta2.retirar(23);
    }
}

Result:

Cuenta{titular=El Nombre, saldo=0.0}
Cuenta{titular=José, saldo=24.0}
Retiraste 23.0 de tu cuenta. Tu saldo actual es de: 1.0

Problems you have ..

  

public Account () {}

This is the first one. A Constructor is required to receive the name of the owner, and we also give you an initial salary of 0 as requested.

public Cuenta(String titular){
    this.titular = titular;
    this.saldo = 0.0;
}

Next problem in the 2nd Constructor ..

  

public Account (String holder, double balance_incial) {

     

this.titular = owner;

     

this.salt = 0.0; // When you create an account, your balance is zero.

     

}

Here you should pass the value of balance_inicial to your variable balance

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

Next problem this ..

  

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

This requires that we pass the titling parameter as well ..

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

And finally these 2 methods 'deposit () and withdraw ()' , in which we add quantity or withdraw. I leave them implemented in my own way, in which we add or subtract that amount to our current balance ..

    public void depositar(double cantidad) {
        saldo += cantidad;
    }

    public void retirar(double cantidad) {
        if (cantidad <= saldo) {
            saldo -= cantidad;
            System.out.print("Retiraste " + cantidad + " de tu cuenta. ");
            System.out.println("Tu saldo actual es de: " + saldo);
        } else {
            System.out.println("Quieres retirar mas dinero de lo permitido..");
        }

    }
    
answered by 03.11.2018 в 11:13