见Ibuyadventure;到wrox.com上去下载一个,
该程序使用得是即时注册,用户不需注册和登陆就可以把物品放进购物车,等到结帐得时候在进行登陆或注册,该程序采用得是数据库储存会话,不容易发生信息丢失现象,采用得是n层结构,表示层,业务逻辑层,数据层
该程序使用得是即时注册,用户不需注册和登陆就可以把物品放进购物车,等到结帐得时候在进行登陆或注册,该程序采用得是数据库储存会话,不容易发生信息丢失现象,采用得是n层结构,表示层,业务逻辑层,数据层
解决方案 »
- 关于ADO.NET的dataset的merge的问题,跪求解~~~~
- activex控件的使用
- 在应用程序或全局主题目录中无法找到主题“Theme1”。 请高手了
- 请问website类型的项目能运行unsafe代码吗?如能,应该如何设置compiler的unsafe开关?
- 我有一个CrysRpt.rpt的水晶报表文件放在D:\目录下,我怎么预览它?
- Forms认证深入一步,各位帮忙!!!!!!!!
- 关于xml排序的问题
- 再求:asp.net中 怎么执行带多个参数的存储过程?
- 请问大家在dreamweaverMX中写的javascript和vs.net2003中有什么不同
- web程序如何读取客户端的文件?
- 请教高手
- 为什么返回页面是乱码?
最后一章
< cart>
< total>0< /total>
< /cart>
将这个XML字符串由parseString函数转换成XMLDocument存入myCart。
其代码如下:
public void emptyCart() throws IOException,SAXException{
String stringCart=“< ?xml version=‘1.0’encoding=‘gb2312’?> ”+
“< cart>< total>0< /total>< /cart>”;
myCart=parseString(stringCart);
}2. 添加商品
添加商品,即将传入的item元素添加到根元素cart里,
其中item里包括商品详细信息,
同时计算total的值。其代码如下:
public void addItemToCart(String stringItem)
throws IOException,SAXException{
//将item由String转换为XMLDocument
XMLDocument itemAdded=parseString(stringItem);
//取出item节点,并复制它
NodeList itemList=itemAdded.getElementsByTagName(“item”);
Node item=itemList.item(0);
Node cloneItem=item.cloneNode(true);
//如果购物车为空,则构造一个新的购物车
if(isCartEmpty()){
myCart.emptyCart();
}
//如果该商品不在购物车中,则插入该商品,并计算总金额
if(!isItemExist(item,myCart)){
//取myCart的根元素,并将复制的item节点添加到后面
Element cartRoot=myCart.getDocumentElement();
Node cartNode=cartRoot.appendChild(cloneItem);
computeTotal(); //计算总金额
}
}
3. 删除商品
删除商品,即根据商品代码将该商品的item元素
从myCart的根元素cart中删除,
并重新计算total的值:
public void moveItemFromCart(String id){
//取出以item为单位的节点集cartList以及根元素cartRoot
NodeList cartList=myCart.getElementsByTagName(“item”);
Element cartRoot=myCart.getDocumentElement();
//在cartList中查找代码为选中id的商品
for(int x=0;x< cartList.getLength();x++){
Node itemNode=cartList.item(x);
String idValue=itemNode.getFirstChild().
getFirstChild().getNodeValue();
//如果找到,则从cartRoot中删除该节点,并跳出循环
if(idValue.equals(id)){
itemNode=cartRoot.removeChild(itemNode);
break;
}
}
computeTotal(); //计算总金额
}
4. 改变商品数量
根据客户在页面上所填的数量,修改myCart中quantity,
并重新计算total:
public void addQuantityToCart(String qnty) throws
IOException,SAXException{
//将传过来的包含商品数量的一组XML字符串转换为XML文档
XMLDocument quantityChanged=parseString(qnty);
//取出包含新数量的quantity节点集和myCart中的quantity节点集
NodeList quantityList=quantityChanged.getElementsByTagName(“quantity”);
NodeList cartList=myCart.getElementsByTagName(“quantity”);
//循环改变商品的数量
for(int x=0;x< cartList.getLength();x++){
//将新quantity的值赋给myCart中相应的quantity中去
String quantity=quantityList.item(x).getFirstChild().getNodeValue();
cartList.item(x).getFirstChild().setNodeValue(quantity);
}
computeTotal(); //计算总金额
}
5. 计算总金额
即计算total的值,其中total=∑(price*quantity):
public void computeTotal(){
NodeList quantityList=myCart.getElementsByTagName(“quantity”);
NodeList priceList=myCart.getElementsByTagName(“price”);
float total=0;
//累加总金额
for(int x=0;x< priceList.getLength();x++){
float quantity=Float.parseFloat(quantityList.item(x)
.getFirstChild().getNodeValue());
float price=Float.parseFloat(priceList.item(x).getFirstChild().getNodeValue());
total=total+quantity*price;
}
//将total附给myCart的total
String totalString=String.valueOf(total);
myCart.getElementsByTagName(“total”).
item(0).getFirstChild().setNodeValue(totalString);
}
6. 判断购物车是否为空
通常在添加新商品时,还需要知道购物车是否为空,
如果为空的话,则要生成一个新的购物车。
public boolean isCartEmpty(){
//item的节点集,如果该节点集包含的节点数为0,则购物车内没有商品,返回true
NodeList itemList=myCart.getElementsByTagName(“item”);
if(itemList.getLength()==0) return true;
else return false;
}
7. 判断所选商品是否已在购物车内
即判断新传来商品的item是否已在myCart中存在,如果存在,返回true。
public boolean isItemExist(Node item, XMLDocument cart){
NodeList itemList=cart.getElementsByTagName(“item”);
Node id=item.getFirstChild();
String idValue=id.getFirstChild().getNodeValue();
if(itemList.getLength()!=0){
for(int x=0;x< itemList.getLength();x++){
Node itemTemp = itemList.item(x);
7Node idTemp=itemTemp.getFirstChild();
String idTempValue=idTemp.getFirstChild().getNodeValue();
if(idValue.equals(idTempValue)) return true;
}
return false;
}
return false;
}除上述方法外,XMLCart还包括将XML字符串由输入时的String转换成XMLDocument的方法parseString,以及用于输出时将XSL赋给myCart并返回String型XML字串的 cartTurnToStringWithXSL方法来辅助购物车主要操作的实现
{
id,
num
}
即可,自编例子
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.SessionState;
namespace conan
{
public class order
{
private string m_id;
private int m_amount;
public order()
{
}
public order(string str_id,int n_amount)
{
m_id=str_id;
m_amount=n_amount;
}
public string id
{
get
{
return m_id;
}
}
public int amount
{
get
{
return m_amount;
}
}
}
public class shopcart : System.Web.Services.WebService
{
public shopcart(string strUser)
{
InitializeComponent();
m_user=strUser;
Session["shoppingcart"]=m_user;
}
public shopcart()
{
InitializeComponent();
if(Session["shoppingcart"]!=null)
m_user=Session["shoppingcart"].ToString();
if(Session["content"]!=null)
m_list=(ArrayList)Session["content"];
}
private string m_user;
public string user
{
get
{
if(Session["shoppingcart"]!=null)
return Session["shoppingcart"].ToString();
else
return null;
}
}
private ArrayList m_list;
public ArrayList list
{
get
{
if(Session["content"]!=null)
{
m_list=(ArrayList)Session["content"];
return m_list;
}
else
return null;
}
}
public decimal cost
{
get
{
return (decimal)Session["cost"];
}
set
{
Session["cost"]=value;
}
}
public int pay
{
get
{
return (int)Session["pay"];
}
set
{
Session["pay"]=value;
}
}
public string memo
{
get
{
return Session["memo"].ToString();
}
set
{
Session["memo"]=value;
}
} public int choice
{
get
{
return (int)Session["choice"];
}
set
{
Session["choice"]=value;
}
}
public bool invoice
{
get
{
return (bool)Session["invoice"];
}
set
{
Session["invoice"]=value;
}
}
public int send
{
get
{
return (int)Session["send"];
}
set
{
Session["send"]=value;
}
}
public void reset()
{
Session["shoppingcart"]=null;
Session["content"]=null;
}
public void AddShoppingCart(string strID, int nAmount)
{
order neworder=new order(strID,nAmount);
order oldorder;
if(nAmount==0)
{
DeleteShoppingCart(strID);
return;
}
if(Session["content"]!=null)
{
m_list=(ArrayList)Session["content"];
}
for(int i=0;i<m_list.Count;i++)
{
oldorder=(order)m_list[i];
if(strID==oldorder.id)
{
UpdateShoppingCart(strID,nAmount+oldorder.amount);
return;
} }
m_list.Add(neworder);
Session["content"]=m_list;
}
public void DeleteShoppingCart(string strID)
{
order tmporder=new order();
int index=0;
if(Session["content"]!=null)
{
m_list=(ArrayList)Session["content"];
for(index=0;index<m_list.Count;index++)
{
tmporder=(order)m_list[index];
if(tmporder.id==strID)
break;
}
if(index==m_list.Count)
return;
m_list.RemoveAt(index);
Session["content"]=m_list;
}
}
public void UpdateShoppingCart(string strID,int mAmount)
{
DeleteShoppingCart(strID);
AddShoppingCart(strID,mAmount);
Session["content"]=m_list;
}
public void DeleteAll()
{
Session["content"]=null;
m_list.RemoveRange(0,m_list.Count);
reset();
}
#region Component Designer generated code
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
m_list=new ArrayList();
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
}}
请问在
http://web.wrox.com/0764558900/ASP.NET_Code.rar 最后一章
那个范例, 我执行setup时会出现错误而无法将里面的档案做complier, 所以在最后一章的范例没有办法执行, 我想在最后一章中在components中的cs檔应该是要编译成dll檔后放在bin之下, 请问要怎么手动编译呢 ?