wangpan80 发表于 2013-2-4 20:06:40

购物车

采用javabean+jsp方式,将javabean的scope的属性设为session. 使其在整个会话中有效.
以下类的说明。

1.cart.class
 
方法:
    addCart(int id,   int pro_c,    float price,float price1, String name, 
String bei) //加入产品. 产品ID,定购数量,原价,现价,产品名称,备注

    getCart()  //返回一个Hashtable 数据类开型。
 
    getCartprice() //返回一个float数据类型 (购物车的总价)。
 
    modCount(String id,  int cou)  //修改一个产品的定购数量。产品ID,定购数量

    removeCart(String id) //删除定购的产品。
 
2.msgCart.class
 
 方法:
      setCart(int id, int pro_c,    float price,  float price1,String
name,String bei)  //加入产品,产品ID,定购数量,原价,现价,产品名称,备注
 
      getId()  //返回产品的ID号, int 类型。
 
      getProcount()  //返回产品的定购数量。int 类型。
 
      getPrice()     //返回产品原价。float 类型。
 
      getPrice1()   //返回产品现价。float 类型。
 
      getPricecount()  //返回每个产品的订购总价(以现价为准)。float 类型。
 
      getProname()     //返回每个产品的名字。
 
      getProbei()      //返回每个产品注泽。(比如:打折信息)
 
      clearCart()     //清除表。
 
 
-------------------------
以下是源程序。
 
//主程序
 
package cart;
import java.util.*;
 
public class cart{
 
public Hashtable items;
 
 
public cart()
 {
   items = new Hashtable();
  
 
  }
 
public void addCart(int id,int pro_c,float price,float price1,String name,String
bei)
  {
   String id_=new String(id+"");
  
   if(items.containsKey(id_))
      {
     
        msgCart m_cart=new msgCart();
        m_cart=(msgCart)items.get(id_);
        m_cart.addPro(pro_c);
      }
   else
      {
         msgCart m_cart=new msgCart();
         items.put(id_,m_cart);
         m_cart.setCart(id,pro_c,price,price1,name,bei);
       
       }
    }
   
public void modCount(String id,int cou)
   {
      if(items.containsKey(id))
      {
     
        msgCart m_cart=new msgCart();
        m_cart=(msgCart)items.get(id);
        m_cart.modPro(cou);
      }
      else
       {
         System.out.print("no modify pc_count");
        }
  
    }
 
public void removeCart(String id)
  {
    items.remove(id);
   }
  
public void clearCart()
  {
    items.clear();
   }
 
public Hashtable getCart()
  {
    return items;
   }
 
public float getCartprice()
  {
    Enumeration enum=items.elements();
    msgCart m_cart=new msgCart();
    float count=0.0f;
    while(enum.hasMoreElements())
      {
        m_cart=(msgCart)enum.nextElement();
        count+=m_cart.getPricecount();
      }
    return count;
   }
      
}
 
 
 
// 产品信息类。
package cart;
 
import java.util.*;
 
public class msgCart {
 
public int pro_count=0; 
public float price=0;
public float price1=0;
public int id;
public String pro_name;
public String pro_bei;
 
 
  public msgCart()
   {
    }
  public void setCart(int id,int pro_c,float price,float price1,String
name,String bei)
    {
      this.pro_count=pro_c;
      this.price=price;
      this.price1=price1;
      this.id=id;
      this.pro_name=name;
      this.pro_bei=bei;
    }
   
   
public void addPro(int pc)
 {
   pro_count+=pc;   
  }
 
 
public void modPro(int mc)
  {
    pro_count=mc;
  }
   
  public int getId()
   {
     return id;
    }
  public int getProcount()
   {
     return pro_count;
    }
       
 public float getPrice()
   {
     return price;
    }
 public float getPrice1()
   {
     return price1;
    } 
 public float getPricecount()
   {
     return price1*pro_count;
    }
public String getProname()
   {
     return pro_name;
    }
   {
     return pro_bei;
    }      
 
    
}
 
 
//以下是一个jsp文件的示例。
 
 
<%@ page contentType="text/html; charset=gb2312" language="java"
import="java.sql.*,java.util.*" errorPage="" %>
 
<html>
<head>
<title>sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<jsp:useBean id="my_cart" scope="session" class="cart.cart" />
<body>
<%
my_cart.addCart(12,2,78.23f,7.23f,"my cart","bei");
my_cart.addCart(120,2,78.23f,7.23f,"my cart","bei");
my_cart.addCart(121,2,78.23f,7.23f,"my cart","bei");
my_cart.addCart(122,2,78.23f,7.23f,"my cart","bei");
out.print("ok");
 
Hashtable cart1=new Hashtable();
cart1=my_cart.getCart();
 
cart.msgCart mcart=new cart.msgCart();
mcart=(cart.msgCart)cart1.get("120");
 
out.print(mcart.getId()+"<br>");
out.print(mcart.getProcount()+"<br>");
out.print(mcart.getProname()+"<br>");
out.print(mcart.getProbei()+"<br>");
out.print(mcart.getPricecount()+"<br>");
out.print("总计:"+my_cart.getCartprice());
 %>
 
</body>
</html> 
页: [1]
查看完整版本: 购物车