parent
f4e3993133
commit
f7137a818b
@ -0,0 +1,7 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
// Const for everywhere
|
||||||
|
public class Const {
|
||||||
|
public static final String PROGRAM_NAME = "J-WatchList";
|
||||||
|
public static final String USAGE_TEXT = "Usage: Placeholder";
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class Nyse implements StockType {
|
||||||
|
private static final String NAME = "NYSE";
|
||||||
|
private HashSet sources;
|
||||||
|
|
||||||
|
public Nyse() {
|
||||||
|
sources = new HashSet();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
public class StockEntry {
|
||||||
|
private String identifier;
|
||||||
|
private StockType stype;
|
||||||
|
private float price;
|
||||||
|
private float change;
|
||||||
|
|
||||||
|
public StockEntry(StockType stype, String idstring) {
|
||||||
|
identifier = idstring;
|
||||||
|
this.stype = stype;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update() {
|
||||||
|
float[] result = this.stype.update(this.identifier);
|
||||||
|
this.price = result[0];
|
||||||
|
this.change = result[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTypeName() {
|
||||||
|
return this.stype.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getID() {
|
||||||
|
return this.identifier;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,14 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public interface StockType {
|
||||||
|
//Effects: return current price[0] and %change[1]
|
||||||
|
// (2 element array)
|
||||||
|
//Require: working sources
|
||||||
|
public float[] update(String idstring);
|
||||||
|
|
||||||
|
//Effects: return name of type
|
||||||
|
//Require: working sources
|
||||||
|
public String getName();
|
||||||
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import data.StockType;
|
||||||
|
//import data.Nyse;
|
||||||
|
//import data.Nasdaq;
|
||||||
|
//import data.Jpx;
|
||||||
|
//import data.Sse;
|
||||||
|
//import data.Hkse;
|
||||||
|
|
||||||
|
public class StypeMap {
|
||||||
|
private HashMap<String, StockType> smap;
|
||||||
|
|
||||||
|
public StypeMap() {
|
||||||
|
smap = new HashMap<String, StockType>();
|
||||||
|
smap.put("Nyse", new Nyse());
|
||||||
|
//smap.put("Nasdaq", new Nasdaq());
|
||||||
|
//smap.put("Jpx", new Jpx());
|
||||||
|
//smap.put("Sse", new Sse());
|
||||||
|
//smap.put("Hkse", new Hkse());
|
||||||
|
}
|
||||||
|
|
||||||
|
public StockType getStype(String typeString) {
|
||||||
|
return smap.get(typeString);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
package data;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import data.StockEntry;
|
||||||
|
|
||||||
|
public class WatchList {
|
||||||
|
private LinkedHashMap<String, StockEntry> listdata;
|
||||||
|
|
||||||
|
public WatchList() {
|
||||||
|
this.listdata = new LinkedHashMap<String, StockEntry>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addStock(String target, StockType stype) {
|
||||||
|
//The only implementation yet
|
||||||
|
this.listdata.put(target, new StockEntry(stype, target));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delStock(String target) {
|
||||||
|
this.listdata.remove(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterator iterator() {
|
||||||
|
Set entryset = listdata.entrySet();
|
||||||
|
return entryset.iterator();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,18 @@
|
|||||||
//import data.Options;
|
package ui;
|
||||||
|
|
||||||
public abstract class Iface {
|
import data.Options;
|
||||||
|
|
||||||
|
public interface Iface {
|
||||||
//private Options ifaceOpts;
|
//private Options ifaceOpts;
|
||||||
|
|
||||||
|
//Effect: Redraw the whole interface
|
||||||
|
//Modify: This
|
||||||
|
//Require: Running thread of initialized Iface, data backend
|
||||||
|
public void redraw();
|
||||||
|
|
||||||
|
//Effect: Close all windows and disable all output
|
||||||
|
//Modify: This
|
||||||
|
//Require: Running thread of initialized Iface
|
||||||
|
public void destory();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,17 @@
|
|||||||
//import data.Options;
|
package ui;
|
||||||
//import ui.Iface;
|
|
||||||
|
import data.Options;
|
||||||
|
import ui.Tui;
|
||||||
|
|
||||||
public class IfaceFactory {
|
public class IfaceFactory {
|
||||||
//public Iface getIface(Options IfaceOptions){
|
// Produce Iface given the config
|
||||||
|
public static Iface getIface(Main mainobj) {
|
||||||
|
//uses Tui for now
|
||||||
|
return new Tui(mainobj);
|
||||||
|
}
|
||||||
|
//public static Iface getIface(Options iOpts) {
|
||||||
|
// //XXX iOpts not ready
|
||||||
|
// //uses Tui for now
|
||||||
|
// return new Tui(iOpts);
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +1,54 @@
|
|||||||
package ui;
|
package ui;
|
||||||
|
|
||||||
import java.util.*;
|
import data.Const;
|
||||||
|
import data.Options;
|
||||||
|
import data.StypeMap;
|
||||||
|
import data.StockType;
|
||||||
|
import data.WatchList;
|
||||||
|
import ui.IfaceFactory;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static final String PROGRAM_NAME = "Num guess";
|
private Iface iface;
|
||||||
//public static final String USAGE_TEXT = "Usage";
|
private Options allOptions;
|
||||||
private int win;
|
private WatchList mainList;
|
||||||
private int games;
|
private StypeMap stypeMap;
|
||||||
//private Iface iface;
|
|
||||||
//private Options allOptions;
|
|
||||||
|
|
||||||
//Constructor, not the java main
|
//Constructor, not the java main
|
||||||
public Main(String[] args) {
|
public Main(String[] args) {
|
||||||
//init options, it will load defaults
|
//init options, it will load defaults
|
||||||
//from resource xml
|
//from resource xml
|
||||||
//options = new Options();
|
allOptions = new Options();
|
||||||
//
|
|
||||||
//parse args, uses
|
//parse args, uses
|
||||||
//options.parseArgs(args);
|
allOptions.parseArgs(args);
|
||||||
|
stypeMap = new StypeMap();
|
||||||
|
mainList = new WatchList();
|
||||||
|
//initalize UI thread, options not ready
|
||||||
|
//this.Iface = IfaceFactory.getIface(allOptions.getSection("ui"));
|
||||||
|
iface = IfaceFactory.getIface(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delWatchStock(String target) {
|
||||||
|
//XXX Concurrency not ready
|
||||||
|
// Should add runnable to executor
|
||||||
|
mainList.delStock(target);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,36 +1,162 @@
|
|||||||
|
package ui;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
//import ui.Iface;
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import ui.Main;
|
||||||
|
import data.Const;
|
||||||
|
import data.WatchList;
|
||||||
|
import data.StockEntry;
|
||||||
|
|
||||||
|
|
||||||
public class Tui extends Iface {
|
public class Tui implements Iface {
|
||||||
private static final String SAVE_CURSOR = "\u001b[s";
|
private static final String SAVE_CURSOR = "\u001b[s";
|
||||||
private static final String RESTORE_CURSOR = "\u001b[s";
|
private static final String RESTORE_CURSOR = "\u001b[s";
|
||||||
private static final String REQUEST_CURSOR = "\u001b[6n";
|
private static final String REQUEST_CURSOR = "\u001b[6n";
|
||||||
private int maxcol;
|
|
||||||
private int maxrow;
|
private int maxrow;
|
||||||
|
private int maxcol;
|
||||||
private BufferedReader stdin;
|
private BufferedReader stdin;
|
||||||
|
private Main main;
|
||||||
|
|
||||||
public Tui() {
|
public Tui(Main main) {
|
||||||
// stdin = new BufferedReader(new InputStreamReader(System.in));
|
stdin = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
getMax();
|
||||||
|
this.main = main;
|
||||||
|
//XXX Start ui thread
|
||||||
|
demo();
|
||||||
}
|
}
|
||||||
|
|
||||||
//public String getInput() {
|
public String getInputLine() {
|
||||||
//}
|
String result;
|
||||||
|
try {
|
||||||
//public getCursor(){
|
// if (stdin.ready() && (result = stdin.readLine()) != null) {
|
||||||
// if (stdin.ready()) {
|
// return result;
|
||||||
// System.out.print(REQUEST_CURSOR);
|
// } else {
|
||||||
// stdin.skip(1);
|
// return "";
|
||||||
// char c;
|
|
||||||
// while (char = (char)stdin.read() != ';') {
|
|
||||||
// row =
|
|
||||||
// }
|
|
||||||
// }
|
// }
|
||||||
//}
|
result = stdin.readLine();
|
||||||
|
return result;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String readUntil(char end) {
|
||||||
|
String result = "";
|
||||||
|
char c;
|
||||||
|
try {
|
||||||
|
while ((c = (char)stdin.read()) != end) {
|
||||||
|
result = result + String.valueOf(c);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return cursor coord as int[0]=row int[1]=colums
|
||||||
|
private int[] getCursor() {
|
||||||
|
int[] result = new int[2];
|
||||||
|
try {
|
||||||
|
if (stdin.ready()) {
|
||||||
|
System.out.print(REQUEST_CURSOR);
|
||||||
|
stdin.skip(1);
|
||||||
|
String s = readUntil(';');
|
||||||
|
result[0] = Integer.parseInt(s); //row
|
||||||
|
s = readUntil('R');
|
||||||
|
result[1] = Integer.parseInt(s); //colums
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void moveCursor(int col, int row) {
|
||||||
|
System.out.print("\u001b[" + col + ";" + row + "H");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getMax() {
|
||||||
|
System.out.print(SAVE_CURSOR);
|
||||||
|
moveCursor(9999,9999); //move cursor to impossibly big coord
|
||||||
|
int[] maxCoord = getCursor();
|
||||||
|
maxrow = maxCoord[0];
|
||||||
|
maxcol = maxCoord[1];
|
||||||
|
}
|
||||||
|
|
||||||
//public void moveCursor(int col, int row) {
|
@Override
|
||||||
// System.out.print("\u001b["+col+";"+row+"H");
|
public void destory() {
|
||||||
//}
|
//Nothing has to be done
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void redraw() {
|
||||||
|
//XXX Do nothing for now
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void demomenu() {
|
||||||
|
System.out.println("Function select:");
|
||||||
|
System.out.println("1: View watchlist");
|
||||||
|
System.out.println("2: Add stock");
|
||||||
|
System.out.println("3: Remove stock");
|
||||||
|
System.out.println("q: Quit");
|
||||||
|
System.out.println("Enter a number or q, then press enter.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean demoinput() {
|
||||||
|
switch (getInputLine()) {
|
||||||
|
case "1":
|
||||||
|
printWatchList();
|
||||||
|
break;
|
||||||
|
case "2":
|
||||||
|
addWatch();
|
||||||
|
break;
|
||||||
|
case "3":
|
||||||
|
delWatch();
|
||||||
|
break;
|
||||||
|
case "q":
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
System.out.println("Invalid input, try again.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void demo() {
|
||||||
|
System.out.println("Welcome to " + Const.PROGRAM_NAME + "!");
|
||||||
|
boolean cont = true;
|
||||||
|
while (cont) {
|
||||||
|
demomenu();
|
||||||
|
cont = demoinput();
|
||||||
|
}
|
||||||
|
System.out.println("Thank you for using " + Const.PROGRAM_NAME + "!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void printWatchList() {
|
||||||
|
WatchList watch = main.getWatchList();
|
||||||
|
Iterator watchit = watch.iterator();
|
||||||
|
while (watchit.hasNext()) {
|
||||||
|
Map.Entry entry = (Map.Entry)watchit.next();
|
||||||
|
StockEntry sentry = (StockEntry)entry.getValue();
|
||||||
|
System.out.println("Type: " + sentry.getTypeName()
|
||||||
|
+ " Identifier: " + sentry.getID());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addWatch() {
|
||||||
|
System.out.print("Enter your stock number and press enter: ");
|
||||||
|
String userin = getInputLine();
|
||||||
|
main.addWatchStock(userin);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delWatch() {
|
||||||
|
System.out.print("Enter your stock number and press enter: ");
|
||||||
|
String userin = getInputLine();
|
||||||
|
main.delWatchStock(userin);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +0,0 @@
|
|||||||
package placeholder;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
class MainTest {
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Reference in new issue