⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shopping.cs

📁 ASP.NET2.0(C#篇)经典教程的源码...本源码很好的实现了购物车....编码规范和类的设计具有很好的借鉴性!
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;

namespace Wrox.Commerce
{
  /// <summary>
  /// Shopping cart item
  /// </summary>
  /// <remarks>Stores a single item in the shopping cart</remarks>
  [Serializable]
  public class CartItem
  {
    #region Private Member Variables
    // private member variables: the private storage for the properties
    private double _lineTotal;
    private double _price;
    private int _productID;
    private string _productImageUrl;
    private string _productName;
    private int _quantity;
#endregion

    #region Constructors

    /// <summary>
    /// Create a new cart item
    /// </summary>
    /// <remarks></remarks>
    public CartItem()
    {
      // no initialization
    }
 
    /// <summary>
    /// Create a new cart item with the supplied values
    /// </summary>
    /// <param name="ProductID">The <see cref="System.Integer">product ID</see></param>
    /// <param name="ProductName">The <see cref="System.String">product name</see></param>
    /// <param name="ProductImageUrl">The <see cref="System.String">URL of the product image</see></param>
    /// <param name="Quantity">The <see cref="System.Integer">quantity required</see></param>
    /// <param name="Price">The <see cref="System.Double">price of the product</see></param>
    /// <remarks></remarks>
    public CartItem(int ProductID, string ProductName, string ProductImageUrl, int Quantity, double Price)
    {
          this._productID = ProductID;
          this._productName = ProductName;
          this._productImageUrl = ProductImageUrl;
          this._quantity = Quantity;
          this._price = Price;
          this._lineTotal = Quantity * Price;
    }
    #endregion

    #region Properties
    /// <summary>
    /// Product ID
    /// </summary>
    /// <value></value>
    /// <remarks></remarks>
    public int ProductID
    {
      get {return this._productID;}
      set {this._productID = value;}
    }
 
    /// <summary>
    /// Product name
    /// </summary>
    /// <value></value>
    /// <remarks></remarks>
    public string ProductName
    {
      get {return this._productName;}
      set {this._productName = value;}
    }
 
    /// <summary>
    /// URL of the product image
    /// </summary>
    /// <value></value>
    /// <remarks></remarks>
    public string ProductImageUrl
    {
      get {return this._productImageUrl;}
      set {this._productImageUrl = value;}
    }
 
    /// <summary>
    /// Quantity required
    /// </summary>
    /// <value></value>
    /// <remarks></remarks>
    public int Quantity
    {
      get {return this._quantity;}
      set {this._quantity = value;}
    }
 
    /// <summary>
    /// Product price
    /// </summary>
    /// <value></value>
    /// <remarks></remarks>
    public double Price
    {
      get {return this._price;}
      set {this._price = value;}
    }

    /// <summary>
    /// Line total
    /// </summary>
    /// <value></value>
    /// <remarks>Read only</remarks>
    public double LineTotal
    {
      get {return (this._quantity * this._price);}
    }
    #endregion
  }

  /// <summary>
  /// The shopping cart
  /// </summary>
  /// <remarks></remarks>
  [Serializable]
  public class ShoppingCart
  {
    // percentage discount given to members
    private const float MemberDiscountPercentage = 0.1f;

    #region Private Member Variables

    private DateTime _dateCreated;
    private List<CartItem> _items;
    private DateTime _lastUpdate;

    #endregion

    #region Constructors

    /// <summary>
    /// Createa new instance of the cart
    /// </summary>
    /// <remarks></remarks>
    public ShoppingCart()
    {
      if (this._items == null)
      {
        this._items = new List<CartItem>();
        this._dateCreated = DateTime.Now;
      }
    }

    #endregion

    #region Properties
    /// <summary>
    /// The items contained within the cart
    /// </summary>
    /// <value></value>
    /// <remarks>Contains a <see cref="System.Collections.Generic.List(Of CartItem)">List</see> of <paramref name="CartItem">CartItem</paramref> objects</remarks>
    public List<CartItem> Items
    {
      get {return this._items;}
      set {this._items = value;}
    }
 
    /// <summary>
    /// Returns the amount of the member discount 
    /// </summary>
    /// <value></value>
    /// <remarks>0 if the user is not a member. Read only</remarks>
    public double MemberDiscount
    {
      get
      {
        if (HttpContext.Current.User.IsInRole("FanClubMember"))
        {
          return (this.SubTotal * MemberDiscountPercentage);
        }
        return 0;
      }
    }
 
    /// <summary>
    /// Returns the subtotal of the order
    /// </summary>
    /// <value></value>
    /// <remarks>Read only</remarks>
    public double SubTotal
    {
      get
      {
        if (this._items == null)
          return 0;

        double t = 0;
        foreach (CartItem item in _items)
          t += item.LineTotal;

        return t;
      }
    }
 
    /// <summary>
    /// Returns the total order value, including member discount
    /// </summary>
    /// <value></value>
    /// <remarks></remarks>
    public double Total
    {
      get {return (this.SubTotal - this.MemberDiscount);}
    }
 
    #endregion

    #region Methods
    /// <summary>
    /// Insert an item into the cart
    /// </summary>
    /// <param name="ProductID">The <see cref="System.Integer">product ID</see></param>
    /// <param name="Price">The <see cref="System.Double">price of the product</see></param>
    /// <param name="Quantity">The <see cref="System.Integer">quantity required</see></param>
    /// <param name="ProductName">The <see cref="System.String">product name</see></param>
    /// <param name="ProductImageUrl">The <see cref="System.String">URL of the product image</see></param>
    /// <remarks></remarks>
    public void Insert(int ProductID, double Price, int Quantity, string ProductName, string ProductImageUrl)
    {
      int ItemIndex = this.ItemIndexOfID(ProductID);
      if (ItemIndex == -1)
      {
        CartItem NewItem = new CartItem();
        NewItem.ProductID = ProductID;
        NewItem.Quantity = Quantity;
        NewItem.Price = Price;
        NewItem.ProductName = ProductName;
        NewItem.ProductImageUrl = ProductImageUrl;
        this._items.Add((CartItem) NewItem);
      }
      else
      {
        _items[ItemIndex].Quantity++;
      }
      this._lastUpdate = DateTime.Now;
    }
 
    /// <summary>
    /// 
    /// </summary>
    /// <param name="RowID">The <see cref="System.Integer">row id</see></param>
    /// <param name="ProductID">The <see cref="System.Integer">product ID</see></param>
    /// <param name="Quantity">The <see cref="System.Integer">quantity required</see></param>
    /// <param name="Price">The <see cref="System.Double">price of the product</see></param>
    /// <remarks></remarks>
    public void Update(int RowID, int ProductID, int Quantity, double Price)
    {
      CartItem Item = this._items[RowID];
      Item.ProductID = ProductID;
      Item.Quantity = Quantity;
      Item.Price = Price;
      this._lastUpdate = DateTime.Now;
    }
 
    /// <summary>
    /// Returns the index of the item in the collection
    /// </summary>
    /// <param name="ProductID"></param>
    /// <returns></returns>
    /// <remarks>-1 if the item isn't found</remarks>
    private int ItemIndexOfID(int ProductID)
    {
      int index = 0;

      foreach (CartItem item in _items)
      {
        if (item.ProductID == ProductID)
          return index;
        index++;
      }
      return -1;
    }

    /// <summary>
    /// Deletes an item form the cart
    /// </summary>
    /// <param name="rowID">The <see cref="System.Integer">row ID of the item to delete</see></param>
    /// <remarks></remarks>
    public void DeleteItem(int rowID)
    {
      this._items.RemoveAt(rowID);
      this._lastUpdate = DateTime.Now;
    }
 
    #endregion
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -