package org.whatisjava.dangdang.bo;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.whatisjava.dangdang.dao.DaoFactory;
import org.whatisjava.dangdang.dao.ProductDao;
import org.whatisjava.dangdang.domain.Product;
import org.whatisjava.dangdang.util.DaoException;
public class Cart implements Serializable {
private static final long serialVersionUID = 1L;
private static Logger logger = Logger.getLogger(Cart.class);
private Integer userId;
// CartItem.product.id
private HashMap<Integer, CartItem> items;
/**
*
* @param user
*/
public Cart(Integer userId) {
this.userId = userId;
items = new HashMap<Integer, CartItem>();
}
public Integer getUserId() {
return userId;
}
/**
* 如果Cart中已有该item,则不添加新的item,只将原有item中产品的数量加1
* @param product
* @return
*/
public boolean addItem(Integer productId) throws DaoException {
if (!items.containsKey(productId)) {
CartItem item=new CartItem();
ProductDao dao=DaoFactory.getProductDao();
Product p=dao.findProductById(productId);
item.setProduct(p);
item.setNumber(1);
item.setDrop(false);
items.put(p.getId(), item);
return true;
}else{
CartItem item=items.get(productId);
item.setNumber(item.getNumber()+1);
return false;
}
}
/**
*
* @param productId
* @param num
* @return
*/
public boolean updateNum(Integer productId, int num) {
if(items.containsKey(productId)){
if(num<=0){
return this.drop(productId);
}
CartItem item=items.get(productId);
item.setNumber(num);
return true;
}
return false;
}
/**
*
* @param productId
* @return
*/
public boolean drop(Integer productId) {
if(items.containsKey(productId)){
CartItem item=items.get(productId);
item.setDrop(true);
return true;
}
return false;
}
/**
*
* @param productId
* @return
*/
public boolean recovery(Integer productId) {
if(items.containsKey(productId)){
CartItem item=items.get(productId);
item.setDrop(false);
return true;
}
return false;
}
/**
*
*/
public void clear() {
items.clear();
}
/**
* 把购物车的信息转换为字符串,用于Cookie存儲
* @return
* @throws UnsupportedEncodingException
*/
public String serialize() {
StringBuilder buffer=new StringBuilder();
buffer.append(userId).append("@");
if(items.isEmpty()){
buffer.append("0");
}else{
Iterator<Integer> it=items.keySet().iterator();
while(it.hasNext()){
Integer productId=it.next();
int num=items.get(productId).getNumber();
boolean drop=items.get(productId).isDrop();
buffer.append(productId+","+num+","+drop);
buffer.append(";");
}
}
if(buffer.length()>0)
buffer.deleteCharAt(buffer.length()-1);
logger.debug(buffer);
return buffer.toString();
}
/**
* 将Cookie中的字符串拆分成对象的属性
* @param content
* @throws DaoException
* @throws UnsupportedEncodingException
*/
public void load(String content) {
this.items.clear();
String userId=content.substring(0,content.indexOf("@"));
if(userId !=null && Integer.parseInt(userId)==this.userId){
content=content.substring(content.indexOf("@")+1);
if(content==null || "0".equals(content))
return;
if(content.length()>0){
String[] itemsArray=content.split(";");
for(int i=0;i<itemsArray.length;i++){
String[] itemArray=itemsArray[i].split(",");
logger.debug(Arrays.toString(itemsArray));
Integer productId=Integer.parseInt(itemArray[0]);
int num=Integer.parseInt(itemArray[1]);
boolean drop=Boolean.parseBoolean(itemArray[2]);
ProductDao dao=DaoFactory.getProductDao();
Product product=dao.findProductById(productId);
CartItem item=new CartItem();
item.setProduct(product);
item.setNumber(num);
item.setDrop(drop);
items.put(product.getId(),item);
logger.debug(items);
}
}
}
}
/**
*
* @return
*/
public boolean isEmpty() {
return items.isEmpty();
}
/**
*
* @return
*/
public List<CartItem> getItems() {
List<CartItem> list = new ArrayList<CartItem>();
Iterator<Integer> it = items.keySet().iterator();
while (it.hasNext()) {
CartItem item = items.get(it.next());
if (!item.isDrop()) {
list.add(item);
}
}
return list;
}
/**
*
* @return
*/
public List<CartItem> getDropItems() {
List<CartItem> list = new ArrayList<CartItem>();
Iterator<Integer> it = items.keySet().iterator();
while (it.hasNext()) {
CartItem item = items.get(it.next());
if (item.isDrop()) {
list.add(item);
}
}
return list;
}
/**
*
* @return
*/
public double getTotalPrice() {
double totalPrice = 0;
Iterator<Integer> it=items.keySet().iterator();
while(it.hasNext()){
CartItem item=items.get(it.next());
if(!item.isDrop()){
totalPrice+=item.getProduct().getFixedPrice()*item.getNumber();
}
}
return totalPrice;
}
/**
*
* @return
*/
public double getDangPrice() {
double totalPrice = 0;
Iterator<Integer> it=items.keySet().iterator();
while(it.hasNext()){
CartItem item=items.get(it.next());
if(!item.isDrop()){
totalPrice+=item.getProduct().getDangPrice()*item.getNumber();
}
}
return totalPrice;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
web开发项目:网上购物系统 (595个子文件)
Cart.class 6KB
RegisterAction.class 5KB
OrderAction.class 4KB
CartAction.class 4KB
ImageServlet.class 4KB
UserDaoImp.class 3KB
Book.class 3KB
Order.class 3KB
BookDaoImp.class 3KB
LoginAction.class 3KB
User.class 3KB
Product.class 3KB
BookListFormAction.class 3KB
OrderDaoImp.class 2KB
CookieUtil.class 2KB
Category.class 2KB
ProductDaoImp.class 2KB
CategoryFormAction.class 2KB
Item.class 2KB
LogoutAction.class 2KB
TestProductDao.class 2KB
AuthorizeUtil.class 2KB
LoginStateAction.class 2KB
BookListForm.class 1KB
AuthorizeMappingDispatchAction.class 1KB
TestCategoryDao.class 1KB
AddressForm.class 1KB
Address.class 1KB
RegisterForm.class 1KB
DaoFactory.class 1KB
CartItem.class 1KB
HibernateSessionFactory.class 1KB
TestBookDao.class 999B
CategoryDaoImp.class 999B
MD5Util.class 956B
CartItemForm.class 802B
LoginForm.class 788B
VerifyForm.class 627B
DaoException.class 623B
UUIDUtil.class 570B
Constants.class 537B
BookDao.class 525B
UserDao.class 514B
ProductDao.class 289B
OrderDao.class 272B
CategoryDao.class 223B
.classpath 2KB
book.css 25KB
second.css 20KB
register_login.css 14KB
book_head090107.css 13KB
login.css 13KB
book1.css 11KB
comm_answer.css 10KB
list.css 10KB
shopping_vehicle.css 9KB
shopping_head.css 5KB
booksaleslist.css 5KB
page_bottom.css 3KB
public_footer.css 2KB
research.css 2KB
product_exp.css 1KB
secBook_Show.css 1KB
navi.css 1KB
pubsug090107.css 555B
Thumbs.db 45KB
Thumbs.db 34KB
Thumbs.db 14KB
Thumbs.db 12KB
Thumbs.db 7KB
Thumbs.db 7KB
Thumbs.db 6KB
Thumbs.db 4KB
Thumbs.db 4KB
Entries 8KB
Entries 2KB
Entries 2KB
Entries 710B
Entries 674B
Entries 383B
Entries 353B
Entries 345B
Entries 340B
Entries 247B
Entries 195B
Entries 192B
Entries 146B
Entries 98B
Entries 84B
Entries 54B
Entries 53B
Entries 46B
Entries 45B
Entries 43B
Entries 42B
Entries 12B
Entries 12B
ancz081216_button05.gif 25KB
xssj_banner.gif 8KB
d_introduce.gif 5KB
共 595 条
- 1
- 2
- 3
- 4
- 5
- 6
可可keketrtr
- 粉丝: 109
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页