How to call a method into an If and Else?

Asked

Viewed 2,638 times

1

I am developing software for a video rental company with Pattern MVC (Model-view-controller) design and the problem of not calling the save() method and the change() method is occurring inside If and Else on the Save button. If the variable type is "new" it saves and if the variable type is "change" it changes. I have tried several ways and I could not find the solution. and someone there could help me ? From now on, I thank you!

Video person class, view layer:

public class VideoPessoa extends javax.swing.JFrame {

    PessoaController pessoaController;
    Pessoa pessoa;
    String tipoCadastro;

    /**
     * Creates new form Pessoa
     */
    public VideoPessoa() {
        initComponents();

        new Conexao();
        pessoaController = new PessoaController();
        pessoa = new Pessoa();
        this.carregarPessoas();// Fica sublinhado em vermelho indicando erro!
         this.novaPessoa();// Fica sublinhado em vermelho indicando erro!
        this.habilitarCampos();// Fica sublinhado em vermelho indicando erro!   
    }

Save button and change button inside the view layer:

       private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {                                          
                    // TODO add your handling code here:

                    if (tipoCadastro.equals("novo")){
                        salvarPessoa();// Fica sublinhado vermelho indicando erro!

                    } else if(tipoCadastro.equals("alteracao")){

                        alteraPessoa(); // Fica sublinhado vermelho indicando erro!
                  }
            }  
        }                                         

     private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {                                           
                    // TODO add your handling code here:
                    novaPessoa();
                    habilitarCampos();
                    recuperarPessoas();
                    tipoCadastro = "alteracao";
                }  
         private void novaPessoa(){
                habilitarCampos();

                txtCodigo.setText("Novo");
                txtNome.setText("");
                txtEndereco.setText("");
                txtBairro.setText("");
                txtCidade.setText("");
                txtCPF.setText("");
                txtUF.setText("");
                txtTelefone.setText("");
                txtCelular.setText("");
                txtSexo.setText("");
                tipoCadastro = "novo";
            }  


 public boolean alterarPessoa() {
        pessoa.setCodigo( Integer.parseInt(this.txtCodigo.getText()));
        pessoa.setNome(this.txtNome.getText());
        pessoa.setEndereco(this.txtEndereco.getText());
        pessoa.setBairro(this.txtBairro.getText());
        pessoa.setCPF(this.txtCPF.getText());
        pessoa.setSexo(this.txtSexo.getText());
        pessoa.setUf(this.txtUF.getText());
        pessoa.setCelular(this.txtCelular.getText());
        pessoa.setTelefone(this.txtTelefone.getText());
        pessoa.setCidade(this.txtCidade.getText());

// Aqui executo um teste se altera e quando exibe esse teste os dados não ficam alteradose e sempre exibe a mensagem alterados com sucesso!

          Integer codigo = pessoa.getCodigo();
         String nome = pessoa.getNome();
JOptionPane.showMessageDialog(this, "Código:"+codigo+ "nome:"+nome);

        if (pessoaController.alterar(pessoa)) {

            JOptionPane.showMessageDialog(this, "Registro alterado com sucesso!");
             this.desabilitarCampos();
            this.carregarPessoas();
            return true;
        } else {
            JOptionPane.showMessageDialog(this, "Erro ao alterar os dados!", "ERRO", JOptionPane.ERROR_MESSAGE);
          return false;
        }



    }               
  • Which error indicates underlined?

  • @rray That mistake : cannot find symbol

  • Hi, once I read that in java the equals compares data type and not the value that is inside the variable, try to change the comparison to == Example: typeCard == "new", Check if it works like this.

  • This is your method to salvaPessoa(); it comes from where?

  • @Techies From within the view layer and still does not call the save() and change() method, why?

  • He doesn’t call or he’s making a mistake? Because in the comment you say you’re making a mistake

  • It does not call and is underlined in red indicating that something is wrong and when I step over the mouse pointer, it displays the message: cannot find symbo!

  • From what I’ve noticed there’s a } the most in your method

  • @Techies I took out that quote and call the methods without that quote.. 4 whole days lost just because of a quote!

  • @Techies Thanks and not even change the data of my person..

  • Put your method change that I try to help

  • @Techies I updated my question..

  • When you click to change the person’s fields are filled in?

  • When I click yes why I put words to myself changes.

Show 10 more comments

2 answers

1

if (salvarPessoa()){
 // faça alguma coisa
}

if (alterarPessoa()){
 // faça alguma coisa
}

I didn’t quite understand your question, but that would be it?

  • That’s right but the variable type is "new", saves the data of my person and the variable type is "change" alters the data of my person.

1

Well, a priori I see that every time you hit the Alter Person button, you call the novaPessoa(), which, in this case, clears all fields and would not change its object. So do not call it before changing the person.

Another detail that, in this case, is better to use a boolean, which occupies less memory than a String and there are no problems of comparison (a String in uppercase is different from a String in lowercase).

Getting something like that:

public class BlaBlaBla { 
    boolean alterarPessoa = false;

  private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {                                           
     habilitarCampos();
     recuperarPessoas();
     alterarPessoa = true;
  }

  private void novaPessoa(){
   habilitarCampos();

   txtCodigo.setText("Novo");
   txtNome.setText("");
   txtEndereco.setText("");
   txtBairro.setText("");
   txtCidade.setText("");
   txtCPF.setText("");
   txtUF.setText("");
   txtTelefone.setText("");
   txtCelular.setText("");
   txtSexo.setText("");
   alterarPessoa = false;
 } 

  private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {
          if (!alterarPessoa){
              salvarPessoa();
          } else {
              alteraPessoa();
          }
    }
}
  • I just found in another forum a problem like mine! I’m making a reuse of code, that is, Ctrl+c and Ctrl+v from a colleague of mine, but I didn’t know that he had copied his code on the Internet and found the code that he had copied. Also I did not know that my colleague had copied an excerpt of a code from another forumThe code owner was encountering the same problem as me that is of not altering the data of my person.. and I couldn’t find the solution.. Follow the link : [http://www.guj.com.br/java/253029-update-de-registros-no-banco-data]

  • I get it, it’s good to always search the internet for specific points of help, complete codes are usually full of problems. When we are beginners, it is common that we do not know how to judge a code in a language we do not master, hence to fall into codes with bad practices, bad implementations and future problems, it is very simple. Just a Copy/Paste. I hope the answer will help you understand a little more and fix the problem. Good implementations!

  • Thanks a lot, man! At least I found the answer and I will implement my code and make the necessary posts from your reply and the other response from the other forum!

Browser other questions tagged

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