Advertisement
Chiddix

company manager

Apr 21st, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package org.stock.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.util.HashMap;
  9. import java.util.logging.Logger;
  10.  
  11. /**
  12.  * Manages the companies.
  13.  * @author Ryan Greene
  14.  *
  15.  */
  16. public class CompanyManager {
  17.  
  18.     /**
  19.      * Available exchanges.
  20.      */
  21.     public static final String NASDAQ_EXCHANGE = "nasdaq";
  22.     public static final String NYSE_EXCHANGE = "nyse";
  23.    
  24.     /**
  25.      * The logger instance, for logging.
  26.      */
  27.     private static final Logger logger = Logger.getLogger(CompanyManager.class.getName());
  28.    
  29.     /**
  30.      * Base URL constants.
  31.      */
  32.     private static final String BASE_URL_START = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=";
  33.     private static final String BASE_URL_STOP = "&render=download";
  34.    
  35.     /**
  36.      * Attribute constants.
  37.      */
  38.     private static final String ATTRIBUTE_SPLIT_REGEX = "\",\"";
  39.    
  40.     private static final int SYMBOL_ATTRIBUTE = 0;
  41.     private static final int NAME_ATTRIBUTE = 1;
  42.     private static final int LAST_SALE_ATTRIBUTE = 2;
  43.     private static final int MARKET_CAP_ATTRIBUTE = 3;
  44.     private static final int ADR_TSO_ATTRIBUTE = 4;
  45.     private static final int IPO_YEAR_ATTRIBUTE = 5;
  46.     private static final int SECTOR_ATTRIBUTE = 6;
  47.     private static final int INDUSTRY_ATTRIBUTE = 7;
  48.     private static final int SUMMARY_QUOTE_ATTRIBUTE = 8;
  49.    
  50.     /**
  51.      * Contains all the companies being managed with the key being their symbol or ticker.
  52.      */
  53.     private static HashMap<String, Company> companies = null;
  54.    
  55.     /**
  56.      * Gets the company with the specified symbol.
  57.      * @param symbol The symbol.
  58.      * @return The company.
  59.      */
  60.     public static Company getCompany(String symbol) {
  61.         return companies.get(symbol);
  62.     }
  63.    
  64.     /**
  65.      * Initiates the company manager with the specified exchange.
  66.      * @param exchange The exchange.
  67.      * @throws IOException
  68.      * @throws IllegalStateException if the companies have already been loaded.
  69.      */
  70.     public static void init(final String exchange) throws IOException {
  71.         if (companies != null) {
  72.             throw new IllegalStateException("Companies already loaded.");
  73.         }
  74.         logger.info("Initiating company manager for the exchange: " + exchange);
  75.         companies = new HashMap<String, Company>();
  76.         URL url = new URL(BASE_URL_START + exchange + BASE_URL_STOP);
  77.         URLConnection connection = url.openConnection();
  78.         BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  79.         try {
  80.             String line = reader.readLine();
  81.             while ((line = reader.readLine()) != null) {
  82.                 String[] attributes = line.split(ATTRIBUTE_SPLIT_REGEX);
  83.                
  84.                 String symbol = attributes[SYMBOL_ATTRIBUTE];
  85.                 String name = attributes[NAME_ATTRIBUTE];
  86.                 double lastSale = attributes[LAST_SALE_ATTRIBUTE].equals("n/a") ? -1 : Double.parseDouble(attributes[LAST_SALE_ATTRIBUTE]);
  87.                 double marketCap = attributes[MARKET_CAP_ATTRIBUTE].equals("n/a") ? -1 : Double.parseDouble(attributes[MARKET_CAP_ATTRIBUTE]);
  88.                 String adrTso = attributes[ADR_TSO_ATTRIBUTE];
  89.                 int ipoYear = attributes[IPO_YEAR_ATTRIBUTE].equals("n/a") ? -1 : Integer.parseInt(attributes[IPO_YEAR_ATTRIBUTE]);
  90.                 String sector = attributes[SECTOR_ATTRIBUTE];
  91.                 String industry = attributes[INDUSTRY_ATTRIBUTE];
  92.                 String summaryQuote = attributes[SUMMARY_QUOTE_ATTRIBUTE];
  93.                
  94.                 Company company = Company.createCompany(symbol, name, lastSale, marketCap, adrTso, ipoYear, sector, industry, summaryQuote);
  95.                 companies.put(symbol, company);
  96.             }
  97.         } finally {
  98.             if (reader != null) {
  99.                 reader.close();
  100.             }
  101.         }
  102.         logger.info("Loaded " + companies.size() + " companies.");
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement