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

📄 cart.java

📁 基于J2EE技术的 电子购物商城系统
💻 JAVA
字号:

package domain;

import java.util.ArrayList;

public class Cart {

	public static void main(String[] args) {
		Cart cart = new Cart();
		String id = new String("hi");//error:String id = "hi";
		Goods goods = new Goods();
		goods.setId(id);
		cart.addGood(goods, 5);
		System.out.println(cart.size());
		System.out.println(cart.getGoodCounts().get(0));
		
		String id2 = new String("hi");//error:String id2 = "hi";
		Goods goods2 = new Goods();
		goods2.setId(id2);
		cart.addGood(goods2, 4);
		System.out.println(cart.size());
		System.out.println(cart.getGoodCounts().get(0));
	}
	
	public Cart() {
		//do nothing;
	}
	
	public Cart(User user) {
		this.user = user;
	}
	
	public Cart(User user, ArrayList<Goods> goodses) {
		this.user = user;
		this.goodses = goodses;
		for (int i = 0; i < this.goodses.size(); i++) {
			goodCounts.add(new Integer(1));
		}
	}
	
	public Cart(User user, ArrayList<Goods> goodses, ArrayList<Integer> goodCounts) {
		this.user = user;
		this.goodses = goodses;
		if (goodses.size() == goodCounts.size()) {
			this.goodCounts = goodCounts;
		}
		else {
			for (int i = 0; i < this.goodses.size(); i++) {
				goodCounts.add(new Integer(1));
			}
		}
	}
	
	public int size() {
		return goodses.size();
	}
	
	public boolean contains(Goods goods) {
		return goodses.contains(goods);
	}
	
	public boolean contains(Goods goods, int count) {
		int index = goodses.indexOf(goods);
		return (index > -1) && (goodCounts.get(index).intValue() == count);
	}
	
	public double costs() {
		double sum = 0;
		for (int i = 0; i < goodses.size(); i++) {
			sum += cost(i);
		}
		return sum;
	}
	
	public double cost(int index) {
		double sum = 0;
		Goods goods = goodses.get(index);
		int count = goodCounts.get(index).intValue();
		sum = goods.getPrice() * count;
		return sum;
	}
	
	public ArrayList<User> getSalers() {
		ArrayList<User> list = new ArrayList<User>();
		for (int i = 0; i < goodses.size(); i++) {
			list.add(getSaler(i));
		}
		return list;
	}
	
	public User getSaler(int index) {
		return goodses.get(index).getSaler();
	}

	public int indexOf(Goods goods) {
		return goodses.indexOf(goods);
	}
	
	public void setGoods(Goods goods, int count, int index) {
		if (count >0 && index >= 0 && goods != null) {
			goodses.set(index, goods);
			goodCounts.set(index, new Integer(count));
		}
	}
	
	public Goods getGoods(int index) {
		return goodses.get(index);
	}

	public void setGoodsCount(int count, int index) {
		setGoods(goodses.get(index), count, index);
	}
	
	public int getGoodsCount(int index) {
		return goodCounts.get(index).intValue();
	}
		
	public void setGoods(Goods goods, int index) {
		setGoods(goods, getGoodsCount(index), index);
	}
	
	public void setGoods(int count, int index) {
		setGoods(getGoods(index), count, index);
	}
	
	public void addGood(Goods goods, int count) {
		if (count > 0) {
			if (goodses.contains(goods)) {
				int index = goodses.indexOf(goods);
				Integer oldCount = goodCounts.get(index);
				goodCounts.remove(index);
				goodCounts.add(index, new Integer(oldCount.intValue() + count));
			}
			else {
				goodses.add(goods);
				goodCounts.add(new Integer(count));
			}	
		}
	}
	
	/**
	 * @remove the specific good from the user's cart.
	 * @return boolean if operate successfully return true; else return false.
	 */		
	public boolean removeGood(Goods goods) {
		if (goodses.contains(goods)) {
			goodCounts.remove(goodses.indexOf(goods));
			return goodses.remove(goods);
		}
		return false;
	}
	
	/**
	 * @Clear the user's cart.
	 */	
	public void clear() {
		goodses.clear();
		goodCounts.clear();
	}
	
	/**
	 * @return Returns the Goods.
	 */
	public ArrayList<Goods> getGoods() {
		return goodses;
	}
	/**
	 * @param bookIds The Goods to set.
	 */
	public void setGoods(ArrayList<Goods> goodses) {
		this.goodses = goodses;
	}
	/**
	 * @return Returns the user.
	 */
	public User getUser() {
		return user;
	}
	/**
	 * @param userId The user to set.
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return Returns the goodCounts.
	 */
	public ArrayList<Integer> getGoodCounts() {
		return goodCounts;
	}
	/**
	 * @param goodCounts The goodCounts to set.
	 */
	public void setGoodCounts(ArrayList<Integer> goodCounts) {
		this.goodCounts = goodCounts;
	}
	
	User user = null;						/** the cart's user */
	ArrayList<Goods> goodses = new ArrayList<Goods>();		/** the cart's goods */
	ArrayList<Integer> goodCounts = new ArrayList<Integer>();	/** the goods' count */ 
}

⌨️ 快捷键说明

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