-5

I am trying to calculate the average value from a shopping cart (ArrayList). Average value means the sum all the products divided by its quantity?, correct me If I am wrong please maybe that's why my logic is not working well.

I was trying to do a loop for calculating the sum of all the products and then divided by its quantity.

public double getAverageValue(){
    double averageValue = 0;

    for ( int i=0; i < cartLineList.size() ; i++) {
        double sum += cartLineList.get(i).getProduct();
    }

    for (CartLine cart : cartLineList) {
        averageValue = (sum / cart.getQuantity());
    }
    return averageValue;
}

public class CartLine {

private Product product;
private int quantity;

public CartLine(Product product, int quantity) {
    this.product = product;
    this.quantity = quantity;
}

public double getSubtotal() {
    return quantity * product.getPrice();
}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}
}
  • It seems fine, what is your problem? – navy1978 Jan 31 '19 at 12:51
  • 4
    Doesn't even compile. `sum` is scoped only for the first loop – Lino Jan 31 '19 at 12:51
  • 1
    https://stackoverflow.com/questions/10359907/array-sum-and-average look for the correct way here. Your code doesn't seem to compile. What you should look for is to define ```sum``` variable first and only once (cause right now you're doing it every loop), then accumulate the sum and in the end divide sum by the size of the cart array. – Никита Михайлов Jan 31 '19 at 12:51
  • @НикитаМихайлов keep in mind that java != javascript – Lino Jan 31 '19 at 12:52
  • I guess getProduct dont return a double but a product. Don't you have a getPrice() method or somethng like that? "=+" is not valid. Last "+=" is not necessary. Division should be done with BigDecimal to avoid issues – alain.janinm Jan 31 '19 at 12:53
  • @Lino didn't even notice that, thanks. However since this is more of an algorithmic problem this should be helpful anyway. – Никита Михайлов Jan 31 '19 at 12:54
  • It seams your logic is not clear, Do you want like `sum of all product divided by sum of quantity` – Deepak Kumar Jan 31 '19 at 12:56
  • Could you please share the structure of `CartLine` class – Deepak Kumar Jan 31 '19 at 12:59
  • Show an example input and desired output. – J-Alex Jan 31 '19 at 13:03
  • 1
    @alain.janinm `=+` is valid, but more like `= +` that is, assignment and [Unary Plus Operator](https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.15.3) (mostly NOP just numeric promotion, sure not what is wanted) – user85421 Jan 31 '19 at 13:05
  • @DKAnsh I m trying to get the average value from a cart. Therefore I want all the products price from that cart and then divided by its quantity. Something like this ( I got 3 products in a cart of 15$, 20 and 10$ then the AVG will be 45$/3 = 15$) I dont know if that makes sense for you. – Luis Rodriguez Ugarte Jan 31 '19 at 13:17
  • @CarlosHeuberger Indeed your right. I said it's not valid because it should be used with primitive numeric. I doubt that its the return type of getProduct(). And honestly I don't think that he wants to use the unary plus operator. – alain.janinm Jan 31 '19 at 13:21

1 Answers1

1

try something like this

public double getAverageValue(){
  double averageValue = 0;
  double sum = 0;

  if(cartLineList.size() > 0){
    for ( int i=0; i < cartLineList.size() ; i++) {
      // assuming the product class has a price
      sum += cartLineList.get(i).getProduct().getPrice();
    }
    averageValue = (sum / (double)cartLineList.size())
  }

  return averageValue;
}
Haris Bouchlis
  • 2,366
  • 1
  • 20
  • 35
  • ```if(cartLineList.size() > 0)``` is redunant though, as well as casting size of the ```cartLineList``` to double since ```sum``` is already double. – Никита Михайлов Jan 31 '19 at 13:21
  • 2
    No you need to check if its more than 0 when you divide by it `averageValue = (sum / (double)cartLineList.size())`. Also when you divide or multiply between a double a non double value you should always convert both to double, or else you get weird results. – Haris Bouchlis Jan 31 '19 at 13:25
  • Sorry, didn't pay enough attention, you're right about checking for the zero, however, it might be better to use ```isEmpty()``` method. I've never heard of some issues with dividing double by integer. Can you share some topics about it? – Никита Михайлов Jan 31 '19 at 13:31
  • 1
    Thank you @HarisBouchlis! It compiles but when I run JUnit Test didn't work but after debugging all good as I corrected the expected value from this function. – Luis Rodriguez Ugarte Jan 31 '19 at 13:37
  • @Никита Михайлов check this https://stackoverflow.com/questions/32571909/java-integer-double-division-confusion?noredirect=1&lq=1 – Haris Bouchlis Jan 31 '19 at 13:44
  • @HarisBouchlis but it is wrong that you need to convert **both** values to double - where is that documented? in contrast to [JLS 5.6.2 - Binary Numeric Promotion](https://docs.oracle.com/javase/specs/jls/se11/html/jls-5.html#jls-5.6.2) (the posted link is about both values being **int**) – user85421 Jan 31 '19 at 18:33