please help me , i can understand how to get just one of the value of the element and collect it with the same type of other element , in the task i need to take weight and price.
DecimalFormat df = new DecimalFormat("0.00");
Products p1 = new Products(1, "tomatoes-", 90, 75, 45, 0.0, 0.98);
Products p2 = new Products(2, "potatoes-", 80, 54, 35, 8.90, 0.67);
Products p3 = new Products(3, "melon-", 123, 60, 100, 0.08, 0.70);
Products p4 = new Products(4, "eggplant-", 30, 50, 56, 4.78, 0.60);
ArrayList<Products> product = new ArrayList<Products>();
product.add(p1);
product.add(p2);
product.add(p3);
product.add(p4);
Iterator itr = product.iterator();
while (itr.hasNext()) {
Products pt = (Products) itr.next();
//sum of the weight if product weight is more than 100g
if (pt.weight > 0.1) {
double sumWeight =+pt.weight ;
System.out.println(sumWeight);
}
//show products
System.out.printf("id: " + pt.id + " name: " + pt.name + " " + pt.height + "x" +pt.lenght
+ "x"+ pt.width + "mm" + " weight: " + df.format(pt.weight) + "kg" + " price: "
+df.format(pt.price)+ "lv.\n");
}
}
}
class Products {
int id;
String name;
int height;
int lenght;
int width;
double weight;
double price;
public Products(int id, String name, int height, int lenght, int width, double weight, double price)
{
this.id = id;
this.name = name;
this.height = height;
this.lenght = lenght;
this.width = width;
this.weight = weight;
this.price = price;
}
}