Repeated elements in hashmap

0

Hi, I'm finishing a small application that stores, the total sales of each employee depending on the product sold. I am using a hashmap for this, since I have thought that this data structure is the most efficient for my project. The problem I have in that it shows me duplicate data and I can not find a way to remove these duplicates, attached source:

public static String totalizarIDEmpleadoArticulo() throws SQLException{
    String sql = "SELECT * FROM ventas inner join articulos on ventas.idArt = articulos.idArt"
                + " ORDER BY ventas.idEmp, ventas.idArt";

    rs = stmt.executeQuery(sql);

    int empleado = 0;
    int articulo;

    String cadena = "";
    double suma = 0;
    rs.beforeFirst();
    Object[] contenido = new String[50];

    boolean correcto = rs.next();
    while (correcto) {
        suma = 0;

        empleado = rs.getInt("idEmp");
        articulo = rs.getInt("idArt");

        while (correcto && empleado == rs.getInt("idEmp")) {
            suma += rs.getDouble("ppu") * rs.getInt("unidades");
            correcto = rs.next();
        }

        contenedor.put(empleado, suma);

        contenido = contenedor.keySet().toArray();
        for (int i = 0; i < contenido.length; i++) {
                System.out.println("Empleado: " + "\t" + "total vendido:" + "\n" + "----------" + "\t" + "----------"
                        + "\n" + empleado + "\t" + "\t" + suma);

        } 
    }

    return cadena;
}
    
asked by daviserraalonso 03.04.2018 в 20:57
source

1 answer

2

I do not know why the code does not work for you, but you could solve everything in the SQL query.

This query accumulates by idEmp e idArt , adding the product of ppu by unidades :

SELECT ventas.idEmp, ventas.idArt, sum(ppu * unidades) as suma
FROM ventas inner join articulos on ventas.idArt = articulos.idArt
GROUP BY ventas.idEmp, ventas.idArt
ORDER BY ventas.idEmp, ventas.idArt

With this query it is not necessary to iterate over the result to calculate the sum.

    
answered by 03.04.2018 / 21:10
source