parent
7c48f55481
commit
e997c9d00d
@ -0,0 +1,34 @@
|
|||||||
|
package network;
|
||||||
|
|
||||||
|
import data.StypeMap;
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import data.DataSource;
|
||||||
|
import network.exceptions.*;
|
||||||
|
|
||||||
|
public class AlphaVantage extends DataSource {
|
||||||
|
public AlphaVantage() {
|
||||||
|
super("AlphaVantage", "https://www.alphavantage.co/query", "4MC2LL0HOQ2TFQL1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double[] update(String stype, String idstring) {
|
||||||
|
double[] result = {0.0, 0.0};
|
||||||
|
try {
|
||||||
|
String urlString = Net.urlStringBuilder(url,
|
||||||
|
"function", "GLOBAL_QUOTE",
|
||||||
|
"symbol", idstring,
|
||||||
|
"apikey", apiKey);
|
||||||
|
JsonObject response = StockJson.urlToJson(urlString);
|
||||||
|
JsonObject mainJson = StockJson.jsonInJson(response, "Global Quote");
|
||||||
|
result[0] = Double.parseDouble(StockJson.stringGetter(mainJson, "05. price"));
|
||||||
|
result[1] = StockJson.doublePercent(mainJson, "10. change percent");
|
||||||
|
} catch (ParaMismatchException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error getting data from: " + name);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
package network;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import network.exceptions.*;
|
||||||
|
|
||||||
|
//Ref: https://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests
|
||||||
|
public class Net {
|
||||||
|
public static String urlStringBuilder(String url, String... paras) throws ParaMismatchException {
|
||||||
|
String urlString = url;
|
||||||
|
String charset = "UTF-8";
|
||||||
|
if ((paras.length % 2) != 0) {
|
||||||
|
throw new ParaMismatchException("Except even paras, but have " + paras.length);
|
||||||
|
}
|
||||||
|
if (paras.length != 0) {
|
||||||
|
urlString += "?";
|
||||||
|
try {
|
||||||
|
for (int i = 0; i < paras.length - 3; i = i + 2) {
|
||||||
|
urlString += paras[i] + "=" + URLEncoder.encode(paras[i + 1], charset) + "&";
|
||||||
|
}
|
||||||
|
urlString += paras[paras.length - 2] + "=" + URLEncoder.encode(paras[paras.length - 1], charset);
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
System.out.println("Error during encoding url: Unsupported encoding");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return urlString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static InputStream urlToInputStream(String url) throws IOException {
|
||||||
|
try {
|
||||||
|
return (new URL(url).openStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
package network;
|
||||||
|
|
||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.json.JsonReader;
|
||||||
|
import javax.json.JsonNumber;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
//Json library from https://docs.oracle.com/javaee/7/api/javax/json/Json.html
|
||||||
|
public class StockJson {
|
||||||
|
public static JsonObject inputStreamToJson(InputStream istream) {
|
||||||
|
JsonReader jreader = Json.createReader(istream);
|
||||||
|
JsonObject jobj = jreader.readObject();
|
||||||
|
jreader.close();
|
||||||
|
return jobj;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonObject urlToJson(String url) throws IOException {
|
||||||
|
try {
|
||||||
|
return inputStreamToJson(Net.urlToInputStream(url));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double doubleGetter(JsonObject jobj, String name) {
|
||||||
|
return jobj.getJsonNumber(name).doubleValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonObject jsonInJson(JsonObject jobj, String name) {
|
||||||
|
return jobj.getJsonObject(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String stringGetter(JsonObject jobj, String name) {
|
||||||
|
return jobj.getString(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double doublePercent(JsonObject jobj, String name) {
|
||||||
|
String temp = stringGetter(jobj, name);
|
||||||
|
return Double.parseDouble(temp.split("%")[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,24 @@
|
|||||||
|
package observer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public abstract class Subject {
|
||||||
|
protected ArrayList<Wobserver> obsList = new ArrayList<Wobserver>();
|
||||||
|
|
||||||
|
public void addObs(Wobserver obs) {
|
||||||
|
obsList.add(obs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delObs(Wobserver obs) {
|
||||||
|
//Collections.singleton from https://howtodoinjava.com/java/collections/arraylist/arraylist-removeall/
|
||||||
|
obsList.removeAll(Collections.singleton(obs));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMsg() {
|
||||||
|
for (Wobserver obs : obsList) {
|
||||||
|
obs.update(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package observer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import data.WatchList;
|
||||||
|
|
||||||
|
public interface Wobserver {
|
||||||
|
public void update(Subject obsed);
|
||||||
|
}
|
||||||
Loading…
Reference in new issue