[color=black][size=small]package com.shopping.dao.impl;
import java.util.HashMap;
import java.util.Map;
import com.shopping.po.CartItem;
public class ShoppingCart {
private static final ShoppingCart CART = new ShoppingCart();
private Map<String, CartItem> map = new HashMap<String, CartItem>();
// 单例模式
public static ShoppingCart getInstance() {
return CART;
}
public Map<String, CartItem> getItems() {
return map;
}
// 初始化HashMap
public void setItems(Map<String, CartItem> map) {
this.map = map;
}
// 把数据添加到HashMap中
public void addItem(String itemId, CartItem ct) {
if (!map.containsKey(itemId)) {
map.put(itemId, ct);
}
this.setItems(map);
}
// 从HashMap中删除数据
public void removeItem(String itemId) {
map.remove(itemId);
}
// 修改HashMap中的数据
public void updateItem(String itemId, int qty) {
if (map.containsKey(itemId)) {
CartItem ct = (CartItem) map.get(itemId);
ct.setQuantity(qty);
ct.setTotal();
}
}
// 清空HashMap中所有的数据
public void clearItem() {
map.clear();
}
}
////////////////////////////////////////////////////////////////////
package com.shopping.po;
public class CartItem {
private String productId = ""; // 商品编号
private String productName = ""; // 商品名称
private double price; // 价格
private double total; // 总金额
private int quantity; // 订购数量
private int stock; // 库存
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getTotal() {
return total;
}
public void setTotal() {
this.total = this.getPrice() * this.getQuantity();
}
/*public void caculateItem() {
}*/
}[/size][/color]
import java.util.HashMap;
import java.util.Map;
import com.shopping.po.CartItem;
public class ShoppingCart {
private static final ShoppingCart CART = new ShoppingCart();
private Map<String, CartItem> map = new HashMap<String, CartItem>();
// 单例模式
public static ShoppingCart getInstance() {
return CART;
}
public Map<String, CartItem> getItems() {
return map;
}
// 初始化HashMap
public void setItems(Map<String, CartItem> map) {
this.map = map;
}
// 把数据添加到HashMap中
public void addItem(String itemId, CartItem ct) {
if (!map.containsKey(itemId)) {
map.put(itemId, ct);
}
this.setItems(map);
}
// 从HashMap中删除数据
public void removeItem(String itemId) {
map.remove(itemId);
}
// 修改HashMap中的数据
public void updateItem(String itemId, int qty) {
if (map.containsKey(itemId)) {
CartItem ct = (CartItem) map.get(itemId);
ct.setQuantity(qty);
ct.setTotal();
}
}
// 清空HashMap中所有的数据
public void clearItem() {
map.clear();
}
}
////////////////////////////////////////////////////////////////////
package com.shopping.po;
public class CartItem {
private String productId = ""; // 商品编号
private String productName = ""; // 商品名称
private double price; // 价格
private double total; // 总金额
private int quantity; // 订购数量
private int stock; // 库存
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getTotal() {
return total;
}
public void setTotal() {
this.total = this.getPrice() * this.getQuantity();
}
/*public void caculateItem() {
}*/
}[/size][/color]