shoppingcartbean.java

来自「精通NetBeans光盘源代码,很好很好的资料」· Java 代码 · 共 190 行

JAVA
190
字号
package shop.cart;

import java.util.Enumeration;
import java.util.Vector;
import javax.ejb.*;

/**
 * This is the bean class for the ShoppingCartBean enterprise bean.
 * Created 2006-5-12 23:55:19
 * @author boyingking
 */
public class ShoppingCartBean implements SessionBean, ShoppingCartRemoteBusiness {
    private SessionContext context;
    private Vector cart;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }
    // </editor-fold>
    
    /**
     * See section 7.10.3 of the EJB 2.0 specification
     * See section 7.11.3 of the EJB 2.1 specification
     */
    public void ejbCreate() {
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
        cart=new Vector();
    }
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
    /**
     * 这个方法把一件商品加入到购物蓝中
     *
     * @param 商品的编号      String id
     * @param 商品的名称      String title
     * @param 商品的价格 	   double price
     * @param 商品的数量      int quantity
     *
     */
    public void addToCart(String id, String title, double price, int quantity)	{
        
        // 注意:需要处理添加已经存在的商品的情况
        /***********************************************************************************
         * 任务-->
         * 	当顾客选中商品后,这个方法添加CartItem对象,而CartItem 代表商品的情况。你可以查看CartItem.java的
         *   内容,这个文件已经提供给了大家。
         *
         *
         *   你需要完成以下功能:
         *
         *`	1. 先检查要添加的商品是不是已经在购物蓝中了,如果已经在购物蓝中,那么你只须把这件商品的数量加一。
         *
         *
         *	2. 如果这件商品不在购物蓝中,创建一个新的CartItem 类型的对象,并把它加入到购物蓝中。
         *
         **************************************************************************************/
        
        
        //boolean flag = true;
        Enumeration enumr = cart.elements();
        while(enumr.hasMoreElements()) {
            CartItem ci = (CartItem)enumr.nextElement();
            
            if(ci.getId().equals(id))	{
                
                ci.setQty(ci.getQty() + quantity);
                return;
            }
        }
        cart.add(new CartItem(id, title, price, quantity));
    }
    
    /**
     * 这个方法从购物蓝中删除一件商品
     *
     * @param  购物蓝中商品的名称    String id
     *
     *
     */
    public boolean removeFromCart(String id) {
        
        /***********************************************************************************
         * 任务-->
         * 	这个方法根据id的值,删除在购物蓝中的特定的商品
         *
         *
         * 	你需要完成以下功能:
         *
         *	1. 通过id的值,检查购物蓝中是否存在编号为id的商品,如果有就从购物蓝中删除掉。返回true.
         *
         *
         *	1. 通过id的值,检查购物蓝中是否存在编号为id的商品,如果没有就返回false。
         *
         *
         **************************************************************************************/
        
        
        
        for(int i=0;i<cart.size();i++) {
            
            CartItem ci = (CartItem)cart.elementAt( i );
            
            if(ci.getId().equals(id)){
                
                cart.remove(i);
                return true;
            }
        }
        return false;
    }
    
    /**
     * 返回所购商品的总价
     *
     * @return                  double Balance
     */
    
    
    public double getBalance() {
        
        /***********************************************************************************
         * 任务-->
         * 	这个方法计算所购买的所有商品的总价. 并返回总价钱。
         *
         * 	需要完成以下功能:
         *
         *`	1. 遍历cart中的CartItem对象,小计每种商品的价格
         *
         *	2.累计所有商品的小计,算出总价
         *
         *	3. 把总价返回
         *
         **************************************************************************************/
        Enumeration enumr = cart.elements();
        double cartbalance = 0.0;        
        while(enumr.hasMoreElements()){
            CartItem ci = (CartItem)enumr.nextElement();
            cartbalance = cartbalance + ci.getQty() * ci.getPrice();
        }        
        return cartbalance;
    }
    
    /**
     * 返回当前的购物蓝
     */
    
    public Vector getCart(){
        return cart;
    }
    /**
     * 清空当前的购物蓝
     */
    public void clearCart(){
        cart.clear();
    }
    
    
}

⌨️ 快捷键说明

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