没有合适的资源?快使用搜索试试~ 我知道了~
14_尚硅谷_书城项目_第六、七阶段_王振国 - 课堂笔记1
需积分: 0 4 下载量 92 浏览量
2022-08-04
14:50:39
上传
评论
收藏 1.12MB PDF 举报
温馨提示
试读
19页
1.1、购物车模块分析 1.2、购物车模型编写 1.2.1、购物车模型: 1.2.2、购物车的测试: 1.3、加入购物车功能的实现 1.4、购物车的展示
资源详情
资源评论
资源推荐
14-书城项目第六、七阶段
讲师:王振国
今日任务
1、项目第六阶段:购物车
1.1、购物车模块分析
1.2
、购物车模型编写
1.2.1、购物车模型:
/**
*
购物车的商品项
*/
public class CartItem {
private Integer id;
private String name;
private Integer count;
private BigDecimal price;
private BigDecimal totalPrice;
/**
*
购物车对象
*/
public class Cart {
// private Integer totalCount;
// private BigDecimal totalPrice;
/**
* key
是商品编号,
* value
,是商品信息
*/
private Map<Integer,CartItem> items = new LinkedHashMap<Integer,CartItem>();
/**
*
添加商品项
*
* @param cartItem
*/
public void addItem(CartItem cartItem) {
//
先查看购物车中是否已经添加过此商品,如果已添加,则数量累加,总金额更新,如果没有添加过,直接放到
集合中即可
CartItem item = items.get(cartItem.getId());
if (item == null) {
//
之前没添加过此商品
items.put(cartItem.getId(), cartItem);
} else {
//
已经 添加过的情况
item.setCount( item.getCount() + 1 ); //
数量 累加
item.setTotalPrice( item.getPrice().multiply(new BigDecimal( item.getCount() )) ); //
更
新总金额
}
}
/**
*
删除商品项
*/
public void deleteItem(Integer id) {
items.remove(id);
}
/**
*
清空购物车
*/
public void clear() {
items.clear();
}
/**
*
修改商品数量
*/
public void updateCount(Integer id,Integer count) {
//
先查看购物车中是否有此商品。如果有,修改商品数量,更新总金额
CartItem cartItem = items.get(id);
if (cartItem != null) {
cartItem.setCount(count);//
修改商品数量
cartItem.setTotalPrice( cartItem.getPrice().multiply(new
BigDecimal( cartItem.getCount() )) ); //
更新总金额
}
}
public Integer getTotalCount() {
Integer totalCount = 0;
for (Map.Entry<Integer,CartItem>entry : items.entrySet()) {
totalCount += entry.getValue().getCount();
}
return totalCount;
}
public BigDecimal getTotalPrice() {
BigDecimal totalPrice = new BigDecimal(0);
for (Map.Entry<Integer,CartItem>entry : items.entrySet()) {
totalPrice = totalPrice.add(entry.getValue().getTotalPrice());
}
return totalPrice;
}
public Map<Integer, CartItem> getItems() {
return items;
}
public void setItems(Map<Integer, CartItem> items) {
this.items = items;
}
@Override
public String toString() {
return "Cart{" +
"totalCount=" + getTotalCount() +
", totalPrice=" + getTotalPrice() +
", items=" + items +
'}';
}
}
1.2.2、购物车的测试:
public class CartTest {
@Test
public void addItem() {
Cart cart = new Cart();
cart.addItem(new CartItem(1, "java 从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
cart.addItem(new CartItem(1, "java 从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
cart.addItem(new CartItem(2, "数据结构与算法", 1, new BigDecimal(100),new BigDecimal(100)));
System.out.println(cart);
}
@Test
public void deleteItem() {
Cart cart = new Cart();
cart.addItem(new CartItem(1, "java 从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
cart.addItem(new CartItem(1, "java 从入门到精通", 1, new BigDecimal(1000),new BigDecimal(1000)));
cart.addItem(new CartItem(2, "数据结构与算法", 1, new BigDecimal(100),new BigDecimal(100)));
cart.deleteItem(1);
System.out.println(cart);
}
@Test
public void clear() {
Cart cart = new Cart();
剩余18页未读,继续阅读
学习呀三木
- 粉丝: 22
- 资源: 303
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0