public ChangesStatus transferMoney( int account_id_from, int account_id_to, int customer_id_by, int amount) throws BelowMinimumBalanceException { Connection connection = DBConnectionHelper.getConnection(); try { Account account_1 = new Account(); account_1 = accountDAO.getObject(connection, account_id_from); double newAmount_1 = account_1.getAmount() - amount; if (newAmount_1 < BusinessRules.SAVING_ACCOUNT_MIN_BALANCE_AMOUNT) { throw new BelowMinimumBalanceException("Below Minimum balance exceed"); } account_1.setAmount(newAmount_1); accountDAO.save(connection, account_1); Account account_2 = new Account(); account_2 = accountDAO.getObject(connection, account_id_to); double newAmount_2 = account_2.getAmount() + amount; account_2.setAmount(newAmount_2); accountDAO.save(connection, account_2); Transaction transaction = new Transaction(); transaction.setCustomer_id_by(customer_id_by); transaction.setAccount_id(account_id_from); transaction.setAccount_id_to(account_id_to); transaction.setTransaction_amount(amount); transaction.setTransaction_type(3); Date date = new Date(); transaction.setTransaction_time(new Timestamp(date.getTime())); transactionDAO.create(connection, transaction); // If exception found roll back in catch block connection.commit(); return new ChangesStatus(true, "Successfully Transfered " + amount); } catch (NotFoundException e) { e.printStackTrace(); } catch (SQLException e) { try { e.printStackTrace(); connection.rollback(); return new ChangesStatus(false, "Rolledbacked. Transaction Unsuccessful."); } catch (SQLException e1) { e1.printStackTrace(); return new ChangesStatus(false, "Unsuccessful Rolledback."); } } finally { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return null; }
public ChangesStatus withdrawMoney(int account_id, int customer_id_by, int amount, int pin) throws BelowMinimumBalanceException { Connection connection = DBConnectionHelper.getConnection(); try { Account account = accountDAO.getObject(connection, account_id); // Checking Security Pin if (!(account.getPin() == pin)) { return new ChangesStatus(false, "Security Pin do not match! Transaction Canceled."); } // Checking Permission if (!(PermissionHelper.isThisAccountOwnByThisCustomer(account_id, customer_id_by))) { return new ChangesStatus(false, "You do not own this account. Transaction Canceled."); } double newAmount = account.getAmount() - amount; if (newAmount < BusinessRules.SAVING_ACCOUNT_MIN_BALANCE_AMOUNT) { throw new BelowMinimumBalanceException("Below Minimum Balance to be left"); } account.setAmount(newAmount); accountDAO.save(connection, account); Transaction transaction = new Transaction(); transaction.setCustomer_id_by(customer_id_by); transaction.setAccount_id(account_id); transaction.setTransaction_amount(amount); transaction.setTransaction_type(2); Date date = new Date(); transaction.setTransaction_time(new Timestamp(date.getTime())); transactionDAO.create(connection, transaction); // If exception found roll back in catch block connection.commit(); return new ChangesStatus(true, "Successfully Withdrawed" + amount); } catch (NotFoundException e) { e.printStackTrace(); } catch (SQLException e) { try { connection.rollback(); e.printStackTrace(); return new ChangesStatus(false, "Transaction Rolledbacked. Unsuccessful."); } catch (SQLException e1) { e1.printStackTrace(); return new ChangesStatus(false, "Unsuccessful Rolledback."); } } finally { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } return null; }