[ASP. NET Development Series] Shopping Cart program design--Shopping cart class

Source: Internet
Author: User
Asp.net| Program | Shopping Cart | design

Web application development, most e-commerce sites have online shopping this function module, so the preparation of shopping cart program is very important.

The purpose of a shopping cart is to implement these features: adding objects, modifying objects, deleting objects, checking carts, viewing carts, and so on. This article explains the three features, "Add Objects," "Delete objects," and "View carts." Of course, here is just a simple shopping cart class, the completion of the function is not much, not perfect, need everyone in this foundation to do the expansion to make its function more perfect.

C # is a complete OOP (Object oriented programming) language, is also Microsoft's flagship language, can be said to be one of several popular languages in the future. The sample code in this article is written using C #. The following is the creation of a shopping cart class that completes the functions of adding objects, deleting objects, and viewing cart objects, with filename ShoppingCart.cs:

Using System;

Using System.Web.UI;

Using System.Collections; You must introduce this namespace with the Hashtable class

namespace Wendwcart//namespace name

{

[Serializable]

public class stat_class{//define commodity classes, save various attributes of the product

String Shangpinid; Product ID

String Sp_name; Product Name

Decimal Sp_price; Commodity price

int Sp_quan; Number of items

Public String itemid{

Get{return Shangpinid;}

Set{shangpinid=value;}

}

Public String shangpinname{

Get{return Sp_name;}

Set{sp_name=value;}

}

Public decimal price{

Get{return Sp_price;}

Set{sp_price=value;}

}

public int quantity{

Get{return Sp_quan;}

Set{sp_quan=value;}

}

Public Stat_class (String itemid,string shangpinname,decimal price,int Quantity) {//construction method, initializing individual properties of a product

Shangpinid=itemid;

Sp_name=shangpinname;

Sp_price=price;

sp_quan=quantity;

}

}

[Serializable]

public class shoppingcart{

Hashtable cart_orders=new Hashtable ();

Public ICollection orders{

Get{return Cart_orders.values;}

}

public decimal totalcost{//Calculate Total Price

get{

Decimal total=0;

foreach (DictionaryEntry entry in cart_orders) {

Stat_class order= (stat_class) entry. Value;

total+= (order. Price*order. Quantity);

}

return total;

}

}

public void AddItem (Stat_class order) {//Add object method

Stat_class order= (Stat_class) cart_orders[order.itemid];

if (order!=null)

Order. quantity+=order.quantity;

Else

Cart_orders.add (Order.itemid,order);

}

public void DeleteItem (String ItemID) {//delete object

if (cart_orders[itemid]!=null)

Cart_orders.remove (ItemID);

}

}

}

To compile the ShoppingCart.cs file:

Csc/t:library/out:shoppingcart.dll ShoppingCart.cs

Deploy the ShoppingCart.dll component to the bin directory.

Description

In order to ensure that the session state is effectively saved regardless of the session mode used, the serializable serialization is preceded by the definition class. In addition, in order for each user to log on, an instance of a class can be created and added in the Global.asax file:

<%@ Import namespace= "Wendwcart"%>

<%@ application codebehind= "Global.asax.cs" inherits= "HDLab.BBS.Global"%>

<script language= "C #" runat= "Server" >

void Session_Start ()

{

session["Myshoppingcart"]=new ShoppingCart ();

}

</script>

Where Wendwcart is the name of the control's namespace.

The next article, "Shopping Cart program development-Call Shopping cart class" will explain how to apply the ShoppingCart.dll component to add and remove objects in the ASP.net page.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.