How to extract and compare the values of an object stored in an array? Java

1

I have a ArrayList of clients and I have to find a client by name and compare his debt with another value to know if he can buy or not. This is my code. It tells me that the method.getName is not defined and that it can not resolve DEBT. Would someone give me some guidance on how to do this right? Thanks and regards!

public void generarFactura(int IMPORTE){        
    String NOMBRECLIENTE;
    Scanner sc=new Scanner(System.in);
    System.out.println("Inserte el nombre del cliente que desea hacer una compra");
    NOMBRECLIENTE= sc.nextLine();

    //aca recorreria el arraylist y verificaria que el cliente este, y que su deuda sea 
    //menor a 4000$ para ahi pasar a emitir factura
    int i=0;

    for (i=0;i<((CharSequence) listaClientes).length();i++) {

        if (listaClientes.getNombre().equals(NOMBRECLIENTE)){

            if(listaClientes.DEUDA<4000){
                    //creo el objeto factura y le doy valores random
                    Factura facturacliente=new Factura(3000, 7, 8);
        }else{
            System.out.println("El cliente acumulo demasiada deuda");
        }
    }
}
    
asked by Ivan 23.02.2018 в 17:59
source

2 answers

0

I assume that Clients list is something like:

ArrayList<Cliente> listaClientes;

Therefore, you want to go through the list and find the client you can do it like this:

for (Cliente c: listaClientes) {
    if (c.getNombre().equals(...)) {

    }
}

or like this:

for (int i=0; i< listaClientes.size(); i++) {
    Cliente c= listaClientes.get(i);
    if (c.getNombre().equals(...)) {

    }
}

or like this:

listaClientes.forEach(cliente -> {
    if (cliente.getNombre().equals(...) {

    }
);
    
answered by 23.02.2018 / 18:14
source
0

You should do then:

listaClientes.get(i).getNombre().equals(NOMBRECLIENTE);

You can not apply the getNombre () method to the entire list, but to each member. I mean you can not do this:

listaClientes.getNombre()

Here the same thing happens to you:

if(listaClientes.DEUDA<4000){
                    //creo el objeto factura y le doy valores random
                    Factura facturacliente=new Factura(3000, 7, 8);

It should be:

 if(listaClientes.get(i).DEUDA<4000){
                    //creo el objeto factura y le doy valores random
                    Factura facturacliente=new Factura(3000, 7, 8);
    
answered by 23.02.2018 в 18:14