8

Click here to load reader

Build a Shopping Cart

Embed Size (px)

Citation preview

Page 1: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 1/8

Page 2: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 2/8

the server. This gives you the ability to just copy code out to the

server and it gives you the speed benefit of compiled code.

ASP.NET is an object oriented framework. Every function, property

and page is part of a class. For example, each web page is its ownclass that extends the Page class. The Page class has an event

that is fired when the webpage is loaded called the "Page Load

Event". You can write a function that subscribes to that event and

is called on. The same principle applies to other events like the

button click and "drop-down" "selected index changed" events.

 The logic is separate from the design and content. They interact

with each other, but they are in separate places. Generally, this

allows a designer to design without worrying about function and itallows the programmer to focus on function without looking at the

design. You have the choice of putting them both in the same file

or in different files. This is similar to model-view-controller model.

If you are new to ASP.NET (and you have Windows), you can try it

out for free You can download Visual Studio Express by visiting

the ASP.NET website. Also, when you create a website locally onyour machine, you can run the website at any time and Visual

Studio will quickly start a server on your computer and pull up

your website in your default browser.

Step 1: Create the ShoppingCart Class

We need a place to store the items in the shopping cart as well as

functions to manipulate the items. We'll create a ShoppingCart

class for this. This class will also manage session storage.

Page 3: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 3/8

First, we have to create the App_Code folder. To do this, go to the

"Website" menu, then "Add ASP.NET Folder", and choose

"App_Code." This is where we'll put all of our custom classes.

 These classes will automatically be accessible from the code in

any of our pages (we don't need to reference it using somethingsimilar to "include" or anything). Then we can add a class to that

folder by right-clicking on the folder and choosing "Add New

Item."

Quick Tip: Regions in ASP.NET are really nice to organize and

group code together. The nicest thing about them is that you can

open and close regions to minimize the amount of code that youare looking at or quickly find your way around a file.

view plaincopy to clipboardprint?

using System.Collections.Generic;

using System.Web;

/**

* The ShoppingCart class

*

* Holds the items that are in the cart and provides methods fortheir manipulation

*/

public class ShoppingCart {

Page 4: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 4/8

#region Properties

public List<CartItem> Items { get; private set; }

#endregion

#region Singleton Implementation

// Readonly properties can only be set in initialization or in a

constructor

public static readonly ShoppingCart Instance;

// The static constructor is called as soon as the class is loaded

into memory

static ShoppingCart() {

// If the cart is not in the session, create one and put it there

// Otherwise, get it from the session

if (HttpContext.Current.Session["ASPNETShoppingCart"] ==null) {

Instance = new ShoppingCart();

Instance.Items = new List<CartItem>();

Page 5: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 5/8

HttpContext.Current.Session["ASPNETShoppingCart"] =

Instance;

} else {

Instance =(ShoppingCart)HttpContext.Current.Session["ASPNETShoppingCar

t"];

}

}

// A protected constructor ensures that an object can't becreated from outside

protected ShoppingCart() { }

#endregion

#region Item Modification Methods

/**

* AddItem() - Adds an item to the shopping

*/

public void AddItem(int productId) {

// Create a new item to add to the cart

CartItem newItem = new CartItem(productId);

Page 6: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 6/8

Page 7: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 7/8

RemoveItem(productId);

return;

}

// Find the item and update the quantity

CartItem updatedItem = new CartItem(productId);

foreach (CartItem item in Items) {

if (item.Equals(updatedItem)) {

item.Quantity = quantity;

return;

}

}

}

/**

* RemoveItem() - Removes an item from the shopping cart

*/

public void RemoveItem(int productId) {

CartItem removedItem = new CartItem(productId);

Items.Remove(removedItem);

}

#endregion

Page 8: Build a Shopping Cart

8/14/2019 Build a Shopping Cart

http://slidepdf.com/reader/full/build-a-shopping-cart 8/8

 

#region Reporting Methods

/**

* GetSubTotal() - returns the total price of all of the items

* before tax, shipping, etc.

*/

public decimal GetSubTotal() {

decimal subTotal = 0;

foreach (CartItem item in Items)

subTotal += item.TotalPrice;

return subTotal;

}

#endregion

}