Skip to content

Commit

Permalink
Fix a bug that allowed negative Transaction amounts
Browse files Browse the repository at this point in the history
Now for every amount, the absolute value is taken. As a result, also
  the classes that relied on amount being negative to compute the
  balance are changed (now the balance is computed by calculating the
  sum of all the deposit transaction amounts minus the sum of all the
  withdrawal transaction amounts).
  • Loading branch information
Daan Kooij committed May 13, 2018
1 parent 6dc14f1 commit 564dd1c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
12 changes: 10 additions & 2 deletions sql_queries_and_updates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,19 @@ AND description LIKE ?
AND external_iban LIKE ?
AND type LIKE ?;

getBalanceOnDate(user_id, date):
getDepositsOnDate(user_id, date):
SELECT SUM(amount)
FROM Transaction_Table
WHERE user_id = ?
AND date <= ?;
AND date <= ?
AND type = 'deposit';

getWithdrawalsOnDate(user_id, date):
SELECT SUM(amount)
FROM Transaction_Table
WHERE user_id = ?
AND date <= ?
AND type = 'withdrawal';

getTransactionsAfterDate(user_id, date):
SELECT transaction_id, date, amount, description, external_iban, type
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/nl/utwente/ing/api/MainRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public ResponseEntity postTransaction(@RequestParam(value = "session_id", defaul
if (!t.getType().equals("deposit") && !t.getType().equals("withdrawal")) {
return ResponseEntity.status(405).body("Invalid input given (type should be 'deposit' or 'withdrawal')");
}
t.setAmount(Math.abs(t.getAmount()));
if (t.getDescription() == null) {
t.setDescription("");
}
Expand Down Expand Up @@ -196,6 +197,7 @@ public ResponseEntity putTransaction(@RequestParam(value = "session_id", default
!t.getType().equals("deposit") && !t.getType().equals("withdrawal")) {
return ResponseEntity.status(405).body("Invalid input given (type should be 'deposit' or 'withdrawal')");
}
t.setAmount(Math.abs(t.getAmount()));
try {
String sessionID = this.getSessionID(pSessionID, hSessionID);
long transactionIDLong = Long.parseLong(transactionID);
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/nl/utwente/ing/model/persistentmodel/CustomORM.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,18 @@ public class CustomORM {
"AND description LIKE ?\n" +
"AND external_iban LIKE ?\n" +
"AND type LIKE ?;";
public static final String GET_BALANCE_ON_DATE =
public static final String GET_DEPOSITS_ON_DATE =
"SELECT SUM(amount)\n" +
"FROM Transaction_Table\n" +
"WHERE user_id = ?\n" +
"AND date <= ?;";
"AND date <= ?\n" +
"AND type = 'deposit';";
public static final String GET_WITHDRAWALS_ON_DATE =
"SELECT SUM(amount)\n" +
"FROM Transaction_Table\n" +
"WHERE user_id = ?\n" +
"AND date <= ?\n" +
"AND type = 'withdrawal';";
public static final String GET_TRANSACTIONS_AFTER_DATE =
"SELECT transaction_id, date, amount, description, external_iban, type\n" +
"FROM Transaction_Table\n" +
Expand Down Expand Up @@ -800,12 +807,19 @@ public ArrayList<Long> getMatchingTransactionIDs(int userID, CategoryRule catego
public float getBalanceOnDate(int userID, String date) {
float balance = 0;
try {
PreparedStatement statement = connection.prepareStatement(GET_BALANCE_ON_DATE);
PreparedStatement statement = connection.prepareStatement(GET_DEPOSITS_ON_DATE);
statement.setInt(1, userID);
statement.setString(2, date);
ResultSet resultSet = statement.executeQuery();
resultSet.next();
balance = resultSet.getFloat(1);
balance += resultSet.getFloat(1);

statement = connection.prepareStatement(GET_WITHDRAWALS_ON_DATE);
statement.setInt(1, userID);
statement.setString(2, date);
resultSet = statement.executeQuery();
resultSet.next();
balance -= resultSet.getFloat(1);
} catch (SQLException e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,11 @@ public ArrayList<BalanceCandlestick> getBalanceHistory(String sessionID, Interva
BalanceCandlestick candlestick = new BalanceCandlestick(balance);
while (index < transactions.size() &&
!IntervalHelper.isSmallerThan(interval, transactions.get(index).getDate())) {
candlestick.mutation(transactions.get(index).getAmount());
if (transactions.get(index).getType().equals("deposit")) {
candlestick.mutation(transactions.get(index).getAmount());
} else {
candlestick.mutation(transactions.get(index).getAmount() * (-1));
}
balance = candlestick.getClose();
index++;
}
Expand Down

0 comments on commit 564dd1c

Please sign in to comment.