📄 shoppingcartbean.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package ejb3.day2;import java.util.LinkedList;import java.util.List;import javax.annotation.Resource;import javax.ejb.Init;import javax.ejb.Remote;import javax.ejb.Remove;import javax.ejb.SessionContext;import javax.ejb.Stateful;import javax.interceptor.Interceptors;/** * * @author user */@Stateful(mappedName="ejb/cart")@Remote(ShoppingCart.class)@Interceptors(ShoppingCartInterceptor.class)public class ShoppingCartBean implements ShoppingCart{ private List<SalesLine> sales = new LinkedList<SalesLine>(); public ShoppingCartBean(){ System.out.println("Constructor: ShoppingCartBean" ); } @Resource public void setSessionContext(SessionContext ctx){ System.out.println("ShoppingCart: Dependency injection"); } public void add(SalesLine line) { for(SalesLine l : sales){ if(l.getProduct().getId() == line.getProduct().getId()){ l.setNum(l.getNum() + line.getNum()); return; } } sales.add(line); } public void remove(SalesLine line) { for(SalesLine l : sales){ if(l.getProduct().getId() == line.getProduct().getId()){ if(l.getNum() > line.getNum()){ l.setNum(l.getNum() - line.getNum()); }else{ sales.remove(l); } return; } } } public List<SalesLine> list() { return sales; } public float cost() { float c = 0.0f; for(SalesLine l : sales){ c += l.getNum() * l.getProduct().getPrice(); } return c; } @Remove public float placeOrder() { return cost(); } //@Init public void init() { System.out.println("init"); sales = new LinkedList<SalesLine>(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -