diff --git a/Main.java b/Main.java index d0d83a5..61f9947 100644 --- a/Main.java +++ b/Main.java @@ -4,8 +4,8 @@ import java.util.*; public class Main { + public static final int CLOCK_CYCLE_MILLIS = 300000; public static void main(String[] args) { - double start = System.currentTimeMillis(); LinkedList head = new LinkedList<>(); //make the view JFrame frame = new JFrame("Stocks"); @@ -16,17 +16,24 @@ public static void main(String[] args) { frame.pack(); frame.setVisible(true); //initialize the list - try { - initialize(head,si); - }catch (MalformedURLException m){ - System.out.println("check URLS"); - }catch (FileNotFoundException f){ - System.out.println("check file exsistance"); - } - System.out.println(""+(System.currentTimeMillis()-start)); + new Thread(()-> { + while(true){ + si.reset(); + try { + initialize(head,si); + }catch (FileNotFoundException f){ + System.out.println("check file exsistance"); + } + try { + Thread.sleep(CLOCK_CYCLE_MILLIS); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); } - static void initialize(LinkedList stocks, ShowInfo si) throws MalformedURLException, FileNotFoundException{ + static void initialize(LinkedList stocks, ShowInfo si) throws FileNotFoundException{ //initialize from file File file = new File("stocks.dt"); Scanner scan = new Scanner(file); @@ -45,6 +52,24 @@ static void initialize(LinkedList stocks, ShowInfo si) throws MalformedUR t.start(); threads.add(t); } + + //wait for all threads to finish + boolean isAlive = true; + while(isAlive){ + isAlive = false; + for (int i = 0; i < threads.size() && !isAlive; i++) { + isAlive = threads.get(i).isAlive(); + } + } + //all thread finished, sum every thing up to make summery + //the amount and rate arent interesting + double sumBought = 0; + double sumWorth = 0; + for (Stock s : stocks){ + sumBought+=s.getBought(); + sumWorth += ((int)(s.getAmount()*s.getCurValue()/100 * 100)) / 100.0; + } + si.notify(new String[]{"Summery","-","-",""+sumWorth,""+(((int)((sumWorth-sumBought)*100))/100.0)}); } } diff --git a/ShowInfo.java b/ShowInfo.java index dc5f4fc..c7df25b 100644 --- a/ShowInfo.java +++ b/ShowInfo.java @@ -1,9 +1,5 @@ import javax.swing.*; import javax.swing.table.DefaultTableModel; -import javax.swing.table.TableModel; -import javax.swing.table.TableRowSorter; -import java.util.ArrayList; -import java.util.List; public class ShowInfo { @@ -37,9 +33,20 @@ private String[] parseStock(Stock stock){ } public void notify(Stock stock){ + notify(parseStock(stock)); + } + + public void notify(String[] summery){ DefaultTableModel model = (DefaultTableModel) tbInfo.getModel(); - model.addRow(parseStock(stock)); + model.addRow(summery); frame.revalidate(); frame.repaint(); } + + public void reset(){ + DefaultTableModel dm = (DefaultTableModel) tbInfo.getModel(); + for (int i= dm.getRowCount() -1; i>=0;i--){ + dm.removeRow(i); + } + } } diff --git a/Stock.java b/Stock.java index d31116b..61ef278 100644 --- a/Stock.java +++ b/Stock.java @@ -8,7 +8,6 @@ import java.util.Date; public class Stock { - private final int MINUTES_DIFFERENCE = 10; private String name; private URL investingURL; private int amount; @@ -63,7 +62,7 @@ public Stock(Stock copy){ */ public double getCurValue(){ if(curValue != -1){ - if(lastUpdate != null && ((new Date()).getTime() - lastUpdate.getTime()) > MINUTES_DIFFERENCE*60*1000){ + if(lastUpdate != null && ((new Date()).getTime() - lastUpdate.getTime()) > Main.CLOCK_CYCLE_MILLIS){ updateCurValue(); } }else{ diff --git a/out/Stocks-v1.5.jar b/out/Stocks-v1.5.jar new file mode 100644 index 0000000..4334492 Binary files /dev/null and b/out/Stocks-v1.5.jar differ diff --git a/out/Stocks.jar b/out/Stocks.jar deleted file mode 100644 index f36f0f9..0000000 Binary files a/out/Stocks.jar and /dev/null differ