1
The method sacar()
does not work, does not change the value of the balance.
I make the deposit using super class method ContaBancaria
direct, the sacar()
is of class ContaPoupanca
that extends the super class, but I say, super.getSaldo()
and yet the value of the loot is not deducted.
If I use this.getSaldo()
or super.getSaldo()
the Eclipse shows that the reference is in the class ContaBancaria
, but subtraction doesn’t happen when I run.
public class ContaBancaria {
private double saldo;
public double getSaldo() {
return saldo;
}
public void setSaldo(double saldo) {
this.saldo = saldo;
}
public void depositar(double valor) {
this.saldo += valor;
}
@Override
public String toString() {
return "Saldo: " + saldo;
}
}
public class ContaPoupanca extends ContaBancaria{
private int diaRendimento;
public int getDiaRendimento() {
return diaRendimento;
}
public void setDiaRendimento(int diaRendimento) {
this.diaRendimento = diaRendimento;
}
public void sacar(double valor) {
if (valor < super.getSaldo()) {
super.setSaldo(super.getSaldo() - valor);
System.out.println("Saque realizado com sucesso!\nSaldo: " + super.getSaldo());
} else {
System.out.println("Saldo insuficiente.\nSaldo: " + super.getSaldo());
}
}
public void calcularNovoSaldo(int dia) {
if (dia >= 15) {
System.out.println("Rendimento: " + (this.getSaldo() + 100));
} else {
System.out.println("Sem rendimento. Saldo: " + this.getSaldo());
}
}
}
public class Ex01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ContaBancaria conta = new ContaBancaria();
ContaPoupanca poupanca = new ContaPoupanca();
boolean sair = false;
while (!sair) {
System.out.println("1. Depositar");
System.out.println("2. Sacar");
System.out.println("3. Saldo em conta");
System.out.print("0. Sair\nDigite a opção: ");
int opcao = scan.nextInt();
System.out.println();
if (opcao == 1) {
System.out.print("Digite o valor do depósito: ");
conta.setSaldo(scan.nextDouble());
} else if (opcao == 2) {
System.out.print("Digite o valor do saque: ");
double valor = scan.nextDouble();
poupanca.sacar(valor);
} else if (opcao == 3) {
System.out.println(conta);
} else if (opcao == 0) {
System.out.println("Saindo.");
sair = true;
}
System.out.println();
}
scan.close();
}
It doesn’t work either. What I think is happening is that he is trying to mess with the Savings Account balance, but I’m saying in the code that is for him to subtract from the Account.
– Vinícius Moraes
I tested here and it is working perfectly your code... I simplified your test to be fully java:
public static void main(String[] args) {
 ContaPoupanca c = new ContaPoupanca();
 c.depositar(100);
 c.sacar(10);
 System.out.println(c);
 }
and the result is correct– Jefferson Quesado