traderbean.java
来自「java的一系列产品中包括jsme,jmse,j2ee,本文件提供j2ee实现的」· Java 代码 · 共 124 行
JAVA
124 行
package stocktrader;
import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TraderBean implements SessionBean {
static final boolean VERBOSE = true;
private SessionContext ctx;
private Context environment;
private double tradingBalance;
/**
* Sets the session context.
*/
public void setSessionContext(SessionContext ctx) {
System.out.println("called setSessionContext()");
this.ctx = ctx;
}
/**
* This method is required by the EJB Specification,
* but is not used by this example.
*
*/
public void ejbActivate() {
System.out.println("called ejbActivate()");
}
/**
* This method is required by the EJB Specification,
* but is not used by this example.
*
*/
public void ejbPassivate() {
System.out.println("called ejbPassivate()");
}
/**
* This method is required by the EJB Specification,
* but is not used by this example.
*
*/
public void ejbRemove() {
System.out.println("called ejbRemove()");
}
/**
* This method corresponds to the create method in the home interface
* "TraderHome.java".
* The parameter sets of the two methods are identical.
*/
public void ejbCreate() throws CreateException {
System.out.println("called ejbCreate()");
try {
InitialContext ic = new InitialContext();
environment = (Context) ic.lookup("java:comp/env");
} catch (NamingException ne) {
throw new CreateException("Could not look up context");
}
this.tradingBalance = 0.0;
}
/**
* Buys shares of a stock for a named customer.
*/
public TraderResult buy(String customerName, String stockSymbol, int shares)
throws ProcessingErrorException
{
System.out.println("buy (" + customerName + ", " + stockSymbol + ", " + shares + ")");
double price = getStockPrice(stockSymbol);
tradingBalance -= (shares * price); // subtract purchases from cash account
return new TraderResult(shares, price, TraderResult.BUY);
}
/**
* Sells shares of a stock for a named customer.
*/
public TraderResult sell(String customerName, String stockSymbol, int shares)
throws ProcessingErrorException
{
System.out.println("sell (" + customerName + ", " + stockSymbol + ", " + shares + ")");
double price = getStockPrice(stockSymbol);
tradingBalance += (shares * price);
return new TraderResult(shares, price, TraderResult.SELL);
}
/**
* Returns the current balance of a trading session.
*/
public double getBalance() {
return tradingBalance;
}
/**
* Returns the stock price for a given stock.
*/
public double getStockPrice(String stockSymbol)
throws ProcessingErrorException
{
try {
return ((Double) environment.lookup(stockSymbol)).doubleValue();
} catch (NamingException ne) {
throw new ProcessingErrorException ("Stock symbol " + stockSymbol +
" does not exist");
} catch (NumberFormatException nfe) {
throw new ProcessingErrorException("Invalid price for stock "+stockSymbol);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?