Data search and comparison

Asked

Viewed 41 times

1

I need to carry out a search and I wrote the following code to carry it out, but without success. I wonder if I’m doing it properly.

public void AcessoCC()
{
    int id = 0;
    Integer infoConta;


    System.out.println("Informe o número da conta: ");
    sc = new Scanner(System.in);
    infoConta = sc.nextInt();

   for(int i = 0; i < clienteCC.length; i++)
    {
        if (clienteCC[i].getNumConta() == infoConta)
        {
            id = i;
        }
    }

    if (clienteCC[id].getCliente() != null)
    {
        System.out.println("Cliente: " + clienteCC[id].getCliente());
        System.out.println("Conta número: " + clienteCC[id].getNumConta());
        System.out.println("Cliente: " + clienteCC[id].getSaldo());
        System.out.println("Limite Disponivel: " 
                + clienteCC[id].getLimite());
        System.out.println("Limite Disponivel: " 
                + clienteCC[id].getLimiteTotal());

        do
        {
            System.out.println("Deseja: 'S'aque, 'D'eposito, 'V'er saldo"
                    + " ou 'E'ncerrar");


        }while(op != 'T');
    }else
    {
        System.out.println("Dados não localizados");
    }
}
  • What this code should do that it’s not doing?

  • Could show how the objects of clienteCC ? if possible edit your question and add the Customer Object class! Could you tell us what happens? What doesn’t work? Thanks!

1 answer

1


We have a small bug in your code!

But I don’t know if that’s what you’d like to report!

What happens if the user enters an account that doesn’t exist? He will always show the first!

You start the variable id with 0, and this value is a valid position in a list!

If you do not find any Customer, it will always show the first user of the list (position 0 of the list).

To correct, start the variable id with -1;

And before displaying the data, make sure id is different from -1!

 int id = -1;

    ....
    if(id != -1){
          System.out.println("Cliente: " + clienteCC[id].getCliente());
            System.out.println("Conta número: " + clienteCC[id].getNumConta());
            System.out.println("Cliente: " + clienteCC[id].getSaldo());
            System.out.println("Limite Disponivel: " 
                    + clienteCC[id].getLimite());
            System.out.println("Limite Disponivel: " 
                    + clienteCC[id].getLimiteTotal());

            do
            {
                System.out.println("Deseja: 'S'aque, 'D'eposito, 'V'er saldo"
                        + " ou 'E'ncerrar");


            }while(op != 'T');

    }else{
       System.out.println("Dados não localizados");
    }

One more tip!

No need to iterate the list to the end! If you find the client, you can exit the iteration; For this use break;

Example:

 for(int i = 0; i < clienteCC.length; i++)
    {
        if (clienteCC[i].getNumConta() == infoConta)
        {
            id = i;
            break;
        }
    }

Browser other questions tagged

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