Pass value typed to another method

2

I'm having a question about passing a typed value. I'm posting here because I stayed the whole afternoon yesterday and did not find anything.

In 1st class:

public class CompararLogin {

private String nome;
public String cpf;

Scanner scan = new Scanner(System.in);

public String solicitarCpfLogin() {
    System.out.print("Cpf: ");
    cpf = scan.nextLine();
    return cpf;
}

public String solicitarNome() {
    System.out.print("Nome: ");
    nome = scan.nextLine();
    return nome;
}

public String getCpf() {
    return cpf;
}

public void setCpf(String cpf) {
    this.cpf = cpf;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

}

In 2nd Class:

public class SocioDAO {

public void fazerLogin() throws SQLException, NomeUsuarioNaoInformadoExecption, CpfUsuarioNaoInformadoException, TelefoneUsuarioNaoInformadoException {

    StringBuilder sql = new StringBuilder();

    sql.append("SELECT codigo, nome, cpf ");
    sql.append("FROM socio ");

    Connection conexao = FabricaDeConexao.conectar();

    PreparedStatement comando = conexao.prepareStatement(sql.toString());

    ResultSet rs = comando.executeQuery();

    CompararLogin compararUsuarioExistente = new CompararLogin();

    String verificaNomeCadastrado = compararUsuarioExistente.solicitarNome();
    String verificaSenhaUsuario = compararUsuarioExistente.solicitarCpfLogin();

    boolean achou = false;
        while (rs.next()) {

            String nome = rs.getString("nome");
            String cpf = rs.getString("cpf");
            int codigo = rs.getInt("codigo");


            if (verificaNomeCadastrado.equals(nome) && verificaSenhaUsuario.equals(cpf)){
                System.out.println("Login Efetuado Com Sucesso!!! ");
                achou = true;
        SolicitacoesEmprestimosPendentesExceptions contarSolicitacoesEmprestimo = new SolicitacoesEmprestimosPendentesExceptions();
                contarSolicitacoesEmprestimo.Contar(cpf);
                System.err.println("SEU CÓDIGO P/ CADASTRO DOS DVD'S É: " + codigo);
                MenuChamarCadastroDvdEEmprestimo mostrarOpcao = new MenuChamarCadastroDvdEEmprestimo();
                mostrarOpcao.escolherOpcaoDvdEmprestimo();

            }

                }  if (achou == false) {
                    System.out.println("Usuário não Cadastrado!!!");
                    TratadorDeIniciarAplicacao voltando = new TratadorDeIniciarAplicacao();
                    voltando.main(null);
                }
       }
}

In 3rd Class: This method makes a count of loan requests ie the guy is willing to rent a DVD and is on a waiting list, which I intend to pass this value when the Login is successful.

/ p>

public class SolicitacoesEmprestimosPendentesExceptions extends CompararLogin {

public int contadorDeSolicitacoesEmprestimosPendentes(String cpf) throws SQLException, CpfUsuarioNaoInformadoException{

    StringBuilder sql = new StringBuilder();
    sql.append("SELECT COUNT(*) ");
    sql.append("FROM solicitacaoemprestimo ");
    sql.append("INNER JOIN socio ON (solicitacaoemprestimo.socio_codigo = socio.codigo) ");
    sql.append("WHERE socio.cpf = '11111111111' ");

    Connection conexao = FabricaDeConexao.conectar();
    PreparedStatement comando = conexao.prepareStatement(sql.toString());

    ResultSet resultado = comando.executeQuery();
    int nCont = 0;

    while(resultado.next()){
        nCont = resultado.getInt("COUNT(*)");
    }

    return nCont;

}

public void Contar(String cpf) throws CpfUsuarioNaoInformadoException, SQLException, NomeUsuarioNaoInformadoExecption, TelefoneUsuarioNaoInformadoException{
    SolicitarEmprestimoDAO dao = new SolicitarEmprestimoDAO();

    System.out.println(contadorDeSolicitacoesEmprestimosPendentes(cpf));


}

}

So folks, my idea is this: When I enter the CPF to login I would like my contadorDeSolicitacoesEmprestimosPendentes method to receive this value entered from the CPF. So I would show the pending value result within my FazerLogin() method, which would be this:

SolicitacoesEmprestimosPendentesExceptions contarSolicitacoesEmprestimo = new SolicitacoesEmprestimosPendentesExceptions();
                contarSolicitacoesEmprestimo.Contar(cpf);
    
asked by anonymous 10.06.2015 / 14:21

2 answers

3

You're already passing String cpf when you do:

 contarSolicitacoesEmprestimo.Contar(cpf);

Now, the problem was at the time you were mounting your sql. When you do, sql.append("WHERE socio.cpf = '11111111111' "); ends up looking only at the CPFs with 11111111111.

One option would be to mount as follows, using the String cpf :

sql.append("SELECT COUNT(*) ");
sql.append("FROM solicitacaoemprestimo ");
sql.append("INNER JOIN socio ON (solicitacaoemprestimo.socio_codigo = socio.codigo) ");
sql.append("WHERE socio.cpf =" +cpf); 
    
10.06.2015 / 15:15
2

I was able to solve it, I'll post it here, maybe someone help.

I have modified this class:

public String pegarCpf(){
    cpf = getCpf();
    return cpf;
}

public int contadorDeSolicitacoesEmprestimosPendentes(String cpf) throws SQLException, CpfUsuarioNaoInformadoException{

    StringBuilder sql = new StringBuilder();
    sql.append("SELECT COUNT(*) ");
    sql.append("FROM solicitacaoemprestimo ");
    sql.append("INNER JOIN socio ON (solicitacaoemprestimo.socio_codigo = socio.codigo) ");
    sql.append("WHERE socio.cpf = ? ");

    Connection conexao = FabricaDeConexao.conectar();
    PreparedStatement comando = conexao.prepareStatement(sql.toString());


    comando.setString(1, pegarCpf());

    ResultSet resultado = comando.executeQuery();

    int nCont = 0;

    while(resultado.next()){
        nCont = resultado.getInt("COUNT(*)");
    }

    return nCont;

}

public void Contar(String cpf) throws CpfUsuarioNaoInformadoException, SQLException, NomeUsuarioNaoInformadoExecption, TelefoneUsuarioNaoInformadoException{
    SolicitarEmprestimoDAO dao = new SolicitarEmprestimoDAO();

    System.out.println("Solicitações Pendentes: "+contadorDeSolicitacoesEmprestimosPendentes(cpf));


}

}

And in the class compare Login I modified it to static.

public class CompararLogin {

    private String nome;
    public static String cpf;

    Scanner scan = new Scanner(System.in);

    public String solicitarCpfLogin() {
        System.out.print("Cpf: ");
        cpf = scan.nextLine();
        return cpf;
    }

    public String solicitarNome() {
        System.out.print("Nome: ");
        nome = scan.nextLine();
        return nome;
    }

    public String getCpf() {
        return cpf;
    }

    @SuppressWarnings("static-access")
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

}
    
10.06.2015 / 14:50
______ azszpr69569 ___

%pre% %pre%
    
___
Show selected option in select with JS