Comparison between objects through loop, If and Else?

Asked

Viewed 752 times

1

I’m beginner in java and I’m having doubts in comparing objects by means of loop. I am developing software for a video rental company with Pattern MVC (Model-view-controller) design and I want to know how to compare the person object through his code with zero. When I loop this, it displays the following message: Bad operand types for Binary Operator '>' .

 private boolean salvarPessoa(){
            OPDatas bl = new OPDatas();

            pessoa.setNome(this.txtNome.getText());
            pessoa.setBairro(this.txtBairro.getText());
            pessoa.setEndereco(this.txtEndereco.getText());
            pessoa.setCidade(this.txtCidade.getText());
            pessoa.setUf(this.txtUF.getText());
            pessoa.setCPF(this.txtCPF.getText());
            pessoa.setTelefone(this.txtTelefone.getText());
            pessoa.setdNascimento(bl.converterDataStringParaDate(this.txtdNascimento.getText()));// 



         // Esse é o loop e o objeto pessoa!
            if (pessoaController.salvar(pessoa> 0) {
                JOptionPane.showMessageDialog(this, "Registro gravado com sucesso!");
                this.desabilitarCampos();
                this.carregarClientes();
                return true;
            } else {
                JOptionPane.showMessageDialog(this, "Erro ao gravar os dados!", "ERRO", JOptionPane.ERROR_MESSAGE);
                return false;
            }

         }

Personal controller:

public boolean salvar( String nome, String endereco, String bairro, String sexo, String telefone, String celular, String CPF, Date dNascimento, String cidade, String uf ) {

        boolean retorno;

        Pessoa pessoa = new Pessoa();
        pessoa.setNome(nome);
        pessoa.setEndereco(endereco);
        pessoa.setBairro(bairro);
        pessoa.setSexo(sexo);
        pessoa.setTelefone(telefone);
        pessoa.setCelular(celular);
        pessoa.setCPF(CPF);
        pessoa.setdNascimento(dNascimento);
        pessoa.setCidade(cidade);
        pessoa.setUf(uf);


        retorno = pessoaDAO.salvar(pessoa);

        return retorno;
    }
  • 1

    Why are you comparing whether the person object is greater than zero?

  • To save my person and if the code of the person object is greater than zero he saves!

  • 1

    By your code the save method always returns a boolean, with the return of it you know if the person saved or not, see my answer if it helps you.

  • In person Control in the save method the boolean return variable is not shown as initialized!

  • 1

    It makes no sense to check the Id being that in the method it is not even used (because of the parameters)

3 answers

3


First of all, if and else are not loops. They are conditional operators.

for and while do loops, that is, they repeat the same instruction diverse times until a certain condition is met. Other than if who makes an instruction if a certain condition is met.

Now, let’s get down to business. You said in the comments

To save my person and if the person object code is greater than zero it saves!

To check if the code is greater than zero and only then save, you must do

if (pessoa.getId() > 0) { //verifica se o ID é maior que zero
    if(pessoaController.salvar(pessoa)){ //instrução de salvar
        JOptionPane.showMessageDialog(this, "Registro gravado com sucesso!");
    }else{
        // Mensagem de erro
    }
}

Then we get into another problem of your code, the method salvar() in PessoaController. In the question code the method is asking a lot of parameters, in this case, all attributes of the object Pessoa.

public boolean salvar( String nome, String endereco, String bairro, String sexo, String telefone, String celular, String CPF, Date dNascimento, String cidade, String uf )

However, you are calling the method salvar(), passing as parameter an object Pessoa and not passing all the parameters that the method asks for. This will create another mistake.

The solution is to change the signature of your method salvar() to ask for an object as a parameter Pessoa. This way, you can popular the object attributes and then pass it whole to the PessoaController salvage.

public boolean salvar(Pessoa pessoa) { ... }

This way, you can use the method call as you are currently using

pessoaController.savar(pessoa);
  • Very good explanation and I help me a lot but so the way you said does not save anything!

  • 1

    Young man, the problem must be in your method of salvar() in pessoaDao And that’s another story. If any of the answers here helped you with the initial problem mark it as correct and open a new question with your new problem. @Igorcontini

  • All right, and I’ll do it if you find a mistake.

2

This error occurs due to this chunk of your code:

if (pessoaController.salvar(pessoa> 0)

For you are trying to ascertain whether an object is greater than a number.

Since you already have the populated Person object it is unnecessary for you to pass all attributes in the method parameters salvar, you can pass the person object itself which already possesses all the attributes.

In the Personal Controller:

public boolean salvar(Pessoa pessoa) {
  boolean retorno;
  if(pessoa != null)
    retorno = pessoaDAO.salvar(pessoa);

  return retorno;
}

In his method salvarPessoa you should remove this comparison of pessoa > 0 and pass your person object as method parameter. As noted by @jbueno in case you need to check if there is an Id in the person object and then perform the INSERT just add an if:

if (pessoa.getId() > 0 && pessoaController.salvar(pessoa)) {
 // restante do seu código
}

If you have Id greater than zero and the return method is true, it saved your object. Remembering that a condition && only executes the second if the first one is met.

  • Very good explanation and thanks a lot!

1

you can’t compare whether person is greater than zero because he is a person-like object,

what Voce can do is check the return of your save method if it is true

// Esse é o loop e o objeto pessoa!
        if (pessoaController.salvar(pessoa)) {
            JOptionPane.showMessageDialog(this, "Registro gravado com sucesso!");
            this.desabilitarCampos();
            this.carregarClientes();
            return true;
        } else {
            JOptionPane.showMessageDialog(this, "Erro ao gravar os dados!", "ERRO", JOptionPane.ERROR_MESSAGE);
            return false;
        }
  • I changed the save method in person Check as @jbueno asked and and followed his hint in the IF condition but only saves the person’s code and not name,Cpf and etc..

Browser other questions tagged

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