07-06
08
利用面向对象思想来做购物车
作者:Java伴侣 日期:2007-06-08
复制内容到剪贴板 程序代码
/**
* <p>Title: 购物车</p>
* <p>Description: 实现对商城购物车的操作</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: 公司</p>
* @author blurxx
* @version 1.1
*/
package order;
import java.util.Hashtable;
import org.apache.log4j.Logger;
import java.util.Enumeration;
import order.vo.ProductCart;
public class orderBean {
Logger log = Logger.getLogger(OrderBean.class);
public Hashtable itemHashtable = new Hashtable();
public void setItemHashtable(Hashtable ht) {
itemHashtable = ht;
}
public Hashtable getItemHashtable() {
return itemHashtable;
}
/**
*
* @添加商品
*/
public void addItem(ProductCart pt) {
int id = pt.getId();
//String item[]={Integer.toString(id),name,Double.toString(price),Integer.toString(entries)};
//如果购物车中有此ID号
if (itemHashtable.containsKey(Integer.toString(id))) {
//取得原有此种类型商品集合
ProductCart workItem = (ProductCart) itemHashtable.get(Integer.
toString(
id));
//取得原来条目
int oldentries = workItem.getEntries();
//加上新加条目
int workEntries = pt.getEntries() + oldentries;
//总和放入pt对象中更新
pt.setEntries(workEntries);
//把此类型商品再加入购物车
itemHashtable.put(Integer.toString(id), pt);
} else {
itemHashtable.put(Integer.toString(id), pt);
}
}
public void updateItem(ProductCart pt) {
int id = pt.getId();
//String item[]={Integer.toString(id),name,Double.toString(price),Integer.toString(entries)};
if (itemHashtable.containsKey(Integer.toString(id))) {
//取得条目
int workEntries = pt.getEntries();
pt.setEntries(workEntries);
itemHashtable.put(Integer.toString(id), pt);
} else {
itemHashtable.put(Integer.toString(id), pt);
}
}
/**
* 删除某一类商品
* @return null
*/
public void ItemdelByKey(int id) {
//int id=pt.getId();
//String item[]={Integer.toString(id),name,Double.toString(price),Integer.toString(entries)};
if (itemHashtable.containsKey(Integer.toString(id))) {
itemHashtable.remove(Integer.toString(id));
} else {
return;
}
}
/**
* 价格总和
* @return double
*/
public double getCost() {
Enumeration enuma = itemHashtable.elements();
ProductCart workItem;
double totalCost = 0.00;
while (enuma.hasMoreElements()) {
workItem = (ProductCart) enuma.nextElement();
//总和=个数(条目)*单价
totalCost = totalCost +
workItem.getEntries() *
workItem.getPrice();
}
return totalCost;
}
/**
* 购物车商品类数 //总共多少件商品
* @return int
*/
public int getNumofItem() {
Enumeration enuma = itemHashtable.elements();
ProductCart workItem;
int num = 0;
while (enuma.hasMoreElements()) {
workItem = (ProductCart) enuma.nextElement();
num = num + workItem.getEntries();
}
return num;
}
/**
* 清空购物车记录
*/
public void ClearItem() {
itemHashtable.clear();
}
public static void main(String[] args) {
orderBean orderbean = new orderBean();
}
}
这大概是春节前写的代码,节选自商品购物系统。
最早是利用数组来存商品,后来改进成存一个个商品对象VO。那时思维还是蛮清晰的,比现在好多了—_—!||
补充ProductCart类:
复制内容到剪贴板 程序代码
package order.vo;
/**
* 用于作为购物车中的值传递
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.io.Serializable;
public class ProductCart implements Serializable {
private int id;
private String name;
private double price;
private int entries;
public static void main(String[] args) {
ProductCart productcart = new ProductCart();
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setEntries(int entries) {
this.entries = entries;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getEntries() {
return entries;
}
}
/**
* 用于作为购物车中的值传递
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
import java.io.Serializable;
public class ProductCart implements Serializable {
private int id;
private String name;
private double price;
private int entries;
public static void main(String[] args) {
ProductCart productcart = new ProductCart();
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setEntries(int entries) {
this.entries = entries;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getEntries() {
return entries;
}
}
评论: 3 | 引用: 0 | 查看次数: 718
朋友 能把你这个购物车 发给我吗
谢谢了
我的油箱是quansheng2580.student@sina.com
谢谢了
我的油箱是quansheng2580.student@sina.com
朋友 先谢谢你写这么好的东西 我想知道ProductCart类里的内容 可以吗
那个类应该是个实体bean 把
那个类应该是个实体bean 把
发表评论
那个类应该是个实体bean 把
ProductCart就是个值对象,里面是一些get/set方法