Added Save / Load interface for WatchList + test

deliverable_4
asdfasdf 6 years ago
parent d7ab73eb1c
commit 2a547116ff

@ -0,0 +1,11 @@
package data;
public interface Load {
// Effects: Load data from a file
// Modifies: this
// Requires: Existing file with name "filename"
public void load(String filename);
// Effects: Check if file exists
public boolean fileExists(String filename);
}

@ -0,0 +1,7 @@
package data;
public interface Save {
// Effects: Save data to a file
// Modifies: this
public void save(String filename);
}

@ -1,14 +1,29 @@
package data; package data;
import java.util.*; import java.util.*;
import java.io.*;
import data.StockEntry; import data.StockEntry;
import ui.Main;
public class WatchList { public class WatchList implements Load,Save {
private LinkedHashMap<String, StockEntry> listdata; private LinkedHashMap<String, StockEntry> listdata;
private Main mainObj;
public static final String DEFAULT_SAVEFILE = ".jwatch.list";
// Effects: List is empty // Effects: List is empty
public WatchList() { // or loaded with save values
this.listdata = new LinkedHashMap<String, StockEntry>(); public WatchList(Main mainObj) {
listdata = new LinkedHashMap<String, StockEntry>();
this.mainObj = mainObj;
if (fileExists(DEFAULT_SAVEFILE)) {
load("");
}
}
// Debug constructor
public WatchList(Main mainObj, boolean debug) {
listdata = new LinkedHashMap<String, StockEntry>();
this.mainObj = mainObj;
} }
// Effects: Add an entry with key==target // Effects: Add an entry with key==target
@ -38,4 +53,53 @@ public class WatchList {
public int size() { public int size() {
return listdata.size(); return listdata.size();
} }
@Override
public void save(String filename) {
if (filename.equals("")) {
filename = DEFAULT_SAVEFILE;
}
try {
FileWriter fwriter = new FileWriter(filename);
BufferedWriter bwriter = new BufferedWriter(fwriter);
Iterator iterate = iterator();
while (iterate.hasNext()) {
Map.Entry entry = (Map.Entry)iterate.next();
String outString = (String)entry.getKey();
System.out.println("Exported: " + outString);
bwriter.write(outString);
bwriter.newLine();
}
bwriter.close();
} catch (IOException e) {
System.out.println("IO Error when writing: " + filename);
}
}
@Override
public boolean fileExists(String filename) {
File fileObj = new File(filename);
return fileObj.isFile();
}
@Override
public void load(String filename) {
if (filename.equals("")) {
filename = DEFAULT_SAVEFILE;
}
String sss = null;
try {
FileReader freader = new FileReader(filename);
BufferedReader breader = new BufferedReader(freader);
while ((sss = breader.readLine()) != null) {
mainObj.addWatchStock(sss);
System.out.println("Imported: " + sss);
}
breader.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filename);
} catch (IOException e) {
System.out.println("IO Error when reading: " + filename);
}
}
} }

@ -21,11 +21,20 @@ public class Main {
//parse args, uses //parse args, uses
allOptions.parseArgs(args); allOptions.parseArgs(args);
stypeMap = new StypeMap(); stypeMap = new StypeMap();
mainList = new WatchList(); mainList = new WatchList(this);
//initalize UI thread, options not ready //initalize UI thread, options not ready
//this.Iface = IfaceFactory.getIface(allOptions.getSection("ui")); //this.Iface = IfaceFactory.getIface(allOptions.getSection("ui"));
iface = IfaceFactory.getIface(this); 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) {
@ -38,6 +47,7 @@ public class Main {
return this.mainList; return this.mainList;
} }
// Consider moving all to watchlist
public void addWatchStock(String target) { public void addWatchStock(String target) {
//XXX Concurrency not ready //XXX Concurrency not ready
// Should add runnable to executor // Should add runnable to executor
@ -46,9 +56,15 @@ public class Main {
mainList.addStock(target, stype); mainList.addStock(target, stype);
} }
// Consider moving all to watchlist
public void delWatchStock(String target) { public void delWatchStock(String target) {
//XXX Concurrency not ready //XXX Concurrency not ready
// Should add runnable to executor // Should add runnable to executor
mainList.delStock(target); mainList.delStock(target);
} }
// Consider moving all to watchlist
public void saveWatch(String file) {
mainList.save(file);
}
} }

@ -127,12 +127,13 @@ public class Tui implements Iface {
} }
public void demo() { public void demo() {
System.out.println("Welcome to " + Const.PROGRAM_NAME + "!"); System.out.println(" Welcome to " + Const.PROGRAM_NAME + "!");
boolean cont = true; boolean cont = true;
while (cont) { while (cont) {
demomenu(); demomenu();
cont = demoinput(); cont = demoinput();
} }
saveWatch();
System.out.println("Thank you for using " + Const.PROGRAM_NAME + "!"); System.out.println("Thank you for using " + Const.PROGRAM_NAME + "!");
} }
@ -159,4 +160,8 @@ public class Tui implements Iface {
String userin = getInputLine(); String userin = getInputLine();
main.delWatchStock(userin); main.delWatchStock(userin);
} }
public void saveWatch() {
main.saveWatch("");
}
} }

@ -2,6 +2,8 @@ import data.WatchList;
import data.StypeMap; import data.StypeMap;
import data.StockType; import data.StockType;
import data.Nyse; import data.Nyse;
import ui.Main;
import java.io.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -10,11 +12,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class WatchListTest { public class WatchListTest {
private WatchList watchlist; private WatchList watchlist;
private Main mainObj;
private StypeMap smap; private StypeMap smap;
@BeforeEach @BeforeEach
public void runBefore() { public void runBefore() {
watchlist = new WatchList(); mainObj = new Main(true);
watchlist = mainObj.getWatchList();
smap = new StypeMap(); smap = new StypeMap();
} }
@ -47,4 +51,20 @@ public class WatchListTest {
} }
assertEquals(watchlist.size(), 50); 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();
}
} }

Loading…
Cancel
Save