diff --git a/src/main/data/DataSource.java b/src/main/data/DataSource.java index 6928c6c..2f29af6 100644 --- a/src/main/data/DataSource.java +++ b/src/main/data/DataSource.java @@ -5,6 +5,7 @@ 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 targets; @@ -65,5 +66,5 @@ public abstract class DataSource { return Objects.hash(name); } - public abstract double[] update(String stype, String idstring); + public abstract double[] update(String stype, String idstring) throws IOException; } diff --git a/src/main/data/StockEntry.java b/src/main/data/StockEntry.java index e54f396..2f993f9 100644 --- a/src/main/data/StockEntry.java +++ b/src/main/data/StockEntry.java @@ -1,6 +1,7 @@ package data; import java.util.Objects; +import java.io.IOException; public class StockEntry { private String identifier; @@ -17,7 +18,14 @@ public class StockEntry { public boolean update() { boolean changed = false; updating = true; - double[] result = this.stype.update(this.identifier); + double[] result; + try { + result = this.stype.update(this.identifier); + } catch (IOException e) { + System.out.println("Error getting update"); + e.printStackTrace(); + return false; + } if ((result[0] != price) || (result[1] != change)) { changed = true; } diff --git a/src/main/data/StockType.java b/src/main/data/StockType.java index 9e875ae..50726f8 100644 --- a/src/main/data/StockType.java +++ b/src/main/data/StockType.java @@ -1,6 +1,7 @@ package data; import java.util.*; +import java.io.IOException; public abstract class StockType { protected String name; @@ -14,7 +15,7 @@ public abstract class StockType { //Effects: return current price[0] and %change[1] // (2 element array) //Require: working sources - public double[] 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); diff --git a/src/main/data/WatchList.java b/src/main/data/WatchList.java index a545849..524b2bb 100644 --- a/src/main/data/WatchList.java +++ b/src/main/data/WatchList.java @@ -142,10 +142,8 @@ public class WatchList extends Observable implements Load,Save { } sentry.update(); } - if (changed) { - setChanged(); - notifyObservers(new Wevent(Etype.UPDATE)); - } + setChanged(); + notifyObservers(new Wevent(Etype.UPDATE)); } public class Wevent { diff --git a/src/main/network/AlphaVantage.java b/src/main/network/AlphaVantage.java index dc0acec..c551a3d 100644 --- a/src/main/network/AlphaVantage.java +++ b/src/main/network/AlphaVantage.java @@ -13,7 +13,7 @@ public class AlphaVantage extends DataSource { } @Override - public double[] update(String stype, String idstring) { + 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, @@ -21,17 +21,20 @@ public class AlphaVantage extends DataSource { "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); + //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); + } //catch (IOException e) { + // System.out.println("Error getting data from: " + name); //e.printStackTrace(); - } + //} return result; } } diff --git a/src/main/network/StockJson.java b/src/main/network/StockJson.java index 61254b6..9a3c970 100644 --- a/src/main/network/StockJson.java +++ b/src/main/network/StockJson.java @@ -47,6 +47,7 @@ public class StockJson { // From https://stackoverflow.com/questions/33531041/jsonobject-get-value-of-first-node-regardless-of-name public static JsonObject timeSeriesElement(JsonObject jobj, int index) { String name = (String) jobj.keySet().toArray()[index]; + System.out.println(name); return jsonInJson(jobj, name); } } diff --git a/src/main/ui/guicomp/AddButt.java b/src/main/ui/guicomp/AddButt.java index 0de4675..ad125e5 100644 --- a/src/main/ui/guicomp/AddButt.java +++ b/src/main/ui/guicomp/AddButt.java @@ -11,7 +11,10 @@ public class AddButt extends JButton { public AddButt(WatchList wlist) { super("Add Stock..."); - addActionListener(new AddButtList()); + //https://coderanch.com/t/580497/java/JButton-clicked-action + if (getActionListeners().length < 1) { + addActionListener(new AddButtList()); + } this.wlist = wlist; } @@ -19,9 +22,9 @@ public class AddButt extends JButton { 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 + System.out.println("Count of listeners: " + ((JButton) e.getSource()).getActionListeners().length); String userin = (String)JOptionPane.showInputDialog("Enter the identifier:"); 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() { diff --git a/src/main/ui/guicomp/DelButt.java b/src/main/ui/guicomp/DelButt.java index 476ac0a..57a31dd 100644 --- a/src/main/ui/guicomp/DelButt.java +++ b/src/main/ui/guicomp/DelButt.java @@ -12,7 +12,9 @@ public class DelButt extends JButton { public DelButt(WatchList wlist) { super("Remove Stock..."); - addActionListener(new DelButtList()); + if (getActionListeners().length < 1) { + addActionListener(new DelButtList()); + } this.wlist = wlist; } diff --git a/src/main/ui/guicomp/UpButt.java b/src/main/ui/guicomp/UpButt.java index b77944c..090d9e8 100644 --- a/src/main/ui/guicomp/UpButt.java +++ b/src/main/ui/guicomp/UpButt.java @@ -10,7 +10,9 @@ public class UpButt extends JButton { public UpButt(WatchList wlist) { super("Update"); - addActionListener(new UpButtList()); + if (getActionListeners().length < 1) { + addActionListener(new UpButtList()); + } this.wlist = wlist; } diff --git a/src/test/data/NasdaqTest.java b/src/test/data/NasdaqTest.java index bd84fa7..d261448 100644 --- a/src/test/data/NasdaqTest.java +++ b/src/test/data/NasdaqTest.java @@ -8,6 +8,7 @@ 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; @@ -24,6 +25,10 @@ public class NasdaqTest { @Test public void testUpdate() { - double[] farray = naasdaq.update("MSFT"); + try { + double[] farray = naasdaq.update("MSFT"); + } catch (Exception e) { + fail(); + } } } diff --git a/src/test/data/NyseTest.java b/src/test/data/NyseTest.java index 9f76f62..86fbbfa 100644 --- a/src/test/data/NyseTest.java +++ b/src/test/data/NyseTest.java @@ -8,6 +8,7 @@ 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; @@ -24,7 +25,11 @@ public class NyseTest { @Test public void testUpdate() { - double[] farray = nyyyse.update("MSFT"); + try { + double[] farray = nyyyse.update("MSFT"); + } catch (Exception e) { + fail(); + } } @Test