1

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;
    }
  }
4
  • Do you want to add the weights of all products, if weight is more than 100g? Commented Jul 6, 2020 at 13:32
  • No, for all the products over 100g i have to find the total amount of the weight and the same for the price Commented Jul 6, 2020 at 13:49
  • Total price for all the product which has weight more that 100g right? Commented Jul 6, 2020 at 13:51
  • yes and the weight for those products Commented Jul 6, 2020 at 13:52

2 Answers 2

1

The issues that you need to fix are:

  • sumWeight has to be defined outside the loop. Otherwise it can't accumulate the weights of several products.
  • sumPrice has to be defined similarly
  • sumWeight=+pt.weight does not add a new weight to the sum. You may have confused =+ with the += operator. You could use the += operator to do it, but often it's clearer to spell it out completely and write sumWeight = sumWeight + pt.weight. That way there's no chance of confusion.

Here's the minimal but working code:

    double sumWeight = 0;
    double sumPrice = 0;
    for (Products p : product) {
        if (p.weight > 0.1) {
            sumWeight = sumWeight + p.weight;
            sumPrice = sumPrice + p.price;
        }
    }
    System.out.println("Total weight=" + sumWeight);
    System.out.println("Total price=" + sumPrice);
Sign up to request clarification or add additional context in comments.

2 Comments

I replied with the same. I also haven’t noticed he used =+ instead of +=. Thanks for noticing.
yes i didn't notice the sign, thank you so much it work
1

Does this helps?

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

class Products {
    int id;
    String name;
    int height;
    int lenght;
    int width;
    double weight;
    double price;
    DecimalFormat df;

    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;
        this.df = new DecimalFormat("0.00");
    }

    @Override
    public String toString() {
        return "id: " + this.id + " name: " + this.name + " " + this.height + "x" + this.lenght
                + "x" + this.width + "mm" + " weight: " + df.format(this.weight) + "kg" + " price: "
                + df.format(this.price) + "lv.";
    }
}

class Scratch {
    public static void main(String[] args) {
        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);

        List<Products> product = new ArrayList<>();
        product.add(p1);
        product.add(p2);
        product.add(p3);
        product.add(p4);

        double sumWeight = 0;
        double sumPrice = 0;
        for (Products pt : product) {
            //sum of the weight if product weight is more than 100g
            if (pt.weight > 0.1) {
                sumWeight += pt.weight;
                sumPrice += pt.price;
            }

            //show products
            System.out.println(pt);
        }

        System.out.println("\nAggregated attribute ---");
        System.out.println("Total Wight: " + sumWeight);
        System.out.println("Total Price: " + sumPrice);
    }
}

Note: You don’t actually need iterator to iterate through a List. Also use toString() if you want to format your class

5 Comments

this just take the weight and the price of the element it doesn't calculate them
i could fine it like that double sumWeight = p2.weight + p4.weight; double sumPrice = p2.price + p4.price;
but if you don't know which is the element (p1,p2,p3,p4) you need how it wil be ? that is what i am trying to do
what do mean by don't know which is the element here are we are iterating over all the products and checking whether it has weight greater than 100g, then only calculate the sum of price and weight. Isn’t that what you want?
I have changed =+ to +=. I copied the code from you. Can you please check now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.