3

What i'm trying to do

Find the price sum for all items in a shoppingcart list.

How i tried to solve it

I think it makes sense to add it as a property in the Cart-class. I also think it would be logical to just use a foreach loop to iterate the CartList and add the itemprice (ProductsInCart.Price) to a temporary variable (PriceSum), which is returned in "Cart.PriceAllContent" property.

C#

//Instantiating Cart
  Cart C2Cart = new Cart();

//Getting Item in cart from Session
  C2Cart.TakeCart();

//Writing out result
  Response.Write(C2Cart.PriceAllContent);

Classes

public class Cart
{
    //FIELDS    
    private List<ProductsInCart> _cartList;
    
    //PROPERTIES
    public List<ProductsInCart> CartList
    {
        get { return _cartList; }
        set { _cartList = value; }
    }


    public float PriceAllContent
    {
        get
        {
            float PriceSum= 0;
            foreach (var ProductsInCart in _cartList)
            {
                PriceSum= +ProductsInCart.Price;
            }
            return PriceSum;
        }
    }

  ...........
}

public class ProductsInCart
{
    //FIELDS
    private int _id;
    private string _name;
    private float _price;
    private int _amount;

............
}

Complete class code can be seen here (may not be fully updated yet. Ask if needed) https://github.com/chmodder/PlanteskoleWebsite/tree/master/App_Code

Problem

Problem is, that when writing out the result, It would only write out the last items price instead of the sum of all itemprices.

Response.Write(C2Cart.PriceAllContent);

I allready tried searching for a solution, but couldn't find what i needed. If anyone can help me solve the problem i would be a happy man.

Community
  • 1
  • 1
chmodder
  • 184
  • 1
  • 3
  • 17
  • 1
    `PriceSum= +ProductsInCart.Price` -> `PriceSum += ProductsInCart.Price`... You're doing `PriceSum = (+1 * ProductsInCart.Price)`. You can find issues like this by placing breakpoints, stepping through your code and inspecting the variables. – CodeCaster May 27 '14 at 14:07

3 Answers3

7

If you want to increment PriceSum variable with product price, use += operator:

foreach (var ProductsInCart in _cartList)
{
    PriceSum += ProductsInCart.Price; // instead of =+
}

When you write = +value then its two separate operators = operator and + operator i.e. +ProductsInCart.Price just returns value of products, price, and then you assign this value to PriceSum. As result, you will have price of last product in list.

You also can use LINQ instead of this loop:

public float PriceAllContent
{
    get { return _cartList.Sum(p => p.Price); }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 1
    that was fast :) will try it now – chmodder May 27 '14 at 14:08
  • Consequently to avoid any confusion, `PriceSum = PriceSum + ProductsInCart.Price;` – lucuma May 27 '14 at 14:11
  • 1
    Such a silly little error, which has cost me hours of frustration. It is almost embarrassing, but I'm happy to tell you, that it works now :) Thanks. I will checkmark your answer in about 10 min, when the site allows me to. – chmodder May 27 '14 at 14:13
  • Like I said in my comment, this shouldn't cost you hours. Visual Studio has one of the best debuggers out there. Use breakpoints and inspect your variables. – CodeCaster May 27 '14 at 14:14
  • @lucuma `+=` if very handy operator, I don't think it should confuse you, just get used to it :) – Sergey Berezovskiy May 27 '14 at 14:15
  • It doesn't confuse me, but it could confuse others. – lucuma May 27 '14 at 14:18
  • @CodeCaster: Yeah, but for some reason the debugger has been playing tricks on me in this project. It resets the Cart, when i start debug mode, which makes it hard for me to test on the Cart. About the '= +value', that was just a typo, but i did try it with 'something =+ value', which I didn't recall worked. BUt all is good now. It works and I kan get something else done. Thanks again – chmodder May 27 '14 at 14:22
3

The =+ should be += to fix your problem.

You can also simplify:

public float PriceAllContent
{
    get { return _cartList.Sum(i => i.Price); }
}

Although as a convention, I'd use a method CalculatePriceAllContent() instead of a property, to signify to the caller that you are doing a calculation and not just getting a stored value. See https://stackoverflow.com/a/601648/1094268

Community
  • 1
  • 1
David S.
  • 5,965
  • 2
  • 40
  • 77
  • A sum operation is not computationally complex nor does it produce side effects. – CodeCaster May 27 '14 at 14:17
  • @CodeCaster That depends what they mean by complex. It's actually does some calculations, that's my point. What if you have millions of objects in the list? It's `O(n)`, instead of `O(1)` which I'd like to expect for a property. – David S. May 27 '14 at 14:25
  • I think I will wait with LINQ until I have learned it. Thanks anyway :) – chmodder May 27 '14 at 14:41
0
        int sum1;
        List<int> num = new List<int>() {1,2,3,4,5 };
        sum1 = num.Sum();
        Console.WriteLine(sum1);