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/data/WatchList.java

167 lines
4.8 KiB

package data;
import java.util.*;
import java.io.*;
import data.StockEntry;
import ui.Main;
import data.exceptions.*;
import java.util.Observable;
public class WatchList extends Observable implements Load,Save {
private LinkedHashMap<String, StockEntry> listdata;
public static final String DEFAULT_SAVEFILE = ".jwatch.list";
public enum Etype {
UPDATE, ADD, DEL
}
// 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));
setChanged();
notifyObservers(new Wevent(Etype.ADD, size()));
}
// 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();
}
int index = new ArrayList(listdata.keySet()).indexOf(target);
this.listdata.remove(target);
setChanged();
notifyObservers(new Wevent(Etype.DEL, index));
}
public String[] getNames() {
return listdata.keySet().toArray(new String[0]);
}
// 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();
}
public StockEntry getStock(int index) {
String key = (String) listdata.keySet().toArray()[index];
return (StockEntry) listdata.get(key);
}
// 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();
}
setChanged();
notifyObservers(new Wevent(Etype.UPDATE));
}
public class Wevent {
private Etype type;
private int data;
public Wevent(Etype type) {
this(type, 0);
}
public Wevent(Etype type, int num) {
this.type = type;
this.data = num;
}
public Etype getType() {
return type;
}
public int getData() {
return data;
}
}
}