应用JavaBean实现购物车.docVIP

  • 34
  • 0
  • 约1.04万字
  • 约 20页
  • 2017-08-18 发布于重庆
  • 举报
应用JavaBean实现购物车.doc

应用Servlet实现购物车 具体实现过程 创建封装商品信息的值JavaBeanGoodsSingle package com.yxq.valuebean; public class GoodsSingle { private String name; //保存商品名称 private float price; //保存商品价格 private int num; //保存商品购买数量 public String getName() { return name; } public void setName(String name) { = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } } 创建工具JavaBean MyTools 实现字符型数据转换为整型及乱码处理 package com.yxq.toolbean; import java.io.UnsupportedEncodingException; public class MyTools { public static int strToint(String str){ //将String型数据转换为int型数据的方法 if(str==null||str.equals()) str=0; int i=0; try{ i=Integer.parseInt(str); //把str 转换成 int 类型的变量 }catch(NumberFormatException e){ // try-catch就是监视try中的语句,如果抛出catch中声明的异常类型 i=0; e.printStackTrace(); //把Exception的详细信息打印出来 } return i; } public static String toChinese(String str){ //进行转码操作的方法 if(str==null) str=; try { str=new String(str.getBytes(ISO-8859-1),gb2312); } catch (UnsupportedEncodingException e) { str=; e.printStackTrace(); } return str; } } 创建购物车JavaBean ShopCar实现添加、删除,购物车制作 package com.yxq.toolbean; package com.yxq.toolbean; import java.util.ArrayList; import com.yxq.valuebean.GoodsSingle; public class ShopCar { private ArrayList buylist=new ArrayList(); //用来存储购买的商品 public void setBuylist(ArrayList buylist) { this.buylist = buylist; } /** * @功能 向购物车中添加商品 * @参数 single为GoodsSingle类对象,封装了要添加的商品信息 */ public void addItem(GoodsSingle single){ if(single!=null){ if(buylist.size()==0){ //如果buylist中不存在任何商品 GoodsSingle temp=new GoodsSingle(); temp.setName(single.getName()); temp.setPrice(single.getPrice()); temp.setNum(single.getNum()); buylist.add(temp); //存储商品 } else{ //如果buylist中存在商品 int i=0;

文档评论(0)

1亿VIP精品文档

相关文档