import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator;
public class ShoppingCartManager { HashMap<String, String> hm=new HashMap<String, String>(); float totlePrice=0;
//添加book到购物车 public void addBook(String bookId,String bookQuantity){ if(hm.containsKey(bookId)){ int value=Integer.parseInt(hm.get(bookId)); value+=Integer.parseInt(bookQuantity); hm.put(bookId, value+""); }else{ hm.put(bookId, bookQuantity); } } //修改数量 public void updateQuantity(String bookId,String bookQuantity){ hm.put(bookId, bookQuantity); } //获取购物车的所有信息 并计算总价 public ArrayList<BookBean> getShoppingCart(){ ArrayList<BookBean> al=new ArrayList<BookBean>(); Iterator<String> i=hm.keySet().iterator(); String ids=""; BookTableManager btm=new BookTableManager(); while(i.hasNext()){ ids=ids+","+i.next(); } al= btm.selectByBookIds(ids); totlePrice=0; //清空总价,防止无限累计 for(int j=0;j<al.size();j++){ BookBean bb=al.get(j); totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+"")); } return al; } //获取总价 public float getTotlePrice(){ return totlePrice; } //根据ID获取数量 public String getQuantityById(String id){ String quantity=hm.get(id); return quantity; } //清空购物车 public void clear(){ hm.clear(); } //删除购物车中的一本书 public void deleteById(String id){ hm.remove(id); }
}
using System; using System.Data; using System.Configuration; using System.Collections; 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.Collections; public partial class AddCar : System.Web.UI.Page { ess db = new ess(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string id = Request.QueryString["id"].ToString();
if (Session["car"] != null) { Hashtable hash = Session["car"] as Hashtable; if (!hash.ContainsKey(id)) { hash.Add(id, 1); } else { hash[id] = int.Parse(hash[id].ToString()) + 1; } Session["car"] = hash; } else { Hashtable hash = new Hashtable(); hash.Add(id, 1); Session["car"] = hash; } Hashtable k = Session["car"] as Hashtable;
DataColumn dc0 = new DataColumn("id", typeof(string)); DataColumn dc1 = new DataColumn("商品名", typeof(string)); DataColumn dc2 = new DataColumn("价格", typeof(float)); DataColumn dc3 = new DataColumn("数量", typeof(int)); DataColumn dc4 = new DataColumn("总价格", typeof(float)); DataTable dt = new DataTable(); dt.Columns.Add(dc0); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); dt.Columns.Add(dc4); foreach (DictionaryEntry i in k) { //Response.Write(i.Key+" "+i.Value+"<br>"); DataRow dr = dt.NewRow(); DataSet ds = db.QueryDataSet("select * from product where p_id=" + i.Key); dr["id"] = ds.Tables[0].Rows[0]["p_id"].ToString(); dr["商品名"] = ds.Tables[0].Rows[0]["p_name"].ToString(); dr["价格"] = ds.Tables[0].Rows[0]["p_price"].ToString(); dr["数量"] = int.Parse(k[i.Key].ToString()); dr["总价格"] = int.Parse(k[i.Key].ToString()) * double.Parse(ds.Tables[0].Rows[0]["p_price"].ToString()); dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind(); } }
protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("default2.aspx"); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string id = GridView1.DataKeys[e.RowIndex].Value.ToString(); Hashtable k = Session["car"] as Hashtable; Session["car"] = k; k.Remove(id); DataColumn dc0 = new DataColumn("id", typeof(string)); DataColumn dc1 = new DataColumn("商品名", typeof(string)); DataColumn dc2 = new DataColumn("价格", typeof(float)); DataColumn dc3 = new DataColumn("数量", typeof(int)); DataColumn dc4 = new DataColumn("总价格", typeof(float)); DataTable dt = new DataTable(); dt.Columns.Add(dc0); dt.Columns.Add(dc1); dt.Columns.Add(dc2); dt.Columns.Add(dc3); dt.Columns.Add(dc4); foreach (DictionaryEntry i in k) { //Response.Write(i.Key+" "+i.Value+"<br>"); DataRow dr = dt.NewRow(); DataSet ds = db.QueryDataSet("select * from product where p_id=" + i.Key); dr["id"] = ds.Tables[0].Rows[0]["p_id"].ToString(); dr["商品名"] = ds.Tables[0].Rows[0]["p_name"].ToString(); dr["价格"] = ds.Tables[0].Rows[0]["p_price"].ToString(); dr["数量"] = int.Parse(k[i.Key].ToString()); dr["总价格"] = int.Parse(k[i.Key].ToString()) * double.Parse(ds.Tables[0].Rows[0]["p_price"].ToString()); dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind();
} protected void Button2_Click(object sender, EventArgs e) { Hashtable k = Session["car"] as Hashtable; string code = DateTime.Now.Ticks.ToString() + new Random().Next(); db.ExecuteNonQuery("insert into OrderList values(+code+)"); foreach(DictionaryEntry i in k) { db.ExecuteNonQuery("insert into DetailsList values(+code+,"+Session["u_id"].ToString()+","+i.Key+","+i.Value+")"); } } } 自己慢慢领悟把
星梦云怎么样?星梦云资质齐全,IDC/ISP均有,从星梦云这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,星梦云专注四川高防服务器,成都服务器,雅安服务器。星梦云目前夏日云服务器促销,四川100G高防4H4G10M月付仅60元;西南高防月付特价活动,续费同价,买到就是赚到!点击进入:星梦云官方网站地址1、成都电信年中活动机(成都电信优化线路,封锁...
BuyVM商家属于比较老牌的服务商,早年有提供低价年付便宜VPS主机还记得曾经半夜的时候抢购的。但是由于这个商家风控非常严格,即便是有些是正常的操作也会导致被封账户,所以后来陆续无人去理睬,估计被我们风控的抢购低价VPS主机已经手足无措。这两年商家重新调整,而且风控也比较规范,比如才入手他们新上线的流媒体优化VPS主机也没有不适的提示。目前,BuyVM商家有提供新泽西、迈阿密等四个机房的VPS主机...
spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立服务器租用和Hybrid Dedicated等,spinservers这次提供的大硬盘、大内存服务器很多人很喜欢。TheServerStore自1994年以来,它是一家成熟的企业 IT 设备供应商,专门从事二手服务器和工作站业务,在德克萨斯州拥有40,000 平方英尺的仓库,库存中始终有数千台...