using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// Cart 的摘要說明
/// </summary>
public class Cart
{
public Cart()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public void addItem(string name, int quantity, decimal price)
{
if (System.Web.HttpContext.Current.Session["Cart"] == null)
{
DataTable newdt = new DataTable();
newdt.Columns.Add("ID", typeof(int));
newdt.Columns["ID"].AutoIncrement = true;
newdt.Columns["ID"].AutoIncrementSeed = 1;
newdt.Columns.Add("ProductName", typeof(string));
newdt.Columns.Add("Quantity", typeof(int));
newdt.Columns.Add("Price", typeof(decimal));
//newdt.Columns.Add("UersName", typeof(string));
newdt.Columns.Add("Total", typeof(decimal));//hgh
newdt.PrimaryKey = new DataColumn[] { newdt.Columns[0] };
DataRow dr = newdt.NewRow();
dr["ProductName"] = name;
dr["Quantity"] = quantity;
dr["Price"] = price;
//dr["UersName"] = username;
dr["Total"] = price * quantity;//jkj
newdt.Rows.Add(dr);
System.Web.HttpContext.Current.Session["Cart"] = newdt;
}
else
{
DataTable dt = (DataTable)System.Web.HttpContext.Current.Session["Cart"];
bool match = false;
foreach (DataRow dr in dt.Rows)
{
if (dr["ProductName"].ToString() == name)
{
int num = Convert.ToInt32(dr["Quantity"]);
num += 1;
dr["Quantity"] = num;
match = true;
}
}
if (!match)
{
DataRow dr = dt.NewRow();
dr["ProductName"] = name;
dr["Quantity"] = quantity;
dr["Price"] = price;
//dr["UersName"] = username;
dr["Total"] = price * quantity;//jkj
dt.Rows.Add(dr);
System.Web.HttpContext.Current.Session["Cart"] = dt;
}
}
}
public void delete(int id)
{
DataTable dt = (DataTable)System.Web.HttpContext.Current.Session["Cart"];
dt.Rows.Find(id).Delete();
System.Web.HttpContext.Current.Session["Cart"] = dt;
}
public decimal Total()
{
DataTable dt = (DataTable)System.Web.HttpContext.Current.Session["Cart"];
DataRow dr;
int counter;
decimal total = 0;
for (counter = 0; counter < dt.Rows.Count; counter++)
{
dr = dt.Rows[counter];
total += decimal.Parse(dr["Price"].ToString()) * Int32.Parse(dr["Quantity"].ToString());
}
return total;
}
public void update(int id, int updateNum)
{
DataTable dt = (DataTable)System.Web.HttpContext.Current.Session["Cart"];
DataRow dr = dt.Rows.Find(id);
dr["Quantity"] = updateNum;
dr["Total"] =Convert.ToDecimal(dr["Price"])*updateNum;//jkj
System.Web.HttpContext.Current.Session["Cart"] = dt;
}
}