In the development of web applications, most e-commerce websites have online shopping functional modules, so the writing of shopping cart programs is very important.
The function of the shopping cart is nothing more than to implement these functions: add objects, modify objects, delete objects, check carts, view carts, etc. In this article, we will explain the three functions of "adding objects", "deleting objects" and "viewing carts". Of course, this is just a simple shopping cart class, which does not have many functions and is not perfect enough. You need to expand on this foundation to make its functions more complete.
C# is a complete OOP (Object Oriented Programming) language. It is also Microsoft's flagship language and can also be said to be one of the most popular languages in the future. The sample code in this article is written in C#. The following is to create a shopping cart class, which completes the functions of adding objects, deleting objects and viewing shopping cart objects. The file name is ShoppingCart.cs:
using System;
using System.Web.UI;
using System.Collections; //The namespace must be introduced when using the Hashtable class
namespace WendwCart //The namespace name
{
[Serializable]
public class Stat_Class{ //Define the product class and save various attributes of the product String ShangPinID; //Product ID
String Sp_Name; // Product name decimal Sp_Price; // Product price int Sp_Quan; // Product quantity 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, initialize each attribute of the
productShangPinID=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 the 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);
}
}
}
Compile the ShoppingCart.cs file:
csc /t:library /out: ShoppingCart.dll ShoppingCart.cs
deploys the ShoppingCart.dll component to the bin directory.
Note:
In order to ensure that the session state can be effectively saved no matter what session mode is used, Serializable serialization is added in front of the definition class.
In addition, in order to enable each user to create an instance of the class when logging in, add:<%@ Import Namespace="WendwCart" %>
to the Global.asax file
<%@ 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 namespace name of the control. In the next article "Shopping Cart Program Development - Calling the Shopping Cart Class", we will explain how to use the ShoppingCart.dll component in the ASP.NET page to add and delete objects.