@TransactionAttribute(TransactionAttributeType.REQUIRED) public long openAccount(String name, float initialBalance) throws NegativeBalanceException { checkForPositiveBalance(initialBalance); Account account = new Account(); account.setBalance(initialBalance); account.setName(name); entityManager.persist(account); return account.getId(); }
@TransactionAttribute(TransactionAttributeType.REQUIRED) public float deposit(long accountNumber, float amount) throws NegativeBalanceException, AccountNotFoundException { checkForPositiveBalance(amount); Account account = findAccount(accountNumber); float newBalance = account.getBalance() + amount; account.setBalance(newBalance); entityManager.merge(account); return newBalance; }
@TransactionAttribute(TransactionAttributeType.REQUIRED) public float withdraw(long accountNumber, float amount) throws NegativeBalanceException, InsufficientBalanceException, AccountNotFoundException { checkForPositiveBalance(amount); Account account = findAccount(accountNumber); if (account.getBalance() < amount) { throw new InsufficientBalanceException(); } float newBalance = account.getBalance() - amount; account.setBalance(newBalance); entityManager.merge(account); return newBalance; }