Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.stock.util;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.HashMap;
- import java.util.logging.Logger;
- /**
- * Manages the companies.
- * @author Ryan Greene
- *
- */
- public class CompanyManager {
- /**
- * Available exchanges.
- */
- public static final String NASDAQ_EXCHANGE = "nasdaq";
- public static final String NYSE_EXCHANGE = "nyse";
- /**
- * The logger instance, for logging.
- */
- private static final Logger logger = Logger.getLogger(CompanyManager.class.getName());
- /**
- * Base URL constants.
- */
- private static final String BASE_URL_START = "http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=";
- private static final String BASE_URL_STOP = "&render=download";
- /**
- * Attribute constants.
- */
- private static final String ATTRIBUTE_SPLIT_REGEX = "\",\"";
- private static final int SYMBOL_ATTRIBUTE = 0;
- private static final int NAME_ATTRIBUTE = 1;
- private static final int LAST_SALE_ATTRIBUTE = 2;
- private static final int MARKET_CAP_ATTRIBUTE = 3;
- private static final int ADR_TSO_ATTRIBUTE = 4;
- private static final int IPO_YEAR_ATTRIBUTE = 5;
- private static final int SECTOR_ATTRIBUTE = 6;
- private static final int INDUSTRY_ATTRIBUTE = 7;
- private static final int SUMMARY_QUOTE_ATTRIBUTE = 8;
- /**
- * Contains all the companies being managed with the key being their symbol or ticker.
- */
- private static HashMap<String, Company> companies = null;
- /**
- * Gets the company with the specified symbol.
- * @param symbol The symbol.
- * @return The company.
- */
- public static Company getCompany(String symbol) {
- return companies.get(symbol);
- }
- /**
- * Initiates the company manager with the specified exchange.
- * @param exchange The exchange.
- * @throws IOException
- * @throws IllegalStateException if the companies have already been loaded.
- */
- public static void init(final String exchange) throws IOException {
- if (companies != null) {
- throw new IllegalStateException("Companies already loaded.");
- }
- logger.info("Initiating company manager for the exchange: " + exchange);
- companies = new HashMap<String, Company>();
- URL url = new URL(BASE_URL_START + exchange + BASE_URL_STOP);
- URLConnection connection = url.openConnection();
- BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- try {
- String line = reader.readLine();
- while ((line = reader.readLine()) != null) {
- String[] attributes = line.split(ATTRIBUTE_SPLIT_REGEX);
- String symbol = attributes[SYMBOL_ATTRIBUTE];
- String name = attributes[NAME_ATTRIBUTE];
- double lastSale = attributes[LAST_SALE_ATTRIBUTE].equals("n/a") ? -1 : Double.parseDouble(attributes[LAST_SALE_ATTRIBUTE]);
- double marketCap = attributes[MARKET_CAP_ATTRIBUTE].equals("n/a") ? -1 : Double.parseDouble(attributes[MARKET_CAP_ATTRIBUTE]);
- String adrTso = attributes[ADR_TSO_ATTRIBUTE];
- int ipoYear = attributes[IPO_YEAR_ATTRIBUTE].equals("n/a") ? -1 : Integer.parseInt(attributes[IPO_YEAR_ATTRIBUTE]);
- String sector = attributes[SECTOR_ATTRIBUTE];
- String industry = attributes[INDUSTRY_ATTRIBUTE];
- String summaryQuote = attributes[SUMMARY_QUOTE_ATTRIBUTE];
- Company company = Company.createCompany(symbol, name, lastSale, marketCap, adrTso, ipoYear, sector, industry, summaryQuote);
- companies.put(symbol, company);
- }
- } finally {
- if (reader != null) {
- reader.close();
- }
- }
- logger.info("Loaded " + companies.size() + " companies.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement