Problem with java logic

Asked

Viewed 70 times

0

Hi, I have a problem and I believe it’s logical, but I can’t find it. I have 4 lists and I have to group the markets of a region, then I have a list with all products linked to the market and I have My list that are the lists of products chosen. I want the name of the market and the sum of the values of if I would make a purchase on it but no market can be compared if I don’t 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);
                }
            }
        }
    }
  • Friend, your code is very complicated to understand. I see several problems, but as it is not possible to leave wanting to fix everything at once, first I advise you to rename the variables to more intuitive names. You can’t tell what each variable is there. After that, we can see if it becomes easier to understand the problem.

1 answer

0

Good morning. Friend I for this type of problem first I organize the thought, soon the logic will be organized. 1º - Delete validations - Makes it easy that we will not compare everything 2nd - Rules

It has more things but with that you can improve your code First you made a Bubble Sort that can surely be improved (my point of view).

Come on 1º - Exclusion, take only the region you want with a method then see only those that have the products you need, recommend use hash that gets much better and do not need to sweep the list of products. Note that in this method no loop will be made (for or while), at most a loop to fill the status and product list, however I think you should have done this before this step.

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

I think if you follow these steps you’ll get what you want. I hope I’ve helped. Any doubt put there that I try to help.

Browser other questions tagged

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