vilal_
vilal_

Reputation: 37

MVC in Java array

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

Upvotes: 0

Views: 814

Answers (2)

trashgod
trashgod

Reputation: 205865

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.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 1

Related Questions