You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
J-WatchList/src/main/ui/Tui.java

174 lines
4.9 KiB

package ui;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import ui.Main;
import data.Const;
import data.WatchList;
import data.StockEntry;
import data.ListOfWatchList;
import data.exceptions.*;
public class Tui implements Iface {
private static final String SAVE_CURSOR = "\u001b[s";
private static final String RESTORE_CURSOR = "\u001b[s";
private static final String REQUEST_CURSOR = "\u001b[6n";
private int maxrow;
private int maxcol;
private BufferedReader stdin;
public Tui() {
stdin = new BufferedReader(new InputStreamReader(System.in));
getMax();
//XXX Start ui thread
demo();
}
public String getInputLine() {
String result;
try {
// if (stdin.ready() && (result = stdin.readLine()) != null) {
// return result;
// } else {
// return "";
// }
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];
}
@Override
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();
}
WatchList watch = ListOfWatchList.getList().getWatchList(0);
watch.save("");
System.out.println("Thank you for using " + Const.PROGRAM_NAME + "!");
}
private void printWatchList() {
WatchList watch = ListOfWatchList.getList().getWatchList(0);
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());
}
}
private void addWatch() {
System.out.print("Enter your stock number and press enter: ");
String userin = getInputLine();
WatchList watch = ListOfWatchList.getList().getWatchList(0);
watch.addStock(userin);
}
private void delWatch() {
boolean success = false;
System.out.print("Enter your stock number and press enter: ");
String userin = getInputLine();
WatchList watch = ListOfWatchList.getList().getWatchList(0);
try {
watch.delStock(userin);
success = true;
} catch (StockNotExistsException e) {
System.out.println("Stock: " + userin + " doesn't exists");
} finally {
System.out.println(success ? "Deleted: " + userin : "Failed to delete: " + userin);
}
}
}