Source Code Cross Referenced for GetQuote.java in  » IDE-Netbeans » identity.kit » org » netbeans » identity » samples » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » identity.kit » org.netbeans.identity.samples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * To change this template, choose Tools | Templates
003:         * and open the template in the editor.
004:         */
005:        package org.netbeans.identity.samples;
006:
007:        import java.io.BufferedReader;
008:        import java.io.IOException;
009:        import java.io.InputStreamReader;
010:        import java.net.MalformedURLException;
011:        import java.net.URL;
012:        import java.net.URLConnection;
013:        import java.util.Calendar;
014:        import java.util.GregorianCalendar;
015:        import java.util.HashMap;
016:        import java.util.Map;
017:
018:        /**
019:         *
020:         * @author Peter Liu
021:         */
022:        public class GetQuote {
023:
024:            public GetQuote() {
025:                init();
026:            }
027:
028:            /**
029:             * Returns the stock quote for the stock symbol.
030:             * If unable to obtain the real time quote, returns a cached value
031:             */
032:            public Map getQuote(String symbol) {
033:                // Obtain the quote from Yahoo! Service
034:                Map data = getYahooQuote(symbol);
035:                if (data == null) {
036:                    // Unable to obtain from Yahoo! Get from local cache
037:                    data = getCachedQuote(symbol);
038:                }
039:
040:                // Return the results
041:                return (data);
042:            }
043:
044:            private Map getYahooQuote(String ticker) {
045:                URL url = null;
046:                try {
047:                    // URL for the stock quote from Yahoo! service
048:                    url = new URL(
049:                            "http://download.finance.yahoo.com/d/quotes.csv?s="
050:                                    + ticker + "&d=t&f=sl1d1t1c1ohgvj1pp2wern");
051:
052:                    // Set the timeouts for connection and read
053:                    URLConnection connection = url.openConnection();
054:                    connection.setConnectTimeout(1000);
055:                    connection.setReadTimeout(1000);
056:
057:                    // Request for the stock quote
058:                    BufferedReader in = new BufferedReader(
059:                            new InputStreamReader(url.openStream()));
060:                    String[] values = null;
061:                    String str;
062:                    if ((str = in.readLine()) != null) {
063:                        values = str.split(",");
064:                    }
065:                    in.close();
066:                    if ((values == null) || values.length < 16) {
067:                        return (null);
068:                    }
069:                    // Populate stock values
070:                    Map map = new HashMap();
071:                    map.put("symbol", removeQuotes(values[0]));
072:                    map.put("company", removeQuotes(values[15]));
073:                    map.put("realValue", values[1]);
074:                    map.put("time", removeQuotes(values[2]) + " "
075:                            + removeQuotes(values[3]));
076:                    map.put("volume", values[8]);
077:                    map.put("open", values[5]);
078:                    map.put("change", values[4]);
079:                    map.put("dayHigh", values[6]);
080:                    map.put("dayLow", values[7]);
081:                    map.put("yearRange", removeQuotes(values[12]));
082:                    map.put("marketCap", values[9]);
083:                    map.put("message", "");
084:                    return map;
085:                } catch (MalformedURLException ex) {
086:                    ex.printStackTrace();
087:                } catch (IOException ex) {
088:                    ex.printStackTrace();
089:                }
090:                return null;
091:            }
092:
093:            private Map getCachedQuote(String symbol) {
094:                Map data = (Map) stockData.get(symbol);
095:                if (data == null) {
096:                    data = (Map) stockData.get("ORCL");
097:                }
098:                data.put("symbol", symbol);
099:                data.put("company", symbol.toUpperCase());
100:                return (data);
101:            }
102:
103:            /**
104:             * Assign static values for stock quotes.
105:             * Used as fall-back if quote cannot be
106:             * obtained from Yahoo!
107:             */
108:            private void init() {
109:                Map stockValues = new HashMap();
110:                stockValues.put("company", "");
111:                stockValues.put("realValue", "7.36");
112:                stockValues.put("volume", "31,793,369");
113:                stockValues.put("open", "7.37");
114:                stockValues.put("change", "-0.01");
115:                stockValues.put("dayHigh", "7.38");
116:                stockValues.put("dayLow", "7.12");
117:                stockValues.put("yearRange", "N/A");
118:                stockValues.put("marketCap", "N/A");
119:                stockValues.put("message", "Quote AUTO Generated");
120:                stockValues.put("time", getTime());
121:                stockData.put("SUNW", stockValues);
122:
123:                stockValues = new HashMap();
124:                stockValues.put("realValue", "16.35");
125:                stockValues.put("company", "");
126:                stockValues.put("volume", "38,544,715");
127:                stockValues.put("open", "16.35");
128:                stockValues.put("change", "-0.27");
129:                stockValues.put("dayHigh", "16.64");
130:                stockValues.put("dayLow", "16.31");
131:                stockValues.put("yearRange", "N/A");
132:                stockValues.put("marketCap", "N/A");
133:                stockValues.put("message", "Quote AUTO Generated");
134:                stockValues.put("time", getTime());
135:                stockData.put("ORCL", stockValues);
136:            }
137:
138:            private String getTime() {
139:                GregorianCalendar time = new GregorianCalendar();
140:                return (time.get(Calendar.MONTH) + "/"
141:                        + time.get(Calendar.DAY_OF_MONTH) + "/"
142:                        + time.get(Calendar.YEAR) + " "
143:                        + time.get(Calendar.HOUR) + ":"
144:                        + time.get(Calendar.MINUTE) + time.get(Calendar.AM_PM));
145:            }
146:
147:            private String removeQuotes(String key) {
148:                return (key.replaceAll("\"", ""));
149:            }
150:
151:            private static Map stockData = new HashMap();
152:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.