Compare commits
31 Commits
deliverabl
...
deliverabl
| Author | SHA1 | Date |
|---|---|---|
|
|
ca8d7e876a | 6 years ago |
|
|
8f6cd85a2e | 6 years ago |
|
|
7adccd3cc5 | 6 years ago |
|
|
4e2180dd6f | 6 years ago |
|
|
88e46f3b41 | 6 years ago |
|
|
6b4cd4ebc9 | 6 years ago |
|
|
69bf511fd2 | 6 years ago |
|
|
85c1de8f3a | 6 years ago |
|
|
7ab1b38684 | 6 years ago |
|
|
f476d1e3cb | 6 years ago |
|
|
5e92eae32f | 6 years ago |
|
|
bc7542de7a | 6 years ago |
|
|
3629449c0f | 6 years ago |
|
|
58c5cbe870 | 6 years ago |
|
|
ba13fcad28 | 6 years ago |
|
|
b6d67611ca | 6 years ago |
|
|
e33a1e9ab6 | 6 years ago |
|
|
71fe283b9f | 6 years ago |
|
|
e997c9d00d | 6 years ago |
|
|
7c48f55481 | 6 years ago |
|
|
401b60fe5b | 6 years ago |
|
|
ccbdd480dc | 6 years ago |
|
|
202ee6e220 | 6 years ago |
|
|
30ea1ec59c | 6 years ago |
|
|
08d7475257 | 6 years ago |
|
|
5e722ceba6 | 6 years ago |
|
|
1b378485f2 | 6 years ago |
|
|
c8df751f47 | 6 years ago |
|
|
1810409047 | 6 years ago |
|
|
e6eef8899f | 6 years ago |
|
|
bc604a3a53 | 6 years ago |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,71 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class DataSource {
|
||||||
|
protected HashMap<String, StockType> targets;
|
||||||
|
protected String name;
|
||||||
|
protected String url;
|
||||||
|
protected String apiKey;
|
||||||
|
|
||||||
|
public DataSource(String name, String url, String apiKey) {
|
||||||
|
this.targets = new HashMap<String, StockType>();
|
||||||
|
this.name = name;
|
||||||
|
this.url = url;
|
||||||
|
this.apiKey = apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: add target to this
|
||||||
|
// add this to target if not already done
|
||||||
|
//Modifies: this, stype
|
||||||
|
public void addStype(StockType stype) {
|
||||||
|
String sname = stype.getName();
|
||||||
|
if (!this.targets.containsKey(sname)) {
|
||||||
|
targets.put(sname,stype);
|
||||||
|
stype.addSource(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: del target to this
|
||||||
|
// del this to target if not already done
|
||||||
|
//Modifies: this, stype
|
||||||
|
public void delStype(StockType stype) {
|
||||||
|
String sname = stype.getName();
|
||||||
|
if (this.targets.containsKey(sname)) {
|
||||||
|
targets.remove(sname,stype);
|
||||||
|
stype.delSource(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj == this) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(obj instanceof DataSource)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
DataSource temp = (DataSource) obj;
|
||||||
|
if (temp.getName().equals(name)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: returns updated data in the form: double[2], [0] is price, [1] is %change
|
||||||
|
public abstract double[] update(String stype, String idstring) throws IOException;
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
//Singleton pattern from https://www.tutorialspoint.com/java/java_using_singleton.htm
|
||||||
|
public class ListOfWatchList {
|
||||||
|
private static ListOfWatchList lowl = new ListOfWatchList();
|
||||||
|
private ArrayList<WatchList> wlists;
|
||||||
|
|
||||||
|
private ListOfWatchList() {
|
||||||
|
wlists = new ArrayList<WatchList>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ListOfWatchList getList() {
|
||||||
|
return lowl;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: add a watchlist to this list, if it isn't already in the list
|
||||||
|
//Modifies: this
|
||||||
|
public void addWatchList(WatchList wlist) {
|
||||||
|
if (!(wlists.contains(wlist))) {
|
||||||
|
wlists.add(wlist);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: remove a watchlist
|
||||||
|
//Modifies: this
|
||||||
|
public void delWatchList(WatchList wlist) {
|
||||||
|
wlists.remove(wlist);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: return a watchlist by index
|
||||||
|
public WatchList getWatchList(int index) {
|
||||||
|
return (WatchList) wlists.get(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,11 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import data.StockType;
|
||||||
|
|
||||||
|
public class Nasdaq extends StockType {
|
||||||
|
public Nasdaq() {
|
||||||
|
super();
|
||||||
|
name = "NASDAQ";
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,28 +1,13 @@
|
|||||||
package data;
|
package data;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import data.StockType;
|
||||||
|
import network.AlphaVantage;
|
||||||
|
|
||||||
public class Nyse implements StockType {
|
public class Nyse extends StockType {
|
||||||
private static final String NAME = "NYSE";
|
|
||||||
private HashSet sources;
|
|
||||||
|
|
||||||
public Nyse() {
|
public Nyse() {
|
||||||
sources = new HashSet();
|
super();
|
||||||
}
|
name = "NYSE";
|
||||||
|
addSource(new AlphaVantage());
|
||||||
@Override
|
|
||||||
public float[] update(String idstring) {
|
|
||||||
Iterator iterator = sources.iterator();
|
|
||||||
float[] result = new float[2];
|
|
||||||
while (iterator.hasNext()) {
|
|
||||||
//XXX
|
|
||||||
//DataSource source = (DataSource)iterator.next();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return this.NAME;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,75 +0,0 @@
|
|||||||
package data;
|
|
||||||
|
|
||||||
import java.util.prefs.Preferences;
|
|
||||||
import data.Const;
|
|
||||||
|
|
||||||
public class Options {
|
|
||||||
private Preferences store;
|
|
||||||
private boolean wasEmpty;
|
|
||||||
private static final String DEF_OPTS_PATH = "";
|
|
||||||
|
|
||||||
public Options() {
|
|
||||||
//store = Preferences.userRoot();
|
|
||||||
//wasEmpty = store.nodeExists(Const.PROGRAM_NAME);
|
|
||||||
//store = store.node(Const.PROGRAM_NAME);
|
|
||||||
//if (wasEmpty) {
|
|
||||||
// //load default opts from resource
|
|
||||||
// importDefPreferences();
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Options(String pathname) {
|
|
||||||
store = Preferences.userRoot().node(pathname);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Options getSection(String section) {
|
|
||||||
//String path = Const.PROGRAM_NAME + "/" + section;
|
|
||||||
//Options result = new Options(path);
|
|
||||||
//return result;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void imoprtPreferences(String xmlpath) {
|
|
||||||
//XXX
|
|
||||||
System.out.println("Loaded preference from: "
|
|
||||||
+ xmlpath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void imoprtDefPreferences(String xmlpath) {
|
|
||||||
//XXX
|
|
||||||
System.out.println("Loaded default preference: ");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void exportPreferences(String xmlpath) {
|
|
||||||
//XXX
|
|
||||||
System.out.println("Exported preference from: "
|
|
||||||
+ xmlpath);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void destroy() {
|
|
||||||
//XXX
|
|
||||||
// Clear config if requested or config was empty
|
|
||||||
// and is not saving
|
|
||||||
//if ((getBool(EMPTY) && getBool(NO_SAVE))
|
|
||||||
// || getBool(CLEAR)) {
|
|
||||||
// this.store.clear();
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default all false bool pref to be safe
|
|
||||||
// Default should be imported by constructor regardless
|
|
||||||
public boolean getBool(String key) {
|
|
||||||
return this.store.getBoolean(key, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default all empty string pref to be safe
|
|
||||||
// Default should be imported by constructor regardless
|
|
||||||
public String getString(String key) {
|
|
||||||
return this.store.get(key, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void parseArgs(String[] args) {
|
|
||||||
//XXX
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,14 +1,71 @@
|
|||||||
package data;
|
package data;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class StockType {
|
||||||
|
protected String name;
|
||||||
|
protected HashSet<DataSource> sources;
|
||||||
|
|
||||||
|
// Sources is empty
|
||||||
|
public StockType() {
|
||||||
|
sources = new HashSet<DataSource>();
|
||||||
|
}
|
||||||
|
|
||||||
public interface StockType {
|
|
||||||
//Effects: return current price[0] and %change[1]
|
//Effects: return current price[0] and %change[1]
|
||||||
// (2 element array)
|
// (2 element array)
|
||||||
//Require: working sources
|
//Require: working sources
|
||||||
public float[] update(String idstring);
|
public double[] update(String idstring) throws IOException {
|
||||||
|
double[] result = {0.0, 0.0};
|
||||||
|
for (DataSource source : sources) {
|
||||||
|
double[] sourceRes = source.update(name, idstring);
|
||||||
|
result[0] += sourceRes[0];
|
||||||
|
result[1] += sourceRes[1];
|
||||||
|
}
|
||||||
|
result[0] /= sources.size();
|
||||||
|
result[1] /= sources.size();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effects: add source to this
|
||||||
|
// add this to source, if not already added
|
||||||
|
//Modifies: this, source
|
||||||
|
public void addSource(DataSource source) {
|
||||||
|
if (!this.sources.contains(source)) {
|
||||||
|
this.sources.add(source);
|
||||||
|
source.addStype(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effects: add source to this
|
||||||
|
// add this to source, if not already added
|
||||||
|
//Modifies: this, source
|
||||||
|
public void delSource(DataSource source) {
|
||||||
|
if (this.sources.contains(source)) {
|
||||||
|
this.sources.remove(source);
|
||||||
|
source.delStype(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Effects: return name of type
|
//Effects: return name of type
|
||||||
//Require: working sources
|
public String getName() {
|
||||||
public String getName();
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj == this) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!(obj instanceof StockType)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
StockType sobj = (StockType) obj;
|
||||||
|
return this.name.equals(sobj.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,6 @@
|
|||||||
|
package data.exceptions;
|
||||||
|
|
||||||
|
import data.exceptions.WatchListExceptions;
|
||||||
|
|
||||||
|
public class StockNotExistsException extends WatchListExceptions {
|
||||||
|
}
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
package data.exceptions;
|
||||||
|
|
||||||
|
public abstract class WatchListExceptions extends Exception {
|
||||||
|
}
|
||||||
@ -0,0 +1,41 @@
|
|||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: get intraday price and %change through JSON given the stock ticker
|
||||||
|
@Override
|
||||||
|
public double[] update(String stype, String idstring) throws IOException {
|
||||||
|
double[] result = {0.0, 0.0};
|
||||||
|
try {
|
||||||
|
String urlString = Net.urlStringBuilder(url, "function", "TIME_SERIES_INTRADAY", "symbol", idstring,
|
||||||
|
"interval", "5min",
|
||||||
|
"apikey", apiKey);
|
||||||
|
JsonObject response = StockJson.urlToJson(urlString);
|
||||||
|
JsonObject preJson = StockJson.jsonInJson(response, "Time Series (5min)");
|
||||||
|
if (preJson == null) {
|
||||||
|
throw new IOException("Error getting data from " + name);
|
||||||
|
}
|
||||||
|
JsonObject mainJson = StockJson.timeSeriesElement(preJson, 0);
|
||||||
|
//System.out.print(mainJson);
|
||||||
|
result[0] = Double.parseDouble(StockJson.stringGetter(mainJson, "4. close"));
|
||||||
|
Double open = Double.parseDouble(StockJson.stringGetter(mainJson, "1. open"));
|
||||||
|
result[1] = (result[0] - open) / open;
|
||||||
|
} catch (ParaMismatchException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} //catch (IOException e) {
|
||||||
|
// System.out.println("Error getting data from: " + name);
|
||||||
|
//e.printStackTrace();
|
||||||
|
//}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,43 @@
|
|||||||
|
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 {
|
||||||
|
//Effect: returns a String of url built with given url and paras
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: Open connection, fires a http GET and returns the InputStream of result
|
||||||
|
public static InputStream urlToInputStream(String url) throws IOException {
|
||||||
|
try {
|
||||||
|
return (new URL(url).openStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,57 @@
|
|||||||
|
package network;
|
||||||
|
|
||||||
|
import javax.json.Json;
|
||||||
|
import javax.json.JsonObject;
|
||||||
|
import javax.json.JsonReader;
|
||||||
|
import javax.json.JsonNumber;
|
||||||
|
import javax.json.JsonArray;
|
||||||
|
import javax.json.JsonValue;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Effect: return double from jsonnumber
|
||||||
|
public static double doubleGetter(JsonObject jobj, String name) {
|
||||||
|
return jobj.getJsonNumber(name).doubleValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Effect: return jsonobject from jsonobject
|
||||||
|
public static JsonObject jsonInJson(JsonObject jobj, String name) {
|
||||||
|
return jobj.getJsonObject(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Effect: return string from jsonstring
|
||||||
|
public static String stringGetter(JsonObject jobj, String name) {
|
||||||
|
return jobj.getString(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Effect: return double from percentage string
|
||||||
|
public static double doublePercent(JsonObject jobj, String name) {
|
||||||
|
String temp = stringGetter(jobj, name);
|
||||||
|
return Double.parseDouble(temp.split("%")[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// From https://stackoverflow.com/questions/33531041/jsonobject-get-value-of-first-node-regardless-of-name
|
||||||
|
// Effect: extract the jsonobject from jsonobject by index
|
||||||
|
public static JsonObject timeSeriesElement(JsonObject jobj, int index) {
|
||||||
|
String name = (String) jobj.keySet().toArray()[index];
|
||||||
|
return jsonInJson(jobj, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
package network.exceptions;
|
||||||
|
|
||||||
|
public class ParaMismatchException extends Exception {
|
||||||
|
public ParaMismatchException(String sss) {
|
||||||
|
super(sss);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,18 +1,83 @@
|
|||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.SwingUtilities;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import javax.swing.JComponent;
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import javax.swing.GroupLayout;
|
||||||
|
import java.awt.Container;
|
||||||
|
import java.awt.LayoutManager;
|
||||||
|
import ui.guicomp.*;
|
||||||
|
import data.WatchList;
|
||||||
|
import data.ListOfWatchList;
|
||||||
|
import static data.Const.PROGRAM_NAME;
|
||||||
|
import javax.swing.GroupLayout.Alignment;
|
||||||
|
|
||||||
public class Gui {
|
// based on http://zetcode.com/tutorials/javaswingtutorial/firstprograms/
|
||||||
//public Gui() {
|
public class Gui extends JFrame implements Iface {
|
||||||
// JLabel label = new JLabel("Hello World");
|
private JButton addButton;
|
||||||
|
private JButton delButton;
|
||||||
|
private JButton upButton;
|
||||||
|
private WatchTablePane wtable;
|
||||||
|
private WatchList wlist;
|
||||||
|
private Runnable init = new Runnable() {
|
||||||
|
//Effect: Init gui all components
|
||||||
|
//Modifies: this
|
||||||
|
public void run() {
|
||||||
|
addComponents();
|
||||||
|
createLayout();
|
||||||
|
setTitle(PROGRAM_NAME);
|
||||||
|
setSize(300, 200);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
|
||||||
// JFrame.setDefaultLookAndFeelDecorated(true);
|
private void addComponents() {
|
||||||
// JFrame f = new JFrame("Hello World");
|
addButton = new AddButt(wlist);
|
||||||
// f.setSize(300,150);
|
delButton = new DelButt(wlist);
|
||||||
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
upButton = new UpButt(wlist);
|
||||||
|
wtable = new WatchTablePane(wlist);
|
||||||
|
}
|
||||||
|
|
||||||
// f.add(label);
|
private void createLayout() {
|
||||||
|
Container pane = getContentPane();
|
||||||
|
GroupLayout lman = new GroupLayout(pane);
|
||||||
|
pane.setLayout(lman);
|
||||||
|
lman.setAutoCreateContainerGaps(true);
|
||||||
|
lman.setHorizontalGroup(lman.createParallelGroup(GroupLayout.Alignment.CENTER)
|
||||||
|
.addComponent(wtable.getPane())
|
||||||
|
.addGroup(lman.createSequentialGroup()
|
||||||
|
.addComponent(addButton)
|
||||||
|
.addComponent(delButton)
|
||||||
|
.addComponent(upButton)
|
||||||
|
));
|
||||||
|
lman.setVerticalGroup(lman.createSequentialGroup()
|
||||||
|
.addComponent(wtable.getPane())
|
||||||
|
.addGroup(lman.createParallelGroup(GroupLayout.Alignment.CENTER)
|
||||||
|
.addComponent(addButton)
|
||||||
|
.addComponent(delButton)
|
||||||
|
.addComponent(upButton)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// f.setVisible(true);
|
public Gui() {
|
||||||
//}
|
wlist = ListOfWatchList.getList().getWatchList(0);
|
||||||
|
SwingUtilities.invokeLater(init);
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: nothing
|
||||||
|
//not enough time to do something useful for this
|
||||||
|
@Override
|
||||||
|
public void redraw() {
|
||||||
|
//Nothing for now
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: nothing
|
||||||
|
//not enough time to do something useful for this
|
||||||
|
@Override
|
||||||
|
public void destory() {
|
||||||
|
//Nothing for now
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,17 +1,13 @@
|
|||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import data.Options;
|
|
||||||
import ui.Tui;
|
import ui.Tui;
|
||||||
|
|
||||||
public class IfaceFactory {
|
public class IfaceFactory {
|
||||||
// Produce Iface given the config
|
// Produce Iface given the config
|
||||||
public static Iface getIface(Main mainobj) {
|
public static Iface getIface() {
|
||||||
//uses Tui for now
|
//uses Tui for now
|
||||||
return new Tui(mainobj);
|
//return new Tui();
|
||||||
|
//Test Gui
|
||||||
|
return new Gui();
|
||||||
}
|
}
|
||||||
//public static Iface getIface(Options iOpts) {
|
|
||||||
// //XXX iOpts not ready
|
|
||||||
// //uses Tui for now
|
|
||||||
// return new Tui(iOpts);
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,70 +1,23 @@
|
|||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import data.Const;
|
import data.Const;
|
||||||
import data.Options;
|
|
||||||
import data.StypeMap;
|
import data.StypeMap;
|
||||||
import data.StockType;
|
import data.StockType;
|
||||||
import data.WatchList;
|
import data.WatchList;
|
||||||
import ui.IfaceFactory;
|
import ui.IfaceFactory;
|
||||||
|
import data.exceptions.*;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
private Iface iface;
|
private Iface iface;
|
||||||
private Options allOptions;
|
|
||||||
private WatchList mainList;
|
|
||||||
private StypeMap stypeMap;
|
|
||||||
|
|
||||||
//Constructor, not the java main
|
//Constructor, not the java main
|
||||||
public Main(String[] args) {
|
public Main(String[] args) {
|
||||||
//init options, it will load defaults
|
WatchList mainList = new WatchList();
|
||||||
//from resource xml
|
iface = IfaceFactory.getIface();
|
||||||
allOptions = new Options();
|
|
||||||
//parse args, uses
|
|
||||||
allOptions.parseArgs(args);
|
|
||||||
stypeMap = new StypeMap();
|
|
||||||
mainList = new WatchList(this);
|
|
||||||
//initalize UI thread, options not ready
|
|
||||||
//this.Iface = IfaceFactory.getIface(allOptions.getSection("ui"));
|
|
||||||
iface = IfaceFactory.getIface(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Constructor for testing
|
|
||||||
public Main(boolean debug) {
|
|
||||||
//init options, it will load defaults
|
|
||||||
//from resource xml
|
|
||||||
allOptions = new Options();
|
|
||||||
stypeMap = new StypeMap();
|
|
||||||
mainList = new WatchList(this, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// java main
|
// java main
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Main(args);
|
new Main(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WatchList getWatchList() {
|
|
||||||
//XXX volatile, threaded consideration
|
|
||||||
//instant/cached?
|
|
||||||
return this.mainList;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Consider moving all to watchlist
|
|
||||||
public void addWatchStock(String target) {
|
|
||||||
//XXX Concurrency not ready
|
|
||||||
// Should add runnable to executor
|
|
||||||
// nyse only for now
|
|
||||||
StockType stype = this.stypeMap.getStype("Nyse");
|
|
||||||
mainList.addStock(target, stype);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Consider moving all to watchlist
|
|
||||||
public void delWatchStock(String target) {
|
|
||||||
//XXX Concurrency not ready
|
|
||||||
// Should add runnable to executor
|
|
||||||
mainList.delStock(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Consider moving all to watchlist
|
|
||||||
public void saveWatch(String file) {
|
|
||||||
mainList.save(file);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,37 @@
|
|||||||
|
package ui.guicomp;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import data.WatchList;
|
||||||
|
|
||||||
|
public class AddButt extends JButton {
|
||||||
|
private WatchList wlist;
|
||||||
|
|
||||||
|
public AddButt(WatchList wlist) {
|
||||||
|
super("Add Stock...");
|
||||||
|
//https://coderanch.com/t/580497/java/JButton-clicked-action
|
||||||
|
if (getActionListeners().length < 1) {
|
||||||
|
addActionListener(new AddButtList());
|
||||||
|
}
|
||||||
|
this.wlist = wlist;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class AddButtList implements ActionListener {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
//https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
|
||||||
|
//https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
|
||||||
|
String userin = (String)JOptionPane.showInputDialog("Enter the identifier:");
|
||||||
|
if ((userin != null) && (userin.length() > 0)) {
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
wlist.addStock(userin);
|
||||||
|
wlist.updateList();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,45 @@
|
|||||||
|
package ui.guicomp;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import data.WatchList;
|
||||||
|
import data.exceptions.StockNotExistsException;
|
||||||
|
|
||||||
|
public class DelButt extends JButton {
|
||||||
|
private WatchList wlist;
|
||||||
|
|
||||||
|
public DelButt(WatchList wlist) {
|
||||||
|
super("Remove Stock...");
|
||||||
|
if (getActionListeners().length < 1) {
|
||||||
|
addActionListener(new DelButtList());
|
||||||
|
}
|
||||||
|
this.wlist = wlist;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DelButtList implements ActionListener {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
//https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
|
||||||
|
//https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html
|
||||||
|
String[] names = wlist.getNames();
|
||||||
|
String userin = (String)JOptionPane.showInputDialog(
|
||||||
|
null, "Choose a stock to delete:", "Delete Stock",
|
||||||
|
JOptionPane.PLAIN_MESSAGE, null, names, null);
|
||||||
|
if ((userin != null) && (userin.length() > 0)) {
|
||||||
|
//https://stackoverflow.com/questions/12771500/best-way-of-creating-and-using-an-anonymous-runnable-class
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
wlist.delStock(userin);
|
||||||
|
} catch (StockNotExistsException e) {
|
||||||
|
//impossible
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
package ui.guicomp;
|
||||||
|
|
||||||
|
import javax.swing.JButton;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import data.WatchList;
|
||||||
|
|
||||||
|
public class UpButt extends JButton {
|
||||||
|
private WatchList wlist;
|
||||||
|
|
||||||
|
public UpButt(WatchList wlist) {
|
||||||
|
super("Update");
|
||||||
|
if (getActionListeners().length < 1) {
|
||||||
|
addActionListener(new UpButtList());
|
||||||
|
}
|
||||||
|
this.wlist = wlist;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class UpButtList implements ActionListener {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
wlist.updateList();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,80 @@
|
|||||||
|
package ui.guicomp;
|
||||||
|
|
||||||
|
import javax.swing.table.AbstractTableModel;
|
||||||
|
import javax.swing.table.TableModel;
|
||||||
|
import javax.swing.event.*;
|
||||||
|
import data.WatchList;
|
||||||
|
import data.WatchList.Wevent;
|
||||||
|
import data.StockEntry;
|
||||||
|
import java.util.Observer;
|
||||||
|
import java.util.Observable;
|
||||||
|
import javax.swing.SwingUtilities;
|
||||||
|
|
||||||
|
// Ref: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
|
||||||
|
// Ref2: https://docs.oracle.com/javase/8/docs/api/javax/swing/table/AbstractTableModel.html
|
||||||
|
public class WatchTableModel extends AbstractTableModel implements Observer {
|
||||||
|
private final String[] colnames = {"Identifier", "Price", "% change", "Status"};
|
||||||
|
private final Object[] example = {"asdf", 0.0, "asdf", "asdf"};
|
||||||
|
private WatchList watch;
|
||||||
|
|
||||||
|
public WatchTableModel(WatchList wlist) {
|
||||||
|
watch = wlist;
|
||||||
|
watch.addObserver(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColumnCount() {
|
||||||
|
return colnames.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRowCount() {
|
||||||
|
return watch.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getColumnName(int num) {
|
||||||
|
return colnames[num];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: return generated table data for whatever asked
|
||||||
|
public Object getValueAt(int stock, int field) {
|
||||||
|
StockEntry entry = watch.getStock(stock);
|
||||||
|
switch (field) {
|
||||||
|
case 0: //Name
|
||||||
|
return entry.getID();
|
||||||
|
case 1: //Price
|
||||||
|
return entry.getPrice();
|
||||||
|
case 2: //% change
|
||||||
|
return entry.getChange() + " %";
|
||||||
|
case 3:
|
||||||
|
return entry.isUpdating() ? "Updating" : "Ok";
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class getColumnClass(int col) {
|
||||||
|
return example[col].getClass();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: updates table for different events, ran by event dispatch thread as recommanded
|
||||||
|
//Modifies: Table displayed
|
||||||
|
@Override
|
||||||
|
public void update(Observable obsed, Object event) {
|
||||||
|
Wevent weve = (Wevent) event;
|
||||||
|
switch (weve.getType()) {
|
||||||
|
case UPDATE:
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
fireTableDataChanged(); });
|
||||||
|
break;
|
||||||
|
case ADD:
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
fireTableRowsInserted(weve.getData(), weve.getData()); });
|
||||||
|
break;
|
||||||
|
case DEL:
|
||||||
|
SwingUtilities.invokeLater(() -> {
|
||||||
|
fireTableRowsDeleted(weve.getData(), weve.getData()); });
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package ui.guicomp;
|
||||||
|
|
||||||
|
import javax.swing.JTable;
|
||||||
|
import javax.swing.JScrollPane;
|
||||||
|
import javax.swing.table.TableModel;
|
||||||
|
import data.WatchList;
|
||||||
|
|
||||||
|
//https://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html
|
||||||
|
public class WatchTablePane {
|
||||||
|
private JTable table;
|
||||||
|
private TableModel tmodel;
|
||||||
|
private JScrollPane spane;
|
||||||
|
|
||||||
|
public WatchTablePane(WatchList wlist) {
|
||||||
|
tmodel = new WatchTableModel(wlist);
|
||||||
|
table = new JTable(tmodel);
|
||||||
|
spane = new JScrollPane(table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public JScrollPane getPane() {
|
||||||
|
return spane;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,70 +0,0 @@
|
|||||||
import data.WatchList;
|
|
||||||
import data.StypeMap;
|
|
||||||
import data.StockType;
|
|
||||||
import data.Nyse;
|
|
||||||
import ui.Main;
|
|
||||||
import java.io.*;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
public class WatchListTest {
|
|
||||||
private WatchList watchlist;
|
|
||||||
private Main mainObj;
|
|
||||||
private StypeMap smap;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void runBefore() {
|
|
||||||
mainObj = new Main(true);
|
|
||||||
watchlist = mainObj.getWatchList();
|
|
||||||
smap = new StypeMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testAddStock() {
|
|
||||||
StockType nyyyse = smap.getStype("Nyse");
|
|
||||||
for (int i = 0; i < 100; i++) {
|
|
||||||
watchlist.addStock(Integer.toString(i), nyyyse);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSize() {
|
|
||||||
assertEquals(watchlist.size(), 0);
|
|
||||||
StockType nyyyse = smap.getStype("Nyse");
|
|
||||||
for (int i = 0; i < 100; i++) {
|
|
||||||
watchlist.addStock(Integer.toString(i), nyyyse);
|
|
||||||
}
|
|
||||||
assertEquals(watchlist.size(), 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDelStock() {
|
|
||||||
StockType nyyyse = smap.getStype("Nyse");
|
|
||||||
for (int i = 0; i < 100; i++) {
|
|
||||||
watchlist.addStock(Integer.toString(i), nyyyse);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 50; i++) {
|
|
||||||
watchlist.delStock(Integer.toString(i));
|
|
||||||
}
|
|
||||||
assertEquals(watchlist.size(), 50);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSaveLoad() {
|
|
||||||
StockType nyyyse = smap.getStype("Nyse");
|
|
||||||
for (int i = 0; i < 100; i++) {
|
|
||||||
watchlist.addStock(Integer.toString(i), nyyyse);
|
|
||||||
}
|
|
||||||
watchlist.save("");
|
|
||||||
assertTrue(watchlist.fileExists(watchlist.DEFAULT_SAVEFILE));
|
|
||||||
Main testMain = new Main(true);
|
|
||||||
WatchList testlist = testMain.getWatchList();
|
|
||||||
testlist.load("");
|
|
||||||
assertEquals(watchlist.size(), testlist.size());
|
|
||||||
File testFile = new File(watchlist.DEFAULT_SAVEFILE);
|
|
||||||
testFile.delete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import network.AlphaVantage;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class DataSourceTest {
|
||||||
|
private StockType testStype;
|
||||||
|
private DataSource testSource;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void runBefore() {
|
||||||
|
testStype = StypeMap.getStype("NYSE");
|
||||||
|
testSource = new AlphaVantage();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: test add and double deleting for many to many relationship
|
||||||
|
@Test
|
||||||
|
public void testAddDel() {
|
||||||
|
testStype.addSource(testSource);
|
||||||
|
testSource.delStype(testStype);
|
||||||
|
testSource.delStype(testStype);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: test overridden equals and hashcode
|
||||||
|
@Test
|
||||||
|
public void testEqualsHash() {
|
||||||
|
DataSource testSource2 = new AlphaVantage();
|
||||||
|
DataSource testSource3 = new DataSource("test source", "asdf", "asdf") {
|
||||||
|
@Override
|
||||||
|
public double[] update(String asdf, String asdf2) throws IOException {
|
||||||
|
double[] asdfjkl = {0.0, 0.0};
|
||||||
|
return asdfjkl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
String asdf = "asdf";
|
||||||
|
assertEquals(testSource, testSource);
|
||||||
|
assertEquals(testSource, testSource2);
|
||||||
|
assertFalse(testSource.equals(asdf));
|
||||||
|
assertFalse(testSource.equals(testSource3));
|
||||||
|
assertEquals(testSource.hashCode(), testSource2.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import data.ListOfWatchList;
|
||||||
|
import data.WatchList;
|
||||||
|
import data.exceptions.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
public class ListOfWatchListTest {
|
||||||
|
private WatchList watchlist;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void runBefore() {
|
||||||
|
watchlist = new WatchList();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: check if only one instance is returned
|
||||||
|
@Test
|
||||||
|
public void singletonCheck() {
|
||||||
|
assertTrue(ListOfWatchList.getList() == ListOfWatchList.getList());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: check double adding and deleting watchlists
|
||||||
|
@Test
|
||||||
|
public void addDelWatchList() {
|
||||||
|
ListOfWatchList lowl = ListOfWatchList.getList();
|
||||||
|
lowl.addWatchList(watchlist);
|
||||||
|
assertEquals(watchlist, lowl.getWatchList(0));
|
||||||
|
lowl.addWatchList(watchlist);
|
||||||
|
assertEquals(watchlist, lowl.getWatchList(0));
|
||||||
|
try {
|
||||||
|
lowl.getWatchList(1);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// expected fail
|
||||||
|
}
|
||||||
|
lowl.delWatchList(watchlist);
|
||||||
|
try {
|
||||||
|
lowl.getWatchList(0);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
// expected fail
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
import data.StypeMap;
|
||||||
|
import data.StockType;
|
||||||
|
import data.Nasdaq;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
public class NasdaqTest {
|
||||||
|
private StockType naasdaq;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
// Effect: Initialize Stype
|
||||||
|
public void runBefore() {
|
||||||
|
naasdaq = StypeMap.getStype("NASDAQ");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
//Effect: test if name matches
|
||||||
|
public void testName() {
|
||||||
|
assertTrue(naasdaq.getName().equals("NASDAQ"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
//Effect: check (indirectly) if update works, require internet to AlphaVantage
|
||||||
|
public void testUpdate() {
|
||||||
|
try {
|
||||||
|
double[] farray = naasdaq.update("MSFT");
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import data.StypeMap;
|
||||||
|
import data.StockType;
|
||||||
|
import data.Nyse;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
public class NyseTest {
|
||||||
|
private StockType nyyyse;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void runBefore() {
|
||||||
|
nyyyse = StypeMap.getStype("NYSE");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: test constructor and if name matches
|
||||||
|
@Test
|
||||||
|
public void testNameInit() {
|
||||||
|
assertTrue(nyyyse.getName().equals("NYSE"));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: will connect to the internet and test update
|
||||||
|
//Require: internet connection to AlphaVantage.co
|
||||||
|
@Test
|
||||||
|
public void testUpdate() {
|
||||||
|
try {
|
||||||
|
double[] farray = nyyyse.update("MSFT");
|
||||||
|
} catch (Exception e) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: test overridden equals and hashcode
|
||||||
|
@Test
|
||||||
|
public void testEqualsHash() {
|
||||||
|
StockType testst1 = new Nyse();
|
||||||
|
StockType testst2 = new Nasdaq();
|
||||||
|
String asdf = "asdf";
|
||||||
|
assertEquals(nyyyse, nyyyse);
|
||||||
|
assertEquals(nyyyse, testst1);
|
||||||
|
assertFalse(nyyyse.equals(testst2));
|
||||||
|
assertFalse(nyyyse.equals(asdf));
|
||||||
|
assertEquals(nyyyse.hashCode(), testst1.hashCode());
|
||||||
|
assertFalse(nyyyse.hashCode() == testst2.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import data.StockEntry;
|
||||||
|
import data.StypeMap;
|
||||||
|
import data.StockType;
|
||||||
|
import data.Nyse;
|
||||||
|
import ui.Main;
|
||||||
|
import java.io.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class StockEntryTest {
|
||||||
|
private StockEntry entry;
|
||||||
|
private StockType nyyyse;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void runBefore() {
|
||||||
|
nyyyse = StypeMap.getStype("NYSE");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: test constructor, two getters and default 0 price
|
||||||
|
@Test
|
||||||
|
public void testTypeAndName() {
|
||||||
|
entry = new StockEntry(nyyyse, "test");
|
||||||
|
assertTrue(entry.getTypeName().equals("NYSE"));
|
||||||
|
assertTrue(entry.getID().equals("test"));
|
||||||
|
assertFalse(entry.isUpdating());
|
||||||
|
assertEquals(0, entry.getPrice());
|
||||||
|
assertEquals(0, entry.getChange());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: test overridden equals and hashcode
|
||||||
|
@Test
|
||||||
|
public void testEqualsHash() {
|
||||||
|
entry = new StockEntry(nyyyse, "test");
|
||||||
|
StockEntry entry2 = new StockEntry(nyyyse, "test");
|
||||||
|
StockEntry entry3 = new StockEntry(StypeMap.getStype("NASDAQ"),
|
||||||
|
"test");
|
||||||
|
String asdf = "asdf";
|
||||||
|
assertEquals(entry, entry);
|
||||||
|
assertEquals(entry, entry2);
|
||||||
|
assertFalse(entry.equals(entry3));
|
||||||
|
assertFalse(entry.equals(asdf));
|
||||||
|
assertEquals(entry.hashCode(), entry2.hashCode());
|
||||||
|
assertFalse(entry.hashCode() == entry3.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,125 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import data.WatchList;
|
||||||
|
import data.StypeMap;
|
||||||
|
import data.StockType;
|
||||||
|
import data.Nyse;
|
||||||
|
import data.exceptions.*;
|
||||||
|
import ui.Main;
|
||||||
|
import java.io.*;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
public class WatchListTest {
|
||||||
|
private WatchList watchlist;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void runBefore() {
|
||||||
|
watchlist = new WatchList();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: add stock named 1 to 100 to watchlist, test if index is keep
|
||||||
|
//counting up and if getNames() works
|
||||||
|
@Test
|
||||||
|
public void testAddStockNames() {
|
||||||
|
String[] testarray = new String[100];
|
||||||
|
String[] testgetName = new String[100];
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
watchlist.addStock(Integer.toString(i));
|
||||||
|
testarray[i] = Integer.toString(i);
|
||||||
|
}
|
||||||
|
testgetName = watchlist.getNames();
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
assertEquals(testarray[i], watchlist.getStock(i).getID());
|
||||||
|
assertEquals(Integer.toString(i), watchlist.getStock(i).getID());
|
||||||
|
assertEquals(testarray[i], testgetName[i]);
|
||||||
|
assertEquals(Integer.toString(i), testgetName[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Effect: test size function for 100 stocks
|
||||||
|
@Test
|
||||||
|
public void testSize() {
|
||||||
|
assertEquals(watchlist.size(), 0);
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
watchlist.addStock(Integer.toString(i));
|
||||||
|
}
|
||||||
|
assertEquals(watchlist.size(), 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDelStockNoThrow() {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
watchlist.addStock(Integer.toString(i));
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 50; i++) {
|
||||||
|
try {
|
||||||
|
watchlist.delStock(Integer.toString(i));
|
||||||
|
} catch (WatchListExceptions e) {
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(watchlist.size(), 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDelNotExistsStock() {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
watchlist.addStock(Integer.toString(i));
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
watchlist.delStock(Integer.toString(-1));
|
||||||
|
fail();
|
||||||
|
} catch (WatchListExceptions e) {
|
||||||
|
//expected to throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSaveLoad() {
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
watchlist.addStock(Integer.toString(i));
|
||||||
|
}
|
||||||
|
watchlist.save("");
|
||||||
|
assertTrue(watchlist.fileExists(watchlist.DEFAULT_SAVEFILE));
|
||||||
|
WatchList testlist = new WatchList();
|
||||||
|
testlist.load("");
|
||||||
|
assertEquals(watchlist.size(), testlist.size());
|
||||||
|
File testFile = new File(watchlist.DEFAULT_SAVEFILE);
|
||||||
|
testFile.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFileNotFound() {
|
||||||
|
//redirect stdout: https://stackoverflow.com/questions/1119385/junit-test-for-system-out-println
|
||||||
|
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
|
||||||
|
ByteArrayOutputStream errContent = new ByteArrayOutputStream();
|
||||||
|
PrintStream originalOut = System.out;
|
||||||
|
PrintStream originalErr = System.err;
|
||||||
|
System.setOut(new PrintStream(outContent));
|
||||||
|
System.setErr(new PrintStream(errContent));
|
||||||
|
watchlist.load("thisfiledoesnotexist-andwillneverexists");
|
||||||
|
assertEquals("File not found: thisfiledoesnotexist-andwillneverexists\n", outContent.toString());
|
||||||
|
System.setOut(originalOut);
|
||||||
|
System.setErr(originalErr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateEmptyList() {
|
||||||
|
watchlist.updateList();
|
||||||
|
watchlist.addStock("MSFT");
|
||||||
|
watchlist.updateList();
|
||||||
|
//https://stackoverflow.com/questions/24104313/how-do-i-make-a-delay-in-java
|
||||||
|
try {
|
||||||
|
Thread.sleep(500);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail();
|
||||||
|
}
|
||||||
|
watchlist.updateList();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue