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.
126 lines
3.8 KiB
126 lines
3.8 KiB
package data;
|
|
|
|
import java.util.*;
|
|
import java.io.*;
|
|
import data.StockEntry;
|
|
import ui.Main;
|
|
import data.exceptions.*;
|
|
import observer.Subject;
|
|
|
|
public class WatchList extends Subject implements Load,Save {
|
|
private LinkedHashMap<String, StockEntry> listdata;
|
|
public static final String DEFAULT_SAVEFILE = ".jwatch.list";
|
|
|
|
// Effects: List is empty
|
|
// or loaded with save values
|
|
public WatchList() {
|
|
listdata = new LinkedHashMap<String, StockEntry>();
|
|
if (fileExists(DEFAULT_SAVEFILE)) {
|
|
load("");
|
|
}
|
|
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
|
|
// Requires: target doesn't already exists
|
|
public void addStock(String target) {
|
|
//The only implementation yet
|
|
StockType stype = StypeMap.getStype("NYSE");
|
|
this.listdata.put(target, new StockEntry(stype, target));
|
|
}
|
|
|
|
// Effects: Delete an entry with key==target
|
|
// Modifies: this.listdata
|
|
// Requires: target exists in list
|
|
public void delStock(String target) throws StockNotExistsException {
|
|
if (!listdata.containsKey(target)) {
|
|
throw new StockNotExistsException();
|
|
}
|
|
this.listdata.remove(target);
|
|
}
|
|
|
|
// Effects: Return an iterator of the list
|
|
// Warning: not thread-safe, fail-fast
|
|
public Iterator iterator() {
|
|
Set entryset = listdata.entrySet();
|
|
return entryset.iterator();
|
|
}
|
|
|
|
// 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
|
|
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) {
|
|
addStock(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);
|
|
}
|
|
}
|
|
|
|
public void updateList() {
|
|
Iterator watchit = iterator();
|
|
while (watchit.hasNext()) {
|
|
Map.Entry entry = (Map.Entry)watchit.next();
|
|
StockEntry sentry = (StockEntry)entry.getValue();
|
|
sentry.update();
|
|
}
|
|
sendMsg();
|
|
}
|
|
}
|