⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cartservice.java

📁 模仿当当网基于struts+hierbernate与mysql的商务网站。
💻 JAVA
字号:
package org.whatisjava.dang.service;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
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.ProductDao;
import org.whatisjava.dang.domain.CartItem;
import org.whatisjava.dang.domain.Product;
import org.whatisjava.dang.domain.User;

public class CartService implements Serializable {
	private static final long serialVersionUID = 1L;

	private ProductDao productDao = new ProductDao();

	private static Logger logger = Logger.getLogger(CartService.class);

	private User user;

	private HashMap<Integer, CartItem> items;

	/**
	 * 
	 * @param user
	 */
	public CartService(User user) {
		logger.info("CartService(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) {
		logger.info("addItem(Integer)");
		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
	 * @throws WrongNumberException 
	 */
	public boolean updateNum(Integer productId, int num) throws WrongProductNumException {
		if(num<0){
			throw new WrongProductNumException("商品购买数目不能是负数。",productId);
		}
		if (items.containsKey(productId)) {
			CartItem item = items.get(productId);
			item.setNumber(num);
			return true;
		}
		return false;
	}

	/**
	 * 
	 * @param productId
	 * @return
	 */
	public boolean drop(Integer productId) {
		logger.info("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) {
		logger.info("recovery(Integer productId)");
		if (items.containsKey(productId)) {
			CartItem item = items.get(productId);
			item.setDrop(false);
			return true;
		}
		return false;
	}

	public void clear() {
		logger.info("clear()");
		items.clear();
	}

	/**
	 * 
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String serialize() throws UnsupportedEncodingException {
		logger.info("serialize()");
		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 UnsupportedEncodingException {
		logger.info("load(String content)");
		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);
					logger.debug(product);
					CartItem item = new CartItem();
					item.setProduct(product);
					item.setNumber(number);
					item.setDrop(drop);
					items.put(product.getId(), item);
					logger.debug(items);
					logger.debug("load(String) end;");
				}
			}
		}

	}

	/**
	 * 
	 * @return
	 */
	public boolean isEmpty() {
		logger.info("isEmpty()");
		return items.isEmpty();
	}

	/**
	 * 
	 * @return
	 */
	public List<CartItem> getItems() {
		logger.info("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() {
		logger.info("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() {
		logger.info("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().getFixed_price()
						* item.getNumber();
			}
		}
		logger.debug(totalPrice);
		return (double)Math.round(totalPrice*100)/100;
	}

	public double getDangPrice() {
		logger.info("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().getDang_price()
						* item.getNumber();
			}
		}
		logger.debug(totalPrice);
		return (double)Math.round(totalPrice*100)/100;
	}

	
	public String toString(){
		return items.toString();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -