Compare commits

..

No commits in common. 'deliverable_11' and 'deliverable_10' have entirely different histories.

@ -14,26 +14,26 @@ Build deps:
### .class
In a terminal:
```
git clone https://git.leung.rocks/benny/J-WatchList.git
cd J-WatchList
git clone https://github.students.cs.ubc.ca/cpsc210-2019w-t1/project_h9u2b
cd project_h9u2b
gradle clean build
```
Class files are in build/classes, to run with plain java:
```
cd J-WatchList
java -cp build/classes/java/main/:build/classes/java/main/ui ui.Main
cd project_h9u2b
java -cp build/classes/java/main/:build/classes/java/main/ui ui.Iface
```
Or run with gradle:
```
cd J-WatchList
cd project_h9u2b
gradle run
```
To debug:
```
cd J-WatchList
cd project_h9u2b
gradle debug
```
@ -44,3 +44,4 @@ Rebuild tags with:
```
gradle tags
```

@ -5,7 +5,6 @@ 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;
@ -66,6 +65,5 @@ public abstract class DataSource {
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;
public abstract double[] update(String stype, String idstring);
}

@ -15,21 +15,16 @@ public class ListOfWatchList {
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);
}

@ -1,41 +1,22 @@
package data;
import java.util.Objects;
import java.io.IOException;
public class StockEntry {
private String identifier;
private StockType stype;
private double price;
private double change;
private boolean updating = false;
public StockEntry(StockType stype, String idstring) {
identifier = idstring;
this.stype = stype;
}
//Effect: return true if price or change is/are new
// update the price and %change by the stype update
//Modifies: this
public boolean update() {
boolean changed = false;
updating = true;
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;
}
public void update() {
double[] result = this.stype.update(this.identifier);
this.price = result[0];
this.change = result[1];
updating = false;
return changed;
}
public String getTypeName() {
@ -54,10 +35,6 @@ public class StockEntry {
return change;
}
public boolean isUpdating() {
return updating;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {

@ -1,7 +1,6 @@
package data;
import java.util.*;
import java.io.IOException;
public abstract class StockType {
protected String name;
@ -15,7 +14,7 @@ public abstract class StockType {
//Effects: return current price[0] and %change[1]
// (2 element array)
//Require: working sources
public double[] update(String idstring) throws IOException {
public double[] update(String idstring) {
double[] result = {0.0, 0.0};
for (DataSource source : sources) {
double[] sourceRes = source.update(name, idstring);

@ -31,7 +31,6 @@ public class StypeMap {
// Effects: Returns the StockType
// Require: Valid typeString
// note: this is a shortcut to getMap and then getInstStype
public static StockType getStype(String typeString) {
return stypemap.getInstStype(typeString);
}

@ -5,16 +5,12 @@ import java.io.*;
import data.StockEntry;
import ui.Main;
import data.exceptions.*;
import java.util.Observable;
import observer.Subject;
public class WatchList extends Observable implements Load,Save {
public class WatchList extends Subject implements Load,Save {
private LinkedHashMap<String, StockEntry> listdata;
public static final String DEFAULT_SAVEFILE = ".jwatch.list";
public enum Etype {
UPDATE, ADD, DEL
}
// Effects: List is empty
// or loaded with save values
public WatchList() {
@ -25,6 +21,11 @@ public class WatchList extends Observable implements Load,Save {
ListOfWatchList.getList().addWatchList(this);
}
// Debug constructor
public WatchList(boolean debug) {
listdata = new LinkedHashMap<String, StockEntry>();
}
// Effects: Add an entry with key==target
// (XXX For now, only Nyse)
// Modifies: this.listdata
@ -33,8 +34,6 @@ public class WatchList extends Observable implements Load,Save {
//The only implementation yet
StockType stype = StypeMap.getStype("NYSE");
this.listdata.put(target, new StockEntry(stype, target));
setChanged();
notifyObservers(new Wevent(Etype.ADD, size()));
}
// Effects: Delete an entry with key==target
@ -44,15 +43,7 @@ public class WatchList extends Observable implements Load,Save {
if (!listdata.containsKey(target)) {
throw new StockNotExistsException();
}
int index = new ArrayList(listdata.keySet()).indexOf(target);
this.listdata.remove(target);
setChanged();
notifyObservers(new Wevent(Etype.DEL, index));
}
// Effects: return an array of all Stock names in list
public String[] getNames() {
return listdata.keySet().toArray(new String[0]);
}
// Effects: Return an iterator of the list
@ -62,10 +53,10 @@ public class WatchList extends Observable implements Load,Save {
return entryset.iterator();
}
//Effects: return a stockentry given its index
public StockEntry getStock(int index) {
String key = (String) listdata.keySet().toArray()[index];
return (StockEntry) listdata.get(key);
// Effects: Return an readonly iterator of the list
public Iterator roiterator() {
Set entryset = Collections.unmodifiableSet(listdata.entrySet());
return entryset.iterator();
}
// Effects: Return the size of list
@ -73,7 +64,6 @@ public class WatchList extends Observable implements Load,Save {
return listdata.size();
}
//Effect: save list to a file, save to DEFAULT_SAVEFILE if filename is empty
@Override
public void save(String filename) {
if (filename.equals("")) {
@ -96,15 +86,12 @@ public class WatchList extends Observable implements Load,Save {
}
}
//Effect: return true if filename exists and is a file
@Override
public boolean fileExists(String filename) {
File fileObj = new File(filename);
return fileObj.isFile();
}
//Effect: load list from file, load from DEFAULT_SAVEFILE if filename is empty
//Modifies: this
@Override
public void load(String filename) {
if (filename.equals("")) {
@ -126,42 +113,13 @@ public class WatchList extends Observable implements Load,Save {
}
}
//Effect: perform price and %update for @ stockentry in list
//Modifies: this, stockentries
public void updateList() {
Iterator watchit = iterator();
boolean changed = false;
while (watchit.hasNext()) {
Map.Entry entry = (Map.Entry)watchit.next();
StockEntry sentry = (StockEntry)entry.getValue();
if (!changed) {
changed = changed || sentry.update();
}
sentry.update();
}
setChanged();
notifyObservers(new Wevent(Etype.UPDATE));
}
public class Wevent {
private Etype type;
private int data;
public Wevent(Etype type) {
this(type, 0);
}
public Wevent(Etype type, int num) {
this.type = type;
this.data = num;
}
public Etype getType() {
return type;
}
public int getData() {
return data;
}
sendMsg();
}
}

@ -7,35 +7,28 @@ 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 {
public double[] update(String stype, String idstring) {
double[] result = {0.0, 0.0};
try {
String urlString = Net.urlStringBuilder(url, "function", "TIME_SERIES_INTRADAY", "symbol", idstring,
"interval", "5min",
String urlString = Net.urlStringBuilder(url,
"function", "GLOBAL_QUOTE",
"symbol", idstring,
"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;
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();
//}
} catch (IOException e) {
System.out.println("Error getting data from: " + name);
e.printStackTrace();
}
return result;
}
}

@ -10,7 +10,6 @@ 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";
@ -32,7 +31,6 @@ public class Net {
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());

@ -4,11 +4,8 @@ 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 {
@ -27,31 +24,20 @@ public class StockJson {
}
}
// 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,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);
}

@ -1,83 +1,18 @@
package ui;
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;
import javax.swing.*;
// based on http://zetcode.com/tutorials/javaswingtutorial/firstprograms/
public class Gui extends JFrame implements Iface {
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);
}
public class Gui {
//public Gui() {
// JLabel label = new JLabel("Hello World");
private void addComponents() {
addButton = new AddButt(wlist);
delButton = new DelButt(wlist);
upButton = new UpButt(wlist);
wtable = new WatchTablePane(wlist);
}
// JFrame.setDefaultLookAndFeelDecorated(true);
// JFrame f = new JFrame("Hello World");
// f.setSize(300,150);
// f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.add(label);
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
}
// f.setVisible(true);
//}
}

@ -1,5 +1,7 @@
package ui;
import ui.Options;
public interface Iface {
//private Options ifaceOpts;

@ -1,13 +1,17 @@
package ui;
import ui.Options;
import ui.Tui;
public class IfaceFactory {
// Produce Iface given the config
public static Iface getIface() {
//uses Tui for now
//return new Tui();
//Test Gui
return new Gui();
return new Tui();
}
//public static Iface getIface(Options iOpts) {
// //XXX iOpts not ready
// //uses Tui for now
// return new Tui(iOpts);
//}
}

@ -1,6 +1,7 @@
package ui;
import data.Const;
import ui.Options;
import data.StypeMap;
import data.StockType;
import data.WatchList;
@ -9,13 +10,29 @@ import data.exceptions.*;
public class Main {
private Iface iface;
private Options allOptions;
//Constructor, not the java main
public Main(String[] args) {
//init options, it will load defaults
//from resource xml
allOptions = new Options();
//parse args, uses
allOptions.parseArgs(args);
WatchList mainList = new WatchList();
//initalize UI thread, options not ready
//this.Iface = IfaceFactory.getIface(allOptions.getSection("ui"));
iface = IfaceFactory.getIface();
}
//Constructor for testing
public Main(boolean debug) {
//init options, it will load defaults
//from resource xml
allOptions = new Options();
WatchList mainList = new WatchList();
}
// java main
public static void main(String[] args) {
new Main(args);

@ -0,0 +1,150 @@
package ui;
import java.util.prefs.Preferences;
import java.util.prefs.BackingStoreException;
import java.util.prefs.InvalidPreferencesFormatException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import data.Const;
public class Options {
private Preferences store;
private boolean wasEmpty;
private boolean noSave;
private static final String DEF_OPTS_PATH = "";
private static final String DEF_BACKUP = ".jwatch-backup.xml";
private static final String RES_DEF = "defPref.xml";
public Options() {
store = Preferences.userRoot();
try {
wasEmpty = store.nodeExists(Const.PROGRAM_NAME);
store = store.node(Const.PROGRAM_NAME);
if (wasEmpty) {
//load default opts from resource
//XXX
//importDefPreferences();
}
} catch (BackingStoreException e) {
System.out.println("Critical Error: preference backing cannot be initialized");
throw new RuntimeException();
}
}
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 importPreferences(String xmlpath) {
try {
File tarFile = new File(xmlpath);
FileInputStream tarStream = new FileInputStream(tarFile);
store.importPreferences(tarStream);
tarStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + xmlpath);
} catch (IOException e) {
System.out.println("IO Error when reading: " + xmlpath);
e.printStackTrace();
} catch (InvalidPreferencesFormatException e) {
System.out.println("Import format error, possible file corruption");
}
}
public void importDefPreferences() {
try {
InputStream istream = getClass().getResourceAsStream(RES_DEF);
store.importPreferences(istream);
istream.close();
} catch (IOException e) {
System.out.println("IO Error while reading def pref: "
+ RES_DEF);
} catch (InvalidPreferencesFormatException e) {
System.out.println("Def resPref format error, possible program corruption");
}
}
public void exportPreferences(String xmlpath) {
try {
File tarFile = new File(xmlpath);
if (tarFile.isFile()) {
tarFile.delete();
} else {
tarFile.createNewFile();
}
FileOutputStream tarStream = new FileOutputStream(tarFile);
store.exportSubtree(tarStream);
tarStream.flush();
tarStream.close();
System.out.println("Exported preference to: " + xmlpath);
} catch (IOException e) {
System.out.println("IO Error when writing: " + xmlpath);
} catch (BackingStoreException e) {
System.out.println("Error retriving pref from store");
e.printStackTrace();
}
}
public void destroy() {
// Clear config if requested or config was empty
// and is not saving
if (wasEmpty && noSave) {
try {
this.store.clear();
} catch (BackingStoreException e) {
System.out.println("Error clearing pref store");
e.printStackTrace();
}
} else if (noSave) {
restorePrevious();
}
delBackup();
}
public void backupPref() {
exportPreferences(DEF_BACKUP);
System.out.println("User Pref Backup created");
}
public void delBackup() {
try {
File tarFile = new File(DEF_BACKUP);
tarFile.delete();
} catch (Exception e) {
System.out.println("Error deleting backup user pref");
e.printStackTrace();
}
}
public void restorePrevious() {
importPreferences(DEF_BACKUP);
}
// 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
}
}

@ -9,10 +9,9 @@ import data.WatchList;
import data.StockEntry;
import data.ListOfWatchList;
import data.exceptions.*;
import java.util.Observer;
import java.util.Observable;
import observer.*;
public class Tui implements Iface, Observer {
public class Tui implements Iface, Wobserver {
private static final String SAVE_CURSOR = "\u001b[s";
private static final String RESTORE_CURSOR = "\u001b[s";
private static final String REQUEST_CURSOR = "\u001b[6n";
@ -24,12 +23,11 @@ public class Tui implements Iface, Observer {
stdin = new BufferedReader(new InputStreamReader(System.in));
getMax();
WatchList watch = ListOfWatchList.getList().getWatchList(0);
watch.addObserver(this);
watch.addObs(this);
//XXX Start ui thread
demo();
}
//Effect: wait for user input and get a line (pressed Enter)
public String getInputLine() {
String result;
try {
@ -46,8 +44,6 @@ public class Tui implements Iface, Observer {
return "";
}
//Effect: capture user input, constantly
//Unused, for future dev uses
public String readUntil(char end) {
String result = "";
char c;
@ -91,22 +87,18 @@ public class Tui implements Iface, Observer {
maxcol = maxCoord[1];
}
//Effect: nothing has to be done
@Override
public void destory() {
//Nothing has to be done
return;
}
//Effect: nothing
//will be used when Tui is redesigned
@Override
public void redraw() {
//XXX Do nothing for now
return;
}
//Effect: Show a menu of functions
public void demomenu() {
System.out.println("Function select:");
System.out.println("1: Update watchlist");
@ -116,7 +108,6 @@ public class Tui implements Iface, Observer {
System.out.println("Enter a number or q, then press enter.\n");
}
//Effect: execute functions basing on user input from menu
public boolean demoinput() {
switch (getInputLine()) {
case "1":
@ -137,7 +128,6 @@ public class Tui implements Iface, Observer {
return true;
}
//Effect: main loop of program, displays menu, get input and execute functions
public void demo() {
System.out.println(" Welcome to " + Const.PROGRAM_NAME + "!");
boolean cont = true;
@ -158,7 +148,7 @@ public class Tui implements Iface, Observer {
private void printWatchList() {
WatchList watch = ListOfWatchList.getList().getWatchList(0);
Iterator watchit = watch.iterator();
Iterator watchit = watch.roiterator();
while (watchit.hasNext()) {
Map.Entry entry = (Map.Entry)watchit.next();
StockEntry sentry = (StockEntry)entry.getValue();
@ -191,9 +181,8 @@ public class Tui implements Iface, Observer {
}
}
//Effect: reprint the list when the list is updated, or if stocks are added or deleted
@Override
public void update(Observable obsed, Object event) {
public void update(Subject obsed) {
printWatchList();
}
}

@ -1,37 +0,0 @@
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();
}
}
}
}

@ -1,45 +0,0 @@
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();
}
}
}
}

@ -1,29 +0,0 @@
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();
}
}
}

@ -1,80 +0,0 @@
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;
}
}
}

@ -1,23 +0,0 @@
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;
}
}

@ -6,7 +6,6 @@ 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;
@ -18,30 +17,18 @@ public class DataSourceTest {
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());
}
}

@ -1,47 +0,0 @@
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
}
}
}

@ -8,30 +8,22 @@ 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();
}
}
}

@ -8,7 +8,6 @@ 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;
@ -18,24 +17,16 @@ public class NyseTest {
nyyyse = StypeMap.getStype("NYSE");
}
//Effect: test constructor and if name matches
@Test
public void testNameInit() {
public void testName() {
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();

@ -21,18 +21,13 @@ public class StockEntryTest {
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");
@ -43,7 +38,6 @@ public class StockEntryTest {
assertEquals(entry, entry);
assertEquals(entry, entry2);
assertFalse(entry.equals(entry3));
assertFalse(entry.equals(asdf));
assertEquals(entry.hashCode(), entry2.hashCode());
assertFalse(entry.hashCode() == entry3.hashCode());
}

@ -11,8 +11,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class StypeMapTest {
@BeforeEach
public void runBefore() {
}
@Test
//Effect: Test if Stype NYSE exists in StypeMap
public void testGetStype() {
StockType stype = StypeMap.getStype("NYSE");
assertEquals(stype.getName(), "NYSE");

@ -22,26 +22,13 @@ public class WatchListTest {
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];
public void testAddStock() {
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);
@ -107,19 +94,4 @@ public class WatchListTest {
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…
Cancel
Save