cart.java

来自「有简单的网上书店需求及设计流程」· Java 代码 · 共 99 行

JAVA
99
字号
package org.wiely.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.wiely.vo.Book;
import org.wiely.vo.Orders;

/**
 * the model cart is invised for shoppingcart and help contact the book or
 * orders.
 */
public class Cart {

	protected Map<String, Orders> items;

	/**
	 * default constructor init the Map items
	 */
	public Cart() {

		if (items == null) {
			items = new HashMap<String, Orders>();
		}
	}

	/**
	 * 
	 * @param bookid
	 * @param orderitem
	 * @see add book by Orders and bookid. if bookid is exits ,must caculate the
	 *      quntity of bookacount again else put Orders
	 */
	public void addBook(String isbn, Orders orders) {

		if (items.containsKey(isbn)) {

			Orders _orders = items.get(isbn);
			_orders.setBookacount(_orders.getBookacount()
					+ orders.getBookacount());
			items.put(isbn, _orders);
		} else {

			items.put(isbn, orders);
		}
	}

	/**
	 * 
	 * @param isbn
	 * @see remove the book by isbn
	 */
	public void removeBook(String isbn) {
		items.remove(isbn);
	}

	/**
	 * 
	 * @param bookid
	 * @param quantity
	 * @see update the quantity of book
	 */
	public void updateCart(String isbn, int quantity) {

		Orders orders = items.get(isbn);
		orders.setBookacount(quantity);
		items.put(isbn, orders);
	}

	/**
	 * 
	 * @return totalprice by int
	 */
	@SuppressWarnings("unchecked")
	public int getTotalPrice() {

		int totalPrice = 0;
		for (Iterator it = items.values().iterator(); it.hasNext();) {

			Orders orders = (Orders) it.next();
			Book book = orders.getBook();
			int quantity = orders.getBookacount();
			totalPrice += book.getPrice() * quantity;
		}
		return totalPrice;
	}

	/**
	 * 
	 * @return map<Integer,Orders>
	 * @see getItems items. the items store the temple Orders.
	 */
	public Map<String, Orders> getItems() {
		return items;
	}

}

⌨️ 快捷键说明

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