[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using an Addition function in a shopping cart class

Posted on 2011-03-20
13
Medium Priority
?
263 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 30

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 30

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 30

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
 
LVL 30

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 30

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 30

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 30

Accepted Solution

by:
anarki_jimbel earned 2000 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 30

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 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