110,534
社区成员
发帖
与我相关
我的任务
分享
public class Cart
{
private product _Products;//产品
public product Products
{
get { return _Products; }
set { _Products = value; }
}
private int _Count;//购买数量
public int Count
{
get { return _Count; }
set { _Count = value; }
}
private double _Price;//单品总价
public double Price
{
get { return _Price; }
set { _Price = value; }
}
}
public class ShoppingCart : System.Web.UI.Page
{
B_product Bproduct = new B_product();
//所有
public List<Cart> getAll()
{
List<Cart> list = new List<Cart>();
if (Session["Cart"] == null)
{
list = null;
}
else
{
list = Session["Cart"] as List<Cart>;
}
return list;
}
//增加
public List<Cart> getCart(Cart c)
{
List<Cart> list = new List<Cart>();
if (Session["Cart"] == null)
{
list.Add(c);
Session["Cart"] = list;
Session.Timeout = 60;
}
else
{
list = (List<Cart>)Session["Cart"];
bool yn = false;
for (int i = 0; i < list.Count; i++)
{
if (list[i].Products.Id == c.Products.Id)
{
list[i].Count += c.Count;
list[i].Price = list[i].Count * list[i].Products.ProductPrice;
Session["Cart"] = list;
yn = true;
}
}
if (yn == false)
{
list.Add(c);
Session["Cart"] = list;
}
}
return list;
}
//删除
public List<Cart> getDeleCart(Cart c)
{
List<Cart> list = new List<Cart>();
list = (List<Cart>)Session["Cart"];
for (int i = 0; i < list.Count; i++)
{
if (list[i].Products.Id == c.Products.Id)
{
list.Remove(c);
Session["Cart"] = list;
}
}
return list;
}
//更新
public List<Cart> getUpdaCart(Cart c)
{
List<Cart> list = new List<Cart>();
list = (List<Cart>)Session["Cart"];
for (int i = 0; i < list.Count; i++)
{
if (list[i].Products.Id == c.Products.Id)
{
list[i].Count = c.Count;
list[i].Price = c.Price;
Session["Cart"] = list;
}
}
return list;
}
}