|
|
|
|
@ -1,14 +1,29 @@
|
|
|
|
|
package data;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import data.StockEntry;
|
|
|
|
|
import ui.Main;
|
|
|
|
|
|
|
|
|
|
public class WatchList {
|
|
|
|
|
public class WatchList implements Load,Save {
|
|
|
|
|
private LinkedHashMap<String, StockEntry> listdata;
|
|
|
|
|
private Main mainObj;
|
|
|
|
|
public static final String DEFAULT_SAVEFILE = ".jwatch.list";
|
|
|
|
|
|
|
|
|
|
// Effects: List is empty
|
|
|
|
|
public WatchList() {
|
|
|
|
|
this.listdata = new LinkedHashMap<String, StockEntry>();
|
|
|
|
|
// or loaded with save values
|
|
|
|
|
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
|
|
|
|
|
@ -38,4 +53,53 @@ public class WatchList {
|
|
|
|
|
public int 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|