Skip to content

Commit

Permalink
Merge pull request #4 from agilitytestbed/features/balancehistory
Browse files Browse the repository at this point in the history
Features/balancehistory
  • Loading branch information
dkooij authored Jul 16, 2018
2 parents 0723359 + 1708880 commit 1dfe6dd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
14 changes: 11 additions & 3 deletions src/main/java/nl/utwente/ing/exception/APIException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package nl.utwente.ing.exception;

/**
* The APIException interface.
* Used as the parent of all APIExceptions.
* The APIException class.
* Extends Exception.
* Used as the superclass of all APIExceptions.
*
* @author Daan Kooij
*/
public interface APIException {
public class APIException extends Exception {

/**
* The empty constructor of APIException.
*/
public APIException() {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

/**
* The InvalidSessionIDException class.
* Extends Exception and implements APIException.
* Extends APIException.
* InvalidSessionIDException is thrown whenever a sessionID is missing or invalid.
*
* @author Daan Kooij
*/
public class InvalidSessionIDException extends Exception implements APIException {
public class InvalidSessionIDException extends APIException {

/**
* The empty constructor of InvalidSessionIDException.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

/**
* The ResourceNotFoundException class.
* Extends Exception and implements APIException.
* Extends APIException.
* ResourceNotFoundException is thrown whenever a resource is not found.
*
* @author Daan Kooij
*/
public class ResourceNotFoundException extends Exception implements APIException {
public class ResourceNotFoundException extends APIException {

/**
* The empty constructor of ResourceNotFoundException.
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/nl/utwente/ing/misc/date/IntervalHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.utwente.ing.misc.date;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
* The IntervalHelper class.
Expand All @@ -23,11 +24,11 @@ public class IntervalHelper {
*
* @param intervalPeriod The size of the intervals to be generated.
* @param amount The amount of intervals to be generated.
* @return An array containing (amount
* @return An array containing LocalDateTime objects representing the requested intervals.
*/
public static LocalDateTime[] getIntervals(IntervalPeriod intervalPeriod, int amount) {
LocalDateTime[] intervals = new LocalDateTime[amount + 1];
intervals[amount] = LocalDateTime.now();
intervals[amount] = LocalDateTime.now(ZoneOffset.UTC);

if (intervalPeriod == IntervalPeriod.YEAR) {
for (int i = amount - 1; i >= 0; i--) {
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/nl/utwente/ing/model/bean/BalanceCandlestick.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
public class BalanceCandlestick {

private float open, close, high, low, volume;
private long timestamp;

/**
* The constructor of BalanceCandlestick.
* Initially, only the opening balance is specified, after which this balance can be modified by using the mutate
* method. Along the way, this bean keeps track of the opening balance, the closing balance, the highest balance,
* the lowest balance and the volume of all the mutations combined.
* Initially, only the opening balance and the opening timestamp are specified, after which this balance can be
* modified by using the mutate method. Along the way, this bean keeps track of the opening balance, the closing
* balance, the highest balance, the lowest balance and the volume of all the mutations combined.
*
* @param open The opening balance of this BalanceCandlestick.
*/
public BalanceCandlestick(float open) {
public BalanceCandlestick(float open, long timestamp) {
this.open = open;
this.close = open;
this.high = open;
this.low = open;
this.volume = 0;
this.timestamp = timestamp;
}

/**
Expand Down Expand Up @@ -71,6 +73,16 @@ public float getVolume() {
return volume;
}

/**
* Method used to retrieve the unix (seconds) timestamp indicating the beginning of the period of the
* BalanceCandlestick.
*
* @return The unix (seconds) timestamp indicating the beginning of the period of the BalanceCandlestick.
*/
public long getTimestamp() {
return timestamp;
}

/**
* Method used to indicate a mutation of the balance of BalanceCandlestick.
* Along the way, this method updates the closing balance, the highest balance, the lowest balance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.UUID;

Expand Down Expand Up @@ -466,10 +467,12 @@ public ArrayList<BalanceCandlestick> getBalanceHistory(String sessionID, Interva
ArrayList<BalanceCandlestick> candlesticks = new ArrayList<>();
int index = 0;
for (int i = 1; i <= amount; i++) {
LocalDateTime interval = intervals[i];
BalanceCandlestick candlestick = new BalanceCandlestick(balance);
LocalDateTime startInterval = intervals[i - 1];
LocalDateTime endInterval = intervals[i];
long startUnixTime = startInterval.toEpochSecond(ZoneOffset.UTC); // Convert start of interval to UNIX time
BalanceCandlestick candlestick = new BalanceCandlestick(balance, startUnixTime);
while (index < transactions.size() &&
!IntervalHelper.isSmallerThan(interval, transactions.get(index).getDate())) {
!IntervalHelper.isSmallerThan(endInterval, transactions.get(index).getDate())) {
if (transactions.get(index).getType().equals("deposit")) {
candlestick.mutation(transactions.get(index).getAmount());
} else {
Expand Down

0 comments on commit 1dfe6dd

Please sign in to comment.