?? carimpl.java
字號:
package org.itfuture.www.service.impl;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.itfuture.www.po.Item;
import org.itfuture.www.service.CategoryService;
import org.itfuture.www.vo.Car;
public class CarImpl {
//生成一個容器(map),用來存放所購買的所有商品,商品編號為鍵(key),商品實例為值(value)
public static Map map = Collections.synchronizedMap(new TreeMap<String,Object>());
//聲明一個對象作屬性用來在本類(class)中調用CategoryServiceImpl(categoryService的子類)類中的方法
private CategoryService dao;
public CarImpl() {
//生成子類的實例(發生上型,子類充當超類來用s)
dao = new CategoryServiceImpl();
}
//根據商品編號判斷購物車中是否有該物品
public boolean containsKey(String itemid){
return map.containsKey(itemid);
}
//如果購物車中已經存在該物品,則在原有物品上增加數量,如果購物車中沒有該物品
public void addItem(String itemid,Integer quantity){
//如果購物車中已經存在該物品,則在原有物品上增加數量,
if(this.containsKey(itemid)){
Car car = (Car)map.get(itemid);
car.addQuantity(quantity);
//如果購物車中沒有該物品,則生成一個該物品的實例,并放入容器(Map),等待存入session
}else{
Item item = dao.getOneItem(itemid);
Car car = new Car(item,quantity);
map.put(itemid, car);
}
}
//根據用戶購買物品的編號刪除購物車中的一個物品
public void delete(String itemid){
map.remove(itemid);
}
public Double getOneTotalPrice(String itemid){
Car car = (Car)map.get(itemid);
return car.getQuantity()*car.getItem().getListprice();
}
//得到購買所有商品的總錢數
public Double totalPrice(){
// System.out.println(map);
Iterator <Car> iter =map.values().iterator();
Double totalPrice= (double)0 ;
while(iter.hasNext()){
Car car = iter.next();
totalPrice+=car.getItem().getListprice()*car.getQuantity();
}
return totalPrice;
}
//修改購買物品的數據
public void update(String itemid,Integer quantity){
Car car = (Car)map.get(itemid);
car.setQuantity(quantity);
}
public CategoryService getDao() {
return dao;
}
public void setDao(CategoryService dao) {
this.dao = dao;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -