List in object within object list is not updated

1
static public void agregarComida(String comida){

    listaComida.add(comida);
    hacerCuentasComida();


}

private static void hacerCuentasComida(){

    Map<String, Integer> contador = new HashMap<>();
    Map<String, Double> precio = new HashMap<>();
    List<String> comidas = new ArrayList<>();

    Boolean existeCliente=false;
    for (String item : listaComida) {


        String[] parts = item.split(",");

        if (!contador.containsKey(parts[0])) {

            comidas.add(parts[0]);
            contador.put(parts[0], 1);

            //Sacar precio
            precio.put(parts[0], Double.valueOf(parts[1]));

            //Llenar lista objeto

            for(Cliente clienteLoop : listaCliente){
                if (Objects.equals(clienteLoop.getNumeroCliente(), String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()))){
                    new Cliente(String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()),comidas,contador,precio);
                    existeCliente=true;
                }
                else{
                    existeCliente=false;
                }
            }

            if(!existeCliente){
                Cliente cliente =new Cliente(String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()),comidas,contador,precio);
                listaCliente.add(cliente);
            }

        } else {
            Integer count = contador.get(parts[0]);
            contador.put(parts[0], count + 1);
            count = contador.get(parts[0]);
            //Sacar Precio
            precio.put(parts[0], (count*Double.valueOf(parts[1])));

            //Llenar lista objeto

            for(Cliente clienteLoop : listaCliente){
                if (Objects.equals(clienteLoop.getNumeroCliente(), String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()))){
                    new Cliente(String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()),comidas,contador,precio);
                    existeCliente=true;
                }
                else{
                    existeCliente=false;
                }
            }

            if(!existeCliente){
                Cliente cliente =new Cliente(String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()),comidas,contador,precio);
                listaCliente.add(cliente);
            }
        }
    }

I explain:

I store food ordered by a client, in the application I can add more clients to ask for food, as in the end I have to show a receipt for each client, I try to keep each client as an object, which has the lists of: Things you ordered, the amount and the total price of each.

The lists work great, everything is kept perfect in the lists, the problem comes when I try to send the lists to their respective clients, I made an algorithm to add a new client to the clients object, only if the client has not been added before, but if it has been added before I update the information of the lists returning them to put to their respective client within the object.

The code has been very complicated for me, I am close to reaching something, but now, it happens that when you see what is stored inside the lists that are inside the client object, only what you entered for the first time is saved time That is, if you already add 8 foods to the lists, the object still has 1 food, it has not been updated up to 8.

I hope you can help me see where my mistake is.

    
asked by Eduardo Ricardez 23.04.2017 в 00:38
source

1 answer

1

I think the problem is generated in the following code, your object Cliente implements Getter and Setter . And in the fragment of code that evaluates if the client exists:

    boolean existeCliente = false;
    for(Cliente clienteLoop : listaCliente){
        if (Objects.equals(clienteLoop.getNumeroCliente(), String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()))){
        // Si el cliente existe
        clienteLoop.setPrecio(precio);
        clienteLoop.setComidas(comidas);
        clienteLoop.setContador(contador);
        existeCliente = true;
        break;
        }
     }
     if(!existeCliente){
        Cliente cliente = new Cliente(String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()),comidas,contador,precio);
        listaCliente.add(cliente);
     }

You are creating a new object but not referencing it to save it in an existing one.

Or simply:

for(Cliente clienteLoop : listaCliente){
    if (Objects.equals(clienteLoop.getNumeroCliente(), String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()))){
        clienteLoop = new Cliente(String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()),comidas,contador,precio);
        existeCliente=true;
        break;
      }
}

if(!existeCliente){
       Cliente cliente =new Cliente(String.valueOf(tabLayout.getTabAt(tabLayout.getSelectedTabPosition()).getText()),comidas,contador,precio);
       listaCliente.add(cliente);
}
    
answered by 23.04.2017 / 02:18
source