Java-网上购物中用Session实现的购物车

时间:2022-12-29 18:04:33

首先创建一个购物车

package www.csdn.cart;

import java.util.ArrayList;
import java.util.List;

/**
* BuyCart 2013-5-23 下午3:30:20 购物车
*
* @author 乔晓松
*
*/
public class BuyCart {

// 购物项
private List<BuyItem> items = new ArrayList<BuyItem>();

/**
* 添加购物项
*
* @param item
*/
public void addBuyItem(BuyItem item) {
if (items.contains(item)) {
// 累加购买数量
for (BuyItem bitem : items) {
if (bitem.equals(item)) {
bitem.setAmount(bitem.getAmount() + 1);
break;
}
}
} else {
items.add(item);
}
}

public List<BuyItem> getItems() {
return items;
}

public void setItems(List<BuyItem> items) {
this.items = items;
}

}


 

接着,创建购物项

package www.csdn.cart;

import www.csdn.domain.ProductInfo;

/**
* BuyItem 2013-5-23 下午3:30:36 购物项
*
* @author 乔晓松
*
*/
public class BuyItem {
// 商品
private ProductInfo product;
// 商品数量
private int amount;

public ProductInfo getProduct() {
return product;
}

public void setProduct(ProductInfo product) {
this.product = product;
}

public int getAmount() {
return amount;
}

public void setAmount(int amount) {
this.amount = amount;
}

@Override
public int hashCode() {
/**
* 对hashCode重写,并改造一下hashCode,把商品的Id和商品类型的Id组成并取hashCode哈希码
*/
String result = product.getId() + "-"
+ product.getStyles().iterator().next().getSid();
return result.hashCode();
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BuyItem other = (BuyItem) obj;
if (product == null) {
if (other.product != null)
return false;
} else if (!product.equals(other.product))
return false;
if (product.getStyles().size() != other.getProduct().getStyles().size())
return false;
Integer styleid = product.getStyles().iterator().next().getSid();
Integer otherstyleid = other.getProduct().getStyles().iterator().next()
.getSid();
if (!styleid.equals(otherstyleid))
return false;
return true;
}

}


商品类:

package www.csdn.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
* ProductInfo 2013-5-23 下午3:49:46
*
* @author 乔晓松
*
*/
public class ProductInfo implements Serializable {

/**
* 商品信息
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Double price;
private String decript;
private Set<Style> styles = new HashSet<Style>();

public ProductInfo() {
super();
}

public ProductInfo(Integer id) {
super();
this.id = id;
}

public ProductInfo(Integer id, String name, Double price, String decript,
Set<Style> styles) {
super();
this.id = id;
this.name = name;
this.price = price;
this.decript = decript;
this.styles = styles;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public String getDecript() {
return decript;
}

public void setDecript(String decript) {
this.decript = decript;
}

public Set<Style> getStyles() {
return styles;
}

public void setStyles(Set<Style> styles) {
this.styles = styles;
}

public void addProductStyle(Style style) {
this.styles.add(new Style());
}

}


商品颜色类型:

package www.csdn.domain;

import java.io.Serializable;

/**
* Style 2013-5-23 下午3:47:04
*
* @author 乔晓松
*/
public class Style implements Serializable {

/**
* 样式信息
*/
private static final long serialVersionUID = 1L;
// 样式ID
private Integer sid;
// 样式名称
private String sname;

public Style() {
super();
}

public Style(Integer sid) {
super();
this.sid = sid;
}

public Style(Integer sid, String sname) {
super();
this.sid = sid;
this.sname = sname;
}

public Integer getSid() {
return sid;
}

public void setSid(Integer sid) {
this.sid = sid;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

}


 

 

测试类:

package junit.test;

import org.junit.Test;

import www.csdn.cart.BuyCart;
import www.csdn.cart.BuyItem;
import www.csdn.domain.ProductInfo;
import www.csdn.domain.Style;

/**
* BuyCartTest 2013-5-23 下午4:38:12
*
* @author 乔晓松
*
*/
public class BuyCartTest {

/**
* 测试购物车是否能添加商品
*/
@Test
public void save1() {
// 创建购物车
BuyCart cart = new BuyCart();
// 创建购物项
BuyItem item = new BuyItem();
ProductInfo product = new ProductInfo(20);
product.addProductStyle(new Style(90));
item.setProduct(product);
cart.addBuyItem(item);

// 输出购物车中的商品的Id和类型的Id
for (BuyItem bitem : cart.getItems()) {
System.out
.println(bitem.getProduct().getId()
+ "------------"
+ bitem.getProduct().getStyles().iterator().next()
.getSid());
}
}

/**
* 测试添加相同的商品和类型,购物车中商品的数量是否累加
*/
@Test
public void save2() {
BuyCart cart = new BuyCart();
BuyItem item = new BuyItem();
ProductInfo product = new ProductInfo(30);
product.addProductStyle(new Style(80));
item.setProduct(product);
cart.addBuyItem(item);

BuyItem item2 = new BuyItem();
ProductInfo product2 = new ProductInfo(30);
product2.addProductStyle(new Style(80));
item2.setProduct(product2);
cart.addBuyItem(item2);

// 输出购物车中的商品的Id和类型的Id
for (BuyItem bitem : cart.getItems()) {
System.out
.println(bitem.getProduct().getId()
+ "------------"
+ bitem.getProduct().getStyles().iterator().next()
.getSid());
}
}
}