Skip to content

Commit

Permalink
Merge pull request #11 from FIITAY/v1.5
Browse files Browse the repository at this point in the history
V1.5
  • Loading branch information
FIITAY authored Jan 20, 2020
2 parents d92f31f + f39f07f commit e6cecd7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
45 changes: 35 additions & 10 deletions Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stock> head = new LinkedList<>();
//make the view
JFrame frame = new JFrame("Stocks");
Expand All @@ -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<Stock> stocks, ShowInfo si) throws MalformedURLException, FileNotFoundException{
static void initialize(LinkedList<Stock> stocks, ShowInfo si) throws FileNotFoundException{
//initialize from file
File file = new File("stocks.dt");
Scanner scan = new Scanner(file);
Expand All @@ -45,6 +52,24 @@ static void initialize(LinkedList<Stock> 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)});
}
}

Expand Down
17 changes: 12 additions & 5 deletions ShowInfo.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
}
}
}
3 changes: 1 addition & 2 deletions Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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{
Expand Down
Binary file added out/Stocks-v1.5.jar
Binary file not shown.
Binary file removed out/Stocks.jar
Binary file not shown.

0 comments on commit e6cecd7

Please sign in to comment.