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

📄 carimpl.java

📁 采用最新Struts2+Hibernate架构开发
💻 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 + -