dimanche 3 mai 2015

List of unique product by reflection?

I looking for solution how to make List (or something) with unique Product. The reason i want to do it is total price of Product. Each sets can contains same Product.

Here's my classes.

public class Product {

  public String name; // unique name
  public double price;
  public double qty;

}

&

public class Sets {
  public Product item1;
  public Product item2;
  ...
  public Product item7;
  public static listsProduct<Product> = new Arraylists<Product>();
}

I'm trying to make a Lists but i don't know how to add a unique product. To add Product i use reflection.

My method:

 public void getProducts() throws NoSuchMethodException, Exception {
     Sets object = this;
     Class clazz = object.getClass();
     Field[] fields = clazz.getFields();
     Method m1 = Product.class.getMethod("getname", null);
     for (Field field : fields) {
       if(field.get(object)!=null) {
         System.out.println(field.getName()+" = " + m1.invoke(field.get(object),null));
         Product e=(Product) field.get(object);
         if (listsProduct==null ) listsProduct.add((Produkt) field.get(object));
         if (!(listsProduct.contains(field.get(object)))) listsProduct.add(e);

        }
     }

It's adding a Product correctly but how make UNIQUE lists?

Thanks in advance for any help !





Aucun commentaire:

Enregistrer un commentaire