Problem with logic in java

0

Hello, I have a problem and I believe it is logical, but I can not find it. I have 4 listings and I have to group the markets of a region, then I have a list with all the products linked to the market and I have my list which are the lists of the products chosen. I want the market name and the sum of the values of if I were to make a purchase in it but no market can be compared if I do not have all the products.

 Listas l=Listas.getInstance();
    int i =1;  //Regiaoo.getIdReg();
    List<prodmerc> tempPM = new ArrayList();
    List<prodmerc> tempPM1 = new ArrayList();
    List<FiltraMercados> fm=new ArrayList();


    FiltraMercados ff=new FiltraMercados();
    for (int ii=0;ii<l.getM().size();ii++){
        for (int j=0;j<l.getMl().size();j++){
            for (int jj=0;jj<l.getPm().size();jj++){
                if(l.getPm().get(jj).getMercID()==l.getM().get(ii).getMercID() && l.getPm().get(jj).getProdID()==l.getMl().get(j).getP().getProdID() && l.getM().get(ii).getRegID()==i){
                    FiltraMercados f=new FiltraMercados(l.getM().get(ii),l.getMl().get(j).getP().getProdID(),(l.getPm().get(jj).getPmPreco()*l.getMl().get(j).getQtda()));
                    fm.add(f);
                }
            }
        }
    }
    
asked by anonymous 04.07.2016 / 11:20

1 answer

0

Good morning. Friend I for this type of problem I first organize the thought, soon the logic would be organized. 1st - Exclusion validations - Makes it easier not to compare everything 2º - Rules

There are more things, but you can already improve your code First you did a bubble sort that can certainly be improved (my point of view).

Let's go 1 - Exclusion, get only the region you want with a method then see only those who have the products you need, recommend hash uses that looks better and does not need to scan the product list. Notice that in this method there will be no loop (for or while), at most a loop to fill the list of state and products, however I think you should have done this before this step.

2º - Sum of values, I advise you to put in a hash list where the hash is the market, so you put the sum as value and present it in your application

I think if you follow these steps you'll get what you want. Hope this helps. Any doubts put there that I try to help.

    
04.07.2016 / 13:06