How to change the content of an inherited class variable?

Asked

Viewed 234 times

0

I am developing a transaction system for methodological purposes.

I have the following classe mãe calling for Conta:

public class Conta {

    private double _saldo = 0;
    private String _nome;
    private int _idade;
    private int  numeroConta;  
    protected String message;

    Conta(int numeroConta, double saldo) {
        this._saldo += saldo;
        this.numeroConta = numeroConta;
    }
    public String getNome() {
        return "Nome: " + this._nome;
    }    
    public void setNome(String nome) {
        this._nome = nome;
    }
    public int getIdade() {        
        return this._idade;
    }
    public void setIdade(int idade) {
        this._idade = idade;
    }
   public void setDepositar(double money){
        this._saldo += money;
    }
    public void setSacar(double money){
        if (money <= this._saldo) {
            this._saldo -= money;
            this.message = "Saque efetuado!";
           System.out.println(message);
        }      
        else System.out.println("Recusado! Você não possui saldo suficiente!");

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

In the method setSacar perform a check and depending on the result it prints a message to the user.

I have the following classe filha calling for Cartao:

public class Cartao extends Conta {
private int _numeroCartao;
private boolean status  = true;

  Cartao(int numeroConta, double saldo) {
      super(numeroConta,saldo);
  }
public boolean cartaoAtivo(boolean status) {
    if (status) return this.status = true;           

    else return this.status = false;
}
public void comprarProduto(double valor) {
    if (this.status) {            
        this.setSacar(valor); //chamada do metodo
          super.message ="Compra realizada!";// quero alterar essa frase           
    }     
    else System.out.println("Cartão inativo. Compra não realizada!");
}

public void setNumeroCartao(int numero) {
    this._numeroCartao = numero;
}

public int getNumeroCartao() {
    return this._numeroCartao;
}   }

And in my class main I am printing the following:

public class Main {

    public static void main(String[] args) {        
          Cartao c1 = new Cartao(1,500);
          c1.cartaoAtivo(true);
          c1.setNumeroCartao(1);
          c1.comprarProduto(100);//realizando a compra

          c1.setSacar(400);//realizando o saque 
          System.out.println("Seu saldo: R$ " + c1.getSaldo());
          System.out.println("Número do cartão: "+ c1.getNumeroCartao());
    }
}

That’s the way out:

Saque efetuado!
Saque efetuado!
Seu saldo: R$ 0.0
Número do cartão: 1

The point is, I changed the exhibit phrase in the daughter class to Compra realizada! but on the way out, the phrase continues to be drawn from classe mãe.

Someone knows what I’m doing wrong?

  • Card extending account? Your modeling is not cool huh.

  • @Articuno, as I mentioned at the beginning, the project is for study purposes, nor was modelling done.

  • 1

    I read it, and as a hint, I’m telling you it’s not cool. It’s like you’re claiming that cartão é um tipo de conta, but card is card, account is account. And if you want to change superclass behavior, it’s not like that, you need to create a Setter for the field and overwrite it.

  • @Articuno thanks for the tip, what I really wanted to implement was a ContaPoupança but I’m still sorting out a few things to replace with the class Cartao that was created only to try to remedy this doubt that I’m having.

  • @Articuno created Setter, even displays the message of the child class but the message of the mother class is also printed.

  • @Jorgematheus friend, not getting into details of the validity of its architecture. I believe that if you simply change the message before calling the 'setSacar()' method it will reflect the change. The way it is it executes the serve, shows the message, and after that you change the value of the variable 'message'.

  • However. thus, if you call the setSacar() method directly by the Card class after a call from the buyProduct() method, the message will be the same. It will not show the original text.

  • @Israelmerljak yes that’s right, so I wanted to know a method of changing the message instead of showing the same.

  • Even more. It is generally not good practice for you to associate a response to the UI (considering the terminal log as feedback) to a class with business logic. The best would be to return some result of this method setSacar() type a Boolean, and check the success)

  • @Israelmerljak thought the same thing... Can you give me an example to see if it’s the same thing?

  • 4

    You can’t suggest something without messing with the whole structure of the code, there’s no point in a solution and keep doing it wrong. You need to apply the principles of cohesion and encapsulation, otherwise you are learning the wrong way and will continue because forward will be more difficult to learn the right way.

  • Article, thanks for the suggestion. I’ll read later in the reply that contains the link you gave me.

Show 8 more comments
No answers

Browser other questions tagged

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