AnalyzePortfolio.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:4k
源码类别:
Java编程
开发平台:
Java
- 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("nn**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 + "nn");
- return sb.toString();
- }
- }
- /*--- Formatted in Bible Style on Thu, Sep 6, '01 ---*/
- /*------ Formatted by Jindent 3.24 Gold 1.02 --- http://www.jindent.de ------*/