cartservice.java

来自「达内网上购物系统」· Java 代码 · 共 242 行

JAVA
242
字号
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.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;
	}

}

⌨️ 快捷键说明

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