Solved

Using an Addition function in a shopping cart class

Posted on 2011-03-20
13
248 Views
Last Modified: 2012-05-11
I am trying to build a shopping cart class and inside that class I am trying to add an addition function.  For each item I add into the shopping cart, I want to increment the total price of the items.  I'm kind of at a loss but I am thinking it requires a foreach statement.  Here is what I have so far, please help:

 double AddShoppingCartItems(ShoppingCartItem SRItem)
        {
            ShoppingCartItem cartitem;
            cartitem = new ShoppingCartItem();

            double itemprice;
            double totalitemprice;
           
            cartitem.ItemId = SRItem.ItemId;
            cartitem.ItemDescription = SRItem.ItemDescription;
            cartitem.ItemPrice = SRItem.ItemPrice;

            itemprice = cartitem.ItemPrice;



           
        }        
0
Comment
Question by:VBBRett
  • 8
  • 5
13 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35178059
OK, there is a number of options. And I believe different implementations have different approaches.

Option 1. In your ShoppingCart class you have a variable, say, totalCost. Each time you add an item to a cart, you increase this variable. And you should not forget to decrease it if you remove an item. And set to 0 if you clear the cart.

Personally, I don't like this.

2. I'd have a separate function that iterates through all the items and calculates total cost when it's needed.
Say, your CartItem has a property ItemPrice and a property ItemQuantity. In this case the pseudo code would be something like:

double TotaCartlCost(){
   double result = 0.0;
   for each CartItem in entire cart
   {
           result = result + CartItem.ItemPrice * CartItem.ItemQuantity;

    }

   return result;

}


I believe it will be fast enough and less error prone.

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35178073
Anyway, in your Cart class you need to have a list (or any other collection) of Cart items, so that you may add items and remove from this collection.

If you decide with the first approach all you need is to add code to your method like:


// class level variable:
double mTotalCartPrice;
double AddShoppingCartItems(ShoppingCartItem SRItem)
        {
            ShoppingCartItem cartitem;
            cartitem = new ShoppingCartItem();

            double itemprice;
            double totalitemprice;
            
            cartitem.ItemId = SRItem.ItemId;
            cartitem.ItemDescription = SRItem.ItemDescription;
            cartitem.ItemPrice = SRItem.ItemPrice;

// I believe this line has no sense. Where you get cartitem.ItemPrice from?
//You just created this object...

            itemprice = cartitem.ItemPrice;

            mTotalCartPrice = mTotalCartPrice + cartitem.ItemPrice;

            
        }

Open in new window

0
 

Author Comment

by:VBBRett
ID: 35178074
Sorry for my silly question, but would you be able to provide me with an example?  I am not familiar with the for each statement.  Thanks!
0
 

Author Comment

by:VBBRett
ID: 35178106
cartitem.itemprice is a variable where I thought I would hold the price of an item that was funnelled through my function through the (ShoppingCartItem SRItem) from the AddShoppingCartItems function.  The shoppingcartitem class has the following code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ShoppingCartItem
/// </summary>
/// 
namespace MarketingEmail
{
    public class ShoppingCartItem
    {
        private int itemid;
        private string itemdescription;
        private double itemprice;

        public int ItemId
        {
            get
            {
                return itemid;
            }
            set
            {
                if (value < 0)
                {
                    throw new Exception("Invalid ItemId");
                }
                itemid = value;
            }
        }

        public string ItemDescription
        {
            get
            {
                return itemdescription;
            }
            set
            {
                if (value.Length < 1)
                {
                    throw new Exception("Invalid Description");
                }
                itemdescription = value;
            }
        }

        public double ItemPrice
        {
            get
            {
                return itemprice;
            }
            set
            {
                if (value < 0)
                {
                    throw new Exception("Invalid Price");
                }
                itemprice = value;
            }
        }

    }
}

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35178130
What is in this case?
 
0
 

Author Comment

by:VBBRett
ID: 35178144
I guess I am looking to do a foreach statement to add up all the shopping cart items.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35178147
Implementation for foreach loop:


public double  cartTotalPrice()
        {
            double result = 0.0;

            foreach (ShoppingCartItem item in MyShoppingCart.Items)
            {
                result = result + CartItem.ItemPrice * CartItem.ItemQuantity;
            }
            return result;

        }

Open in new window

0
 

Author Comment

by:VBBRett
ID: 35178155
Is MyShoppingCart.Items supposed to be in my shoppingcart class?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35178159
OK, in your implementation your CartItem has no Quantity property. So, if you want to add 10 items to your cart, you just call AddItem 10 times? Or how you track a number of same items?

Also, your  AddShoppingCartItems(ShoppingCartItem SRItem) method is a bit strange. Honestly, I can't understand what you are trying to do?

See, your parameter is a ShoppingCartItem. Inside the method you create another ShoppingCartItem and copy values from one to another. Where you store all items that were added before?

Is that a homework you are trying to do?
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35178168
"Is MyShoppingCart.Items supposed to be in my shoppingcart class?"

I'd say 'yes'. You need some storage for your cart items. And some List or Dictionary would be a good place. In your shoppingcart class you create a collection and put all items there.
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 35178175
Have a look at some examples (heaps on Internet):

http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35178177
have to go...
0
 

Author Comment

by:VBBRett
ID: 35178190
To be honest, I am still pretty new at this and that is developing things out of Object Oriented Programming objects as opposed to Procedural code.  So, excuse me if I sound a bit lost.  What I am trying to do is to get an incrementer in my code so that I don't have to keep adding on my C# page so that everything just works as opposed to program again.  So, I am trying to program some kind of collection of items so that the total cost keeps getting added as I add more to a shopping cart and as I remove things from the shopping cart, things get removed.  So, let's say I have widgets, here is an example of what I would like to do.

I click a button and 1 widget that costs $10 gets added to the cart.

so I have the following in my cart:

1 widget cost: $10

total cost: $10

Now, if I add another widget, here is what would happen.

2 widgets cost: $20

total cost: $20

If I remove a widget, then the following would happen:

1 widget cost: $10

total cost: $10.

Now, if I add something else besides a widget, here is what would happen in the shopping cart:

1 widget: $10

1 ball: $5

total cost: $15

And so on and so forth.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now