0

i am going to type my code here and then I will explain my problem below.

for (int i = 0; i < sales.totalSales(); i++) {
            EntidadGeo gec = sales.getSale(i).getCustomer();
            EntidadGeo get = sales.getSale(i).getStore();                               
            int[] c = geo.georeferenciar(sales.getSale(i).getCustomer().getCP(), ventas.getVenta(i).getCCustomer().getCalle(), ventas.getVenta(i).getCCustomer().getProvincia());
            gec.setX(c[0]);
            gec.setY(c[1]);             
            int[] c2 = geo.georeferenciar(ventas.getSale(i).getStore().getCP(), ventas.getVenta(i).getStore().getCalle(), ventas.getSale(i).getStore().getProvincia());
            get.setX(c2[0]);
            get.setY(c2[1]);
            mapaventas.representar(gec, get);
            }

I have that for loop, what i want to do in my project is to print in a map. The point is what I need to draw in the map are customers and stores and one store can sell to many customers at the same time. In my project I am using MVC pattern, this part belongs to Controller part, and in the model part i draw the map. It works now but the problem is that my project draw one customer and one store instead of 4 customers per 1 store.

Thanks

vilal_
  • 37
  • 1
  • 7
  • I see that you iterate through each store (ventas), but where do you iterate through each store's client array or collection? Shouldn't you have nested for loops here? – Hovercraft Full Of Eels May 11 '13 at 16:28
  • 1
    It is good practice to use english when naming methods and variables. For example, it makes it much easier for us to help you. – Keppil May 11 '13 at 16:28
  • what I have is a container with sales(ventas) and inside it i have 1 object wich is store(tienda) and 1 objetc wich is cliente(customer), that is why I iterate in sales container and get the customer and the stores, but it draws it one by one and i dont want to repeat equal stores. Stores have a field which is id to make them differents among all of them. – vilal_ May 11 '13 at 16:31

2 Answers2

1

Your problem is here:

mapaventas.representar(gec, get);

So it looks like you have a Map<Vendor, Client> which will only associate only one client per vendor. I have to guess at this because we have no knowledge what the method above does. If I am correct a better solution perhaps is to use a Map<Vendor, ArrayList<Client>>. so that a Vendor can be associated with multiple clients. Then you would do something like

ArrayList<Client> getList = mapaventas.get(gec);
// if the above is null, create the arraylist first and put it 
// and the gec into the map.
getList.add(get);

Note that my variable names and types will not be the same as yours, but hopefully you will understand the concept I'm trying to get across. If not, please ask.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @vilal_: but you will have two entities in the Map, one a vendor, the other an ArrayList of client. Otherwise you will need to explain your restrictions in greater detail as an edit to your original question. – Hovercraft Full Of Eels May 11 '13 at 16:38
  • I dont know how to explain all of it, the project is quite big and i am stack on this part. Anyway thanks for your help – vilal_ May 11 '13 at 16:48
  • @vilal_: our ability to help is only as good as your ability to explain the problem. If you still need help, consider updating your question with pertinent information. Que tenga buena suerte. – Hovercraft Full Of Eels May 11 '13 at 16:49
  • Before this line: int[] c2 = geo.georeferenciar(ventas.getSale(i).getStore().getCP(), ventas.getVenta(i).getStore().getCalle(), ventas.getSale(i).getStore().getProvincia());I want to check there if there is an IdStore already drawed and then i dont want to draw it – vilal_ May 11 '13 at 17:07
1

It sounds like your database has a one-to-many relation between Store and Customer. A corresponding object model might be List<Map<Store, List<Customer>>>. Because a Customer may trade at more than one Store, you want "to check there if there is an IdStore already drawn, and then I don't want to draw it."

One approach would be to iterate through the List and add entries to a Set<Location>. Because implementations of Set reject duplicate elements, only one copy would be present, and no explicit check would be required. As a concrete example using JMapViewer, you would add a MapMarker to the mapViewer for each Location in the Set, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045