diff --git a/src/main/data/DataSource.java b/src/main/data/DataSource.java new file mode 100644 index 0000000..3bd9812 --- /dev/null +++ b/src/main/data/DataSource.java @@ -0,0 +1,43 @@ +package data; + +import java.util.Set; +import java.util.Map; +import java.util.HashMap; +import java.util.Iterator; + +public class DataSource { + private HashMap targets; + private String name; + private String url; + private String apiKey; + private String defapi; + + public DataSource(String name, String url, String apiKey, String defapi) { + this.name = name; + this.url = url; + this.apiKey = apiKey; + this.defapi = defapi; + } + + //Effect: add target to this + // add this to target if not already done + //Modifies: this, stype + public void addStype(StockType stype) { + String sname = stype.getClass().getName(); + if (!this.targets.containsKey(sname)) { + targets.put(sname,stype); + stype.addSource(this); + } + } + + //Effect: del target to this + // del this to target if not already done + //Modifies: this, stype + public void delStype(StockType stype) { + String sname = stype.getClass().getName(); + if (this.targets.containsKey(sname)) { + targets.remove(sname,stype); + stype.delSource(this); + } + } +} diff --git a/src/main/data/StockType.java b/src/main/data/StockType.java index 78b5bbd..2605945 100644 --- a/src/main/data/StockType.java +++ b/src/main/data/StockType.java @@ -24,9 +24,46 @@ public abstract class StockType { return result; } + //Effects: add source to this + // add this to source, if not already added + //Modifies: this, source + public void addSource(DataSource source) { + if (!this.sources.contains(source)) { + this.sources.add(source); + source.addStype(this); + } + } + + //Effects: add source to this + // add this to source, if not already added + //Modifies: this, source + public void delSource(DataSource source) { + if (this.sources.contains(source)) { + this.sources.remove(source); + source.delStype(this); + } + } + //Effects: return name of type //Require: working sources public String getName() { return this.name; } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof StockType)) { + return false; + } + StockType sobj = (StockType) obj; + return this.name.equals(sobj.getName()); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } }