Bean是: package warehouse;
import javax.ejb.SessionBean; import javax.ejb.SessionContext; import javax.ejb.CreateException; import java.util.Hashtable;
public class WareHouseBean implements SessionBean { SessionContext sessionContext; Hashtable stocks;
public void ejbCreate() throws CreateException { stocks=new Hashtable(); stocks.put("货物1",new Double(10)); stocks.put("货物2",new Double(60)); stocks.put("货物3",new Double(30)); stocks.put("货物4",new Double(50)); stocks.put("货物5",new Double(20)); }
public void ejbRemove() { }
public void ejbActivate() { }
public void ejbPassivate() { }
public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; }
public Hashtable getStocks() { return stocks; }
public void addStock(String stockName,double stockQuantity) throws StockException{ if(stockName.trim().length()==0) throw new StockException("货物的名字为空."); if(stocks.get(stockName)==null){ stocks.put(stockName,new Double(stockQuantity)); //而这里的方法在客户端测试有效. } else{ double oriStockQuantity=((Double)stocks.get(stockName)).doubleValue(); double sum=oriStockQuantity+stockQuantity; // stocks.put(stockName,new Double(sum)); //这里的方法在客户端测试时无效 } }
public void minusStock(String stockName,double stockQuantity) throws StockException{ if(stockName.trim().length()==0) throw new StockException("货物的名字为空."); if(stocks.get(stockName)==null) throw new StockException("找不到该货物."); double oriStockQuantity=((Double)stocks.get(stockName)).doubleValue(); if(stockQuantity>oriStockQuantity){ throw new StockException("该货物的库存不够."); }else{ stocks.put(stockName,new Double(oriStockQuantity-stockQuantity)); }
}
public void removeStock(String stockName) throws StockException{ if(stockName.trim().length()==0) throw new StockException("货物的名字为空."); if(stocks.get(stockName)==null) throw new StockException("找不到该货物."); stocks.remove(stockName); }
public double getStock(String stockName) throws StockException{ if(stockName.trim().length()==0) throw new StockException("货物的名字为空."); if(stocks.get(stockName)==null) throw new StockException("找不到该货物."); double stockQuantity=((Double)stocks.get(stockName)).doubleValue(); return stockQuantity; }
}