From 564dd1c62943618262aea85d8128de278bd7a8d7 Mon Sep 17 00:00:00 2001 From: Daan Kooij Date: Sun, 13 May 2018 15:36:49 +0200 Subject: [PATCH] Fix a bug that allowed negative Transaction amounts 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). --- sql_queries_and_updates.txt | 12 ++++++++-- .../utwente/ing/api/MainRestController.java | 2 ++ .../ing/model/persistentmodel/CustomORM.java | 22 +++++++++++++++---- .../persistentmodel/PersistentModel.java | 6 ++++- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/sql_queries_and_updates.txt b/sql_queries_and_updates.txt index b7af200..b864188 100644 --- a/sql_queries_and_updates.txt +++ b/sql_queries_and_updates.txt @@ -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 diff --git a/src/main/java/nl/utwente/ing/api/MainRestController.java b/src/main/java/nl/utwente/ing/api/MainRestController.java index cc41ef9..e25f46b 100644 --- a/src/main/java/nl/utwente/ing/api/MainRestController.java +++ b/src/main/java/nl/utwente/ing/api/MainRestController.java @@ -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(""); } @@ -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); diff --git a/src/main/java/nl/utwente/ing/model/persistentmodel/CustomORM.java b/src/main/java/nl/utwente/ing/model/persistentmodel/CustomORM.java index 35ca8d8..2045b2a 100644 --- a/src/main/java/nl/utwente/ing/model/persistentmodel/CustomORM.java +++ b/src/main/java/nl/utwente/ing/model/persistentmodel/CustomORM.java @@ -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" + @@ -800,12 +807,19 @@ public ArrayList 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(); } diff --git a/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java b/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java index 17ea505..a293bbe 100644 --- a/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java +++ b/src/main/java/nl/utwente/ing/model/persistentmodel/PersistentModel.java @@ -470,7 +470,11 @@ public ArrayList 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++; }