Skip to content

Commit 1898d87

Browse files
committed
Working version
1 parent ad61d83 commit 1898d87

File tree

6 files changed

+132
-2
lines changed

6 files changed

+132
-2
lines changed

.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
56
<classpathentry kind="output" path="bin"/>
67
</classpath>

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*.jar
55
*.war
66
*.ear
7+
/bin

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Nathan Hefner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

src/com/nhefner/main/Stock.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
package com.nhefner.main;
22

3-
public class Stock {
3+
public class Stock {
4+
5+
String symbol;
6+
double price;
7+
8+
public Stock() {
9+
10+
}
11+
12+
public Stock(String symbol) {
13+
14+
this.symbol = symbol;
15+
16+
}
17+
18+
public Stock(String symbol, Double price) {
19+
20+
this.symbol = symbol;
21+
this.price = price;
22+
23+
}
24+
25+
public String getSymbol() {
26+
27+
return this.symbol;
28+
29+
}
30+
31+
public Double getPrice() {
32+
33+
return this.price;
34+
35+
}
36+
37+
public void setPrice(double price) {
38+
39+
this.price = price;
40+
41+
}
42+
443
}

src/com/nhefner/main/StockHelper.java

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,53 @@
11
package com.nhefner.main;
22

3-
public class StockHelper {
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.regex.Pattern;
9+
10+
public class StockHelper {
11+
12+
static String symbol = "";
13+
static double price = 0.0;
14+
15+
/*
16+
*
17+
*
18+
*/
19+
public Stock getStock(String symbol) {
20+
21+
Stock stock = new Stock();
22+
23+
try {
24+
25+
// Retrieve CSV File
26+
URL yahoo = new URL("http://finance.yahoo.com/d/quotes.csv?s="+ symbol + "&f=l1");
27+
URLConnection connection = yahoo.openConnection();
28+
InputStreamReader is = new InputStreamReader(connection.getInputStream());
29+
BufferedReader br = new BufferedReader(is);
30+
31+
// Parse CSV Into Array
32+
String line = br.readLine();
33+
String[] stockinfo = line.split(",");
34+
35+
// Check Our Data
36+
if (Pattern.matches("N/A", stockinfo[0])) {
37+
price = 0.00;
38+
} else {
39+
price = Double.parseDouble(stockinfo[0]);
40+
}
41+
42+
// Build Stock Object
43+
stock = new Stock(symbol, price);
44+
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}
48+
49+
return stock;
50+
51+
}
52+
453
}

src/com/nhefner/tests/StockTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.nhefner.tests;
2+
3+
import org.junit.Test;
4+
5+
import com.nhefner.main.Stock;
6+
import com.nhefner.main.StockHelper;
7+
8+
public class StockTest {
9+
10+
@Test
11+
public void testStock() {
12+
13+
StockHelper sh = new StockHelper();
14+
Stock facebook = sh.getStock("FB");
15+
System.out.println("Price: " + facebook.getPrice());
16+
17+
}
18+
19+
}

0 commit comments

Comments
 (0)