Added exception

deliverable_6
asdfasdf 6 years ago
parent c8df751f47
commit 1b378485f2

@ -4,6 +4,7 @@ import java.util.*;
import java.io.*; import java.io.*;
import data.StockEntry; import data.StockEntry;
import ui.Main; import ui.Main;
import data.exceptions.*;
public class WatchList implements Load,Save { public class WatchList implements Load,Save {
private LinkedHashMap<String, StockEntry> listdata; private LinkedHashMap<String, StockEntry> listdata;
@ -38,7 +39,10 @@ public class WatchList implements Load,Save {
// Effects: Delete an entry with key==target // Effects: Delete an entry with key==target
// Modifies: this.listdata // Modifies: this.listdata
// Requires: target exists in list // Requires: target exists in list
public void delStock(String target) { public void delStock(String target) throws StockNotExistsException {
if (!listdata.containsKey(target)) {
throw new StockNotExistsException();
}
this.listdata.remove(target); this.listdata.remove(target);
} }

@ -0,0 +1,6 @@
package data.exceptions;
import data.exceptions.WatchListExceptions;
public class StockNotExistsException extends WatchListExceptions {
}

@ -0,0 +1,4 @@
package data.exceptions;
public abstract class WatchListExceptions extends Exception {
}

@ -6,6 +6,7 @@ import data.StypeMap;
import data.StockType; import data.StockType;
import data.WatchList; import data.WatchList;
import ui.IfaceFactory; import ui.IfaceFactory;
import data.exceptions.*;
public class Main { public class Main {
private Iface iface; private Iface iface;
@ -60,7 +61,11 @@ public class Main {
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); try {
mainList.delStock(target);
} catch (StockNotExistsException e) {
System.out.println("Stock: " + target + " doesn't exists");
}
} }
// Consider moving all to watchlist // Consider moving all to watchlist

@ -1,3 +1,5 @@
package test;
import data.StypeMap; import data.StypeMap;
import data.StockType; import data.StockType;
import data.Nasdaq; import data.Nasdaq;

@ -1,3 +1,5 @@
package data;
import data.StypeMap; import data.StypeMap;
import data.StockType; import data.StockType;
import data.Nyse; import data.Nyse;

@ -1,3 +1,5 @@
package data;
import data.StockEntry; import data.StockEntry;
import data.StypeMap; import data.StypeMap;
import data.StockType; import data.StockType;

@ -1,3 +1,5 @@
package data;
import data.StypeMap; import data.StypeMap;
import data.StockType; import data.StockType;
import data.Nyse; import data.Nyse;

@ -1,7 +1,10 @@
package data;
import data.WatchList; import data.WatchList;
import data.StypeMap; import data.StypeMap;
import data.StockType; import data.StockType;
import data.Nyse; import data.Nyse;
import data.exceptions.*;
import ui.Main; import ui.Main;
import java.io.*; import java.io.*;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -9,6 +12,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class WatchListTest { public class WatchListTest {
private WatchList watchlist; private WatchList watchlist;
@ -18,7 +22,7 @@ public class WatchListTest {
@BeforeEach @BeforeEach
public void runBefore() { public void runBefore() {
mainObj = new Main(true); mainObj = new Main(true);
watchlist = mainObj.getWatchList(); watchlist = mainObj.getWatchList();
smap = new StypeMap(); smap = new StypeMap();
} }
@ -39,19 +43,37 @@ public class WatchListTest {
} }
assertEquals(watchlist.size(), 100); assertEquals(watchlist.size(), 100);
} }
@Test @Test
public void testDelStock() { public void testDelStockNoThrow() {
StockType nyyyse = smap.getStype("Nyse"); StockType nyyyse = smap.getStype("Nyse");
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
watchlist.addStock(Integer.toString(i), nyyyse); watchlist.addStock(Integer.toString(i), nyyyse);
} }
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
watchlist.delStock(Integer.toString(i)); try {
watchlist.delStock(Integer.toString(i));
} catch (WatchListExceptions e) {
fail();
}
} }
assertEquals(watchlist.size(), 50); assertEquals(watchlist.size(), 50);
} }
@Test
public void testDelNotExistsStock() {
StockType nyyyse = smap.getStype("Nyse");
for (int i = 0; i < 100; i++) {
watchlist.addStock(Integer.toString(i), nyyyse);
}
try {
watchlist.delStock(Integer.toString(-1));
fail();
} catch (WatchListExceptions e) {
//expected to throw
}
}
@Test @Test
public void testSaveLoad() { public void testSaveLoad() {
StockType nyyyse = smap.getStype("Nyse"); StockType nyyyse = smap.getStype("Nyse");
@ -60,9 +82,9 @@ public class WatchListTest {
} }
watchlist.save(""); watchlist.save("");
assertTrue(watchlist.fileExists(watchlist.DEFAULT_SAVEFILE)); assertTrue(watchlist.fileExists(watchlist.DEFAULT_SAVEFILE));
Main testMain = new Main(true); Main testMain = new Main(true);
WatchList testlist = testMain.getWatchList(); WatchList testlist = testMain.getWatchList();
testlist.load(""); testlist.load("");
assertEquals(watchlist.size(), testlist.size()); assertEquals(watchlist.size(), testlist.size());
File testFile = new File(watchlist.DEFAULT_SAVEFILE); File testFile = new File(watchlist.DEFAULT_SAVEFILE);
testFile.delete(); testFile.delete();
Loading…
Cancel
Save