package org.whatisjava.dang.service;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.whatisjava.dang.dao.DaoFactory;
import org.whatisjava.dang.dao.ProductDao;
import org.whatisjava.dang.domain.CartItem;
import org.whatisjava.dang.domain.Product;
import org.whatisjava.dang.domain.User;
import org.whatisjava.dang.util.DaoException;
public class CartService implements Serializable {
private static final long serialVersionUID = 1L;
private ProductDao productDao = DaoFactory.getProductDao();
private static Logger logger = Logger.getLogger(CartService.class);
private User user;
private HashMap<Integer, CartItem> items;
/**
*
* @param user
*/
public CartService(User user) {
logger.debug(user.getNickname());
this.user = user;
this.items = new HashMap<Integer, CartItem>();
}
public User getUser() {
return user;
}
/**
*
* @param product
* @return
*/
public boolean addItem(Integer productId) throws DaoException {
if (!items.containsKey(productId)) {
CartItem item = new CartItem();
Product product = productDao.getProduct(productId);
item.setProduct(product);
item.setNumber(1);
item.setDrop(false);
items.put(product.getId(), item);
logger.debug(items);
return true;
}
return false;
}
/**
*
* @param productId
* @param num
* @return
*/
public boolean updateNum(Integer productId, int num) {
if (items.containsKey(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();
}
/**
*
* @return
* @throws UnsupportedEncodingException
*/
public String serialize() throws UnsupportedEncodingException {
StringBuilder buffer = new StringBuilder();
buffer.append(user.getId()).append("@");
if (items.isEmpty())
buffer.append("0");
Iterator<Integer> it = items.keySet().iterator();
while (it.hasNext()) {
Integer id = it.next();
int num = items.get(id).getNumber();
boolean drop = items.get(id).isDrop();
buffer.append(id + "," + num + "," + drop);
buffer.append(";");
}
if (buffer.length() > 0)
buffer.deleteCharAt(buffer.length() - 1);
logger.debug(buffer);
return buffer.toString();
}
/**
*
* @param content
* @throws DaoException
* @throws UnsupportedEncodingException
*/
public void load(String content) throws DaoException,
UnsupportedEncodingException {
items.clear();
String userId = content.substring(0, content.indexOf("@"));
if (userId != null && Integer.parseInt(userId) == user.getId()) {
content = content.substring(content.indexOf("@") + 1);
if (content == null || "0".equals(content))
return;
if (content != null && content.length() > 0) {
String[] itemsArry = content.split(";");
for (int i = 0; i < itemsArry.length; i++) {
String[] itemArry = itemsArry[i].split(",");
logger.debug(Arrays.toString(itemArry));
Integer id = Integer.valueOf(itemArry[0]);
int number = Integer.parseInt(itemArry[1]);
boolean drop = Boolean.parseBoolean(itemArry[2]);
Product product = productDao.getProduct(id);
CartItem item = new CartItem();
item.setProduct(product);
item.setNumber(number);
item.setDrop(drop);
items.put(product.getId(), item);
logger.debug(items);
}
}
}
}
/**
*
* @return
*/
public boolean isEmpty() {
return items.isEmpty();
}
/**
*
* @return
*/
public List<CartItem> getItems() {
ArrayList<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);
}
}
Collections.sort(list);
return list;
}
/**
*
* @return
*/
public List<CartItem> getDropItems() {
ArrayList<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);
}
}
Collections.sort(list);
return list;
}
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;
}
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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
山寨版当当网 struts+hibernate
共466个文件
gif:221个
jpg:63个
class:45个
需积分: 0 19 下载量 134 浏览量
2009-04-03
21:57:15
上传
评论 2
收藏 1.08MB ZIP 举报
温馨提示
山寨版当当网 struts+hibernate
资源详情
资源评论
资源推荐
收起资源包目录
山寨版当当网 struts+hibernate (466个子文件)
CartService.class 7KB
CartAction.class 4KB
RegisterAction.class 4KB
BookDaoImpl.class 3KB
CategoryDaoImpl.class 3KB
UserDaoImpl.class 3KB
AuthorizeService.class 3KB
Book.class 3KB
LoginAction.class 3KB
Product.class 3KB
CookieUtils.class 3KB
CatalogService.class 3KB
Category.class 2KB
User.class 2KB
ProductDaoImpl.class 2KB
BookListAction.class 2KB
CategoryAction.class 2KB
TestHibernate.class 2KB
LogoutAction.class 2KB
LoginInfoAction.class 2KB
CartItem.class 2KB
AuthorizeUtils.class 2KB
AllCategoryAction.class 2KB
HbSessionFactory.class 1KB
ShoppingController.class 1KB
UserForm.class 1KB
Main.class 1KB
ListAction.class 1KB
LoginFormAction.class 1003B
MainAction.class 988B
Recommend.class 985B
MD5Utils.class 979B
DaoFactory.class 968B
New.class 967B
Hot.class 967B
CartItemForm.class 858B
UUIDUtils.class 837B
EmailUtils.class 643B
DaoException.class 608B
CodeUtils.class 565B
BookDao.class 537B
CategoryDao.class 512B
UserDao.class 450B
Constants.class 429B
ProductDao.class 272B
.classpath 3KB
book.css 25KB
second.css 20KB
register_login.css 14KB
book_head090107.css 13KB
login.css 13KB
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
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 6KB
Thumbs.db 4KB
Thumbs.db 4KB
ancz081216_button05.gif 25KB
xssj_banner.gif 8KB
d_introduce.gif 5KB
16.gif 5KB
18.gif 4KB
cheap4.gif 4KB
cheap3.gif 4KB
cheap2.gif 4KB
cheap1.gif 3KB
13.gif 3KB
logo.gif 3KB
logo.gif 3KB
window_loading.gif 2KB
book_logo.gif 2KB
back_window.gif 2KB
title_bg.gif 2KB
pic_myshopping.gif 2KB
butt_balance.gif 2KB
ddb_1.gif 2KB
label3.gif 1KB
book_dangdang.gif 1KB
bang.gif 1KB
ddb_2.gif 1KB
r-r3new.gif 1KB
validate.gif 1KB
validate.gif 1KB
dot_sale.gif 1KB
dot_shanggan.gif 1KB
dot_gandong.gif 1024B
dot_shuzhuopang.gif 1023B
butt_buy.gif 1022B
共 466 条
- 1
- 2
- 3
- 4
- 5
zhengyong8797
- 粉丝: 3
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0