Skip to content

Commit

Permalink
added some javadoc comments + reorganized methods into more chronolog…
Browse files Browse the repository at this point in the history
…ical order + changed access level of auxilliary classes
  • Loading branch information
Sten Laane committed Mar 22, 2019
1 parent ca41c05 commit 213c141
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 208 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.25</version>
<executions>
<execution>
<id>l4j-clui</id>
Expand All @@ -91,6 +92,7 @@
<addDependencies>true</addDependencies>
<preCp>anything</preCp>
</classPath>
<icon>src/main/resources/icon.ico</icon>
<jre>
<minVersion>1.8.0</minVersion>
</jre>
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/stocktracker/CurrencyRateFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
import java.util.Arrays;
import java.util.List;

public class CurrencyRateFetcher {
class CurrencyRateFetcher {

private XMLParser xmlParser;
private String currencyCode;
private final XMLParser xmlParser;
private final String currencyCode;

private CurrencyRateFetcher(String currencyCode) {
this.currencyCode = currencyCode;
this.xmlParser = new XMLParser();
}

public static void main(String[] args) {
writeCurrencyInfo("USD", LocalDate.of(2018, 9, 24));
}

public static void writeCurrencyInfo(String currencyCode, LocalDate firstDate) {
static void writeCurrencyInfo(String currencyCode, LocalDate firstDate) {
CurrencyRateFetcher fetcher = new CurrencyRateFetcher(currencyCode);

String url_str = "https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D." + currencyCode +
Expand All @@ -41,11 +46,6 @@ public static void writeCurrencyInfo(String currencyCode, LocalDate firstDate) {
}
}

private CurrencyRateFetcher(String currencyCode) {
this.currencyCode = currencyCode;
this.xmlParser = new XMLParser();
}

private class XMLParser
{
private void downloadXMLFile(URL url) {
Expand All @@ -67,11 +67,9 @@ private void downloadXMLFile(URL url) {
}
}


/**
* If anything ever breaks, use this:
* https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
* @param src
*/
public List<String> parse(String src) {
try {
Expand Down Expand Up @@ -129,7 +127,7 @@ private String readStream(InputStream in) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
String nextLine = "";
while ((nextLine = reader.readLine()) != null) {
sb.append(nextLine + newLine);
sb.append(nextLine).append(newLine);
}
} catch (IOException e) {
e.printStackTrace();
Expand Down
64 changes: 30 additions & 34 deletions src/main/java/stocktracker/DataAggregator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import java.util.Collections;
import java.util.List;

public class DataAggregator {
// TODO: Add workingDir field
class DataAggregator {
// TODO: Padding money and stock decimal places with zeroes
public static void main(String[] args) {
test();
Expand All @@ -23,7 +22,7 @@ private static void test() {
calculateMoney(testList, testAmounts);
}

public static void calculateMoney(List<String> ticker_currency, List<Number> stockAmounts) {
static void calculateMoney(List<String> ticker_currency, List<Number> stockAmounts) {
aggregate(ticker_currency);
List<String> finalData = FileManager.readLines(StockTracker.PATH + "aggregated_temp.txt");
List<String> dateMoney = new ArrayList<>();
Expand All @@ -44,14 +43,40 @@ public static void calculateMoney(List<String> ticker_currency, List<Number> sto
FileManager.writeList(StockTracker.PATH + "money.txt", dateMoney);
}

private static void aggregate(List<String> ticker_currency) {
String workingDir = StockTracker.PATH;
for (String combination: ticker_currency) {
aggregate(combination);
}
List<String> data;
try {
String dest = workingDir + "aggregated_temp.txt";
data = Files.readAllLines(Paths.get(workingDir + "/" + ticker_currency.get(0) + "_temp.txt"));
for (int i = 0; i < data.size(); i++) {
String line = data.get(i);
data.set(i, line.substring(0,11) + "! " + line.substring(11));
}
for (int i = 1; i < ticker_currency.size(); i++) {
List<String> fileLines = Files.readAllLines(Paths.get(workingDir + "\\" + ticker_currency.get(i) + "_temp.txt"));
for (int j = 0; j < fileLines.size(); j++) {
String stockPrice = fileLines.get(j).split(" ")[1];
String currencyRate = fileLines.get(j).split(" ")[2];
data.set(j, data.get(j) + " ! " + stockPrice + " " + currencyRate);
}
}
FileManager.writeList(dest, data);
}catch (Exception e) {
e.printStackTrace();
}
}

/**
* There are more dates in the currency file than in the ticker one because stock markets
* are closed on nation holidays. Therefore we use the the last day's stock market close
* on dates with no values. If the first day of the whole file happens to be a market
* holiday then we use the next available day's close value instead.
* @param ticker_currency
*/
public static void aggregate(String ticker_currency) {
private static void aggregate(String ticker_currency) {
String workingDir = StockTracker.PATH;
String ticker = ticker_currency.split("_")[0];
String currency = ticker_currency.split("_")[1];
Expand Down Expand Up @@ -124,33 +149,4 @@ private static void fillMissingDates(List<String> datesList, List<String> ratesL
ratesList.add(index, ratesList.get(index));
}
}

public static void aggregate(List<String> ticker_currency) {
String workingDir = StockTracker.PATH;
for (String combination: ticker_currency) {
aggregate(combination);
}
List<String> data;
try {
String dest = workingDir + "aggregated_temp.txt";
data = Files.readAllLines(Paths.get(workingDir + "/" + ticker_currency.get(0) + "_temp.txt"));
for (int i = 0; i < data.size(); i++) {
String line = data.get(i);
data.set(i, line.substring(0,11) + "! " + line.substring(11));
}
for (int i = 1; i < ticker_currency.size(); i++) {
List<String> fileLines = Files.readAllLines(Paths.get(workingDir + "\\" + ticker_currency.get(i) + "_temp.txt"));
for (int j = 0; j < fileLines.size(); j++) {
String stockPrice = fileLines.get(j).split(" ")[1];
String currencyRate = fileLines.get(j).split(" ")[2];
data.set(j, data.get(j) + " ! " + stockPrice + " " + currencyRate);
}
}
FileManager.writeList(dest, data);
}catch (Exception e) {
e.printStackTrace();
}

}

}
10 changes: 1 addition & 9 deletions src/main/java/stocktracker/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ private static void test()

System.out.println("pom.xml exists: " + FileManager.fileExists("pom.xml"));
System.out.println("b.txt exists: " + FileManager.fileExists("b.txt"));
System.out.println("saved_data is empty: " + FileManager.emptyDirectory("src\\main\\resources\\saved_data"));
System.out.println("Empty director /main/java: " + FileManager.emptyDirectory("src\\main\\java"));
System.out.println("Empty director /test/java: " + FileManager.emptyDirectory("src\\test\\java"));
System.out.println(readLines(".gitignore"));
}

Expand Down Expand Up @@ -72,7 +69,7 @@ public static List<String> readLines(String dest) {
ArrayList<String> lines = new ArrayList<>();
try {
Files.lines(Paths.get(dest))
.forEach(line -> lines.add(line));
.forEach(lines::add);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -93,9 +90,4 @@ public static boolean fileExists(String dest) {
File file = new File(dest);
return file.isFile();
}

public static boolean emptyDirectory(String dest) {
File directory = new File(dest);
return directory.list().length == 0;
}
}
36 changes: 18 additions & 18 deletions src/main/java/stocktracker/StockInfoFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import org.patriques.output.timeseries.DailyAdjusted;
import org.patriques.output.timeseries.data.StockData;

import java.io.FileWriter;
import java.time.LocalDate;
import java.util.*;

public class StockInfoFetcher {
class StockInfoFetcher {

private static final String API_KEY = "NZ04YC2MOTE5AN4P";
private static final int TIMEOUT = 3000;
Expand All @@ -27,27 +26,14 @@ private static void test() {

}

public static void getData(String ticker, LocalDate startDate) {
static void getData(String ticker, LocalDate startDate) {
Map<String, String> data = fetchData(ticker, startDate);
writeData(data, ticker);

System.out.println("Fetcing " + ticker + " done");
}

public static LocalDate getMostRecentDay() {
AlphaVantageConnector apiConnector = new AlphaVantageConnector(API_KEY, TIMEOUT);
TimeSeries stockTimeSeries = new TimeSeries(apiConnector);
try {
List<StockData> temp = stockTimeSeries.daily("IVV").getStockData();
LocalDate lastDate = temp.get(0).getDateTime().toLocalDate();
return lastDate;
}
catch (AlphaVantageException e) {
return LocalDate.now();
}
}

public static Map<String, String> fetchData(String ticker, LocalDate startDate)
private static Map<String, String> fetchData(String ticker, LocalDate startDate)
{
AlphaVantageConnector apiConnector = new AlphaVantageConnector(API_KEY, TIMEOUT);
TimeSeries stockTimeSeries = new TimeSeries(apiConnector);
Expand Down Expand Up @@ -83,7 +69,8 @@ public static Map<String, String> fetchData(String ticker, LocalDate startDate)
return null;
}
}
public static void writeData(Map<String, String> data, String ticker) {

private static void writeData(Map<String, String> data, String ticker) {
String filename = StockTracker.PATH + ticker + "_temp.txt";
System.out.println(filename);
Map<String, String> map = new TreeMap<>(data);
Expand All @@ -104,5 +91,18 @@ public static void writeData(Map<String, String> data, String ticker) {
e.printStackTrace();
}
}

static LocalDate getMostRecentDay() {
AlphaVantageConnector apiConnector = new AlphaVantageConnector(API_KEY, TIMEOUT);
TimeSeries stockTimeSeries = new TimeSeries(apiConnector);
try {
List<StockData> temp = stockTimeSeries.daily("IVV").getStockData();
// last date is first in list
return temp.get(0).getDateTime().toLocalDate();
}
catch (AlphaVantageException e) {
return LocalDate.now();
}
}
}

38 changes: 28 additions & 10 deletions src/main/java/stocktracker/StockTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import java.util.List;

//TODO: Add javadoc comments
//TODO: Keep old save configurations and data in Excel table to use API less and boost speed
//TODO: Add .exe somehow
//TODO: migrate files to .csv format?
public class StockTracker {

public static final String VERSION = "0.X";
Expand All @@ -26,9 +25,7 @@ public class StockTracker {
}
}


public static void main(String[] args) {

runNewTest();
//runExistingTest();
}
Expand All @@ -53,18 +50,41 @@ private static void runNewTest()
//deleteTempFiles();
System.out.println("Files aggregated, money calculated");
System.out.println("Done");

}

private static void runExistingTest() {
updateSave();
}

/**
* Writes data of a specified stock and currency to text files.
* @param ticker Ticker of the stock to be recorded.
* @param currencyCode Currcency code of currency to be recorded.
* @param startDate First date the data is written from.
*/
public static void writeData(String ticker, String currencyCode, LocalDate startDate) {
StockInfoFetcher.getData(ticker, startDate);
CurrencyRateFetcher.writeCurrencyInfo(currencyCode, startDate);
}

/**
* Calculates the total value of a stock based on the amount owned.
* @param ticker_currency Ticker and currency of stock.
* @param stockAmounts Amount of stock owned.
*/
public static void calculateMoney(List<String> ticker_currency, List<Number> stockAmounts)
{
DataAggregator.calculateMoney(ticker_currency, stockAmounts);
}

/**
* Creates three text files. The first one saves the stock tickers and currency codes
* and stock amounts specified. The others act as a cache and keep fetched data
* as to not call the APIs too much and improve performance.
* @param nameList List containing stocks' tickers and currency codes in the form
* "TICKER_CURRENCYCODE".
* @param amountList List containing amounts of stocks specified in nameList owned.
*/
public static void createSave(ArrayList<String> nameList, ArrayList<Number> amountList) {
boolean append = false;
for (int i = 0; i < nameList.size(); i++) {
Expand All @@ -82,6 +102,9 @@ public static void createSave(ArrayList<String> nameList, ArrayList<Number> amou
}
}

/**
* Reads the save files and if data in them is outdated, updates them.
*/
public static void updateSave() {
List<String> saveConfig = FileManager.readLines(PATH + "save_config.txt");
List<String> saveData = FileManager.readLines(PATH + "save_data.txt");
Expand Down Expand Up @@ -115,11 +138,6 @@ public static void updateSave() {
}
}

public static void calculateMoney(List<String> ticker_currency, List<Number> stockAmounts)
{
DataAggregator.calculateMoney(ticker_currency, stockAmounts);
}

public static void deleteTempFiles() {
FileManager.deleteFiles("src\\main\\resources", "_temp");
}
Expand Down
Loading

0 comments on commit 213c141

Please sign in to comment.