analyzeportfolio.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 196 行

JAVA
196
字号
package bible.ejb.session.example2;import java.rmi.*;import javax.ejb.*;import java.util.*;import javax.naming.*;/** * Class AnalyzePortfolio * * * @author * @version %I%, %G% */public class AnalyzePortfolio implements SessionBean {  private SessionContext sessionContext;  /**   * Method ejbCreate   *   *   */  public void ejbCreate() {}  /**   * Method ejbRemove   *   *   * @throws RemoteException   *   */  public void ejbRemove() throws RemoteException {}  /**   * Method ejbActivate   *   *   * @throws RemoteException   *   */  public void ejbActivate() throws RemoteException {}  /**   * Method ejbPassivate   *   *   * @throws RemoteException   *   */  public void ejbPassivate() throws RemoteException {}  /**   * Method setSessionContext   *   *   * @param sessionContext   *   * @throws RemoteException   *   */  public void setSessionContext(SessionContext sessionContext)    throws RemoteException {    this.sessionContext = sessionContext;  }  // Internal state variables  private HashMap holdings = new HashMap();  private HashMap debts    = new HashMap();  /**   *  Adds a holding.   */  public void addHolding(String name, Integer amount) throws RemoteException {    holdings.put(name, amount);  }  /**   * Adds a debt.   */  public void addDebt(String name, Integer amount) throws RemoteException {    debts.put(name, amount);  }  /**   * Returns a detailed analysis of the current state.   */  public String getAnalysis() throws RemoteException {    StringBuffer sb = new StringBuffer();    sb.append("\n\n**Current Portfolio Analysis**\n");    sb.append(analyzeMap("Your Holdings", holdings));    sb.append(analyzeMap("Your Debts", debts));    sb.append("  Total Cash Value: " + this.getTotalCash() + "\n");    sb.append("Currently qualifies for Zeetrade Gold plan: "              + this.qualifiesForGoldAccount());    return sb.toString();  }  /**   * Returns true if they have the financial strength to qualify for   * a gold account.   */  public boolean qualifiesForGoldAccount() throws RemoteException {    if (this.getGoldAccountMinimum() > this.getTotalCash()) {      return false;    } else {      return true;    }  }  /**   * Checks the deployment descriptor for the minimum amount of   * total cash is needed to qualify.   */  private int getGoldAccountMinimum() {    InitialContext ctx     = null;    Integer        minimum = new Integer(10000);  // 10000 for default.    try {      ctx     = new InitialContext();      minimum = (Integer) ctx.lookup("java:comp/env/goldAccountMinimum");    } catch (Exception e) {      e.printStackTrace();    } finally {      try {        ctx.close();      } catch (Exception e) {}    }    return minimum.intValue();  }  private int getTotalCash() {    return getTotalHoldings() - getTotalDebt();  }  private int getTotalHoldings() {    return calculateTotal(holdings);  }  private int getTotalDebt() {    return calculateTotal(debts);  }  private int calculateTotal(HashMap map) {    Iterator iter  = map.values().iterator();    int      total = 0;    Integer  tmp   = null;    while (iter.hasNext()) {      tmp   = (Integer) iter.next();      total += tmp.intValue();    }    return total;  }  private String analyzeMap(String name, HashMap map) {    StringBuffer sb = new StringBuffer();    sb.append("  **" + name + "**\n");    Iterator iter   = map.keySet().iterator();    String   key    = null;    Integer  tmpInt = null;    int      total  = 0;    while (iter.hasNext()) {      key    = (String) iter.next();      tmpInt = (Integer) map.get(key);      sb.append("    " + key + ": " + tmpInt.intValue() + "\n");      total += tmpInt.intValue();    }    sb.append("  Total: " + total + "\n\n");    return sb.toString();  }}/*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*//*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/

⌨️ 快捷键说明

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