购物车模块

[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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值