/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.octal.supinbank.service;
import com.google.common.base.Preconditions;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import net.octal.supinbank.dao.AccountDao;
import net.octal.supinbank.entity.Account;
import net.octal.supinbank.entity.Person;
/**
*
* @author octal
*/
@Stateless
public class AccountService {
@EJB
private AccountDao accountDao;
public Account findAccountById(Long id) {
return accountDao.findAccountById(id);
}
public List<Account> getAccountsByCustomer(Person customer) {
return accountDao.getAccountsByCustomer(customer);
}
public Account withDrawAccount(Long accountId, Double amount) {
Account account = accountDao.findAccountById(accountId);
if (account != null) {
Preconditions.checkArgument(account.getTotalAmount() >= amount);
account.setTotalAmount(account.getTotalAmount() - amount);
}
return account;
}
public Account creditAccount(Long accountId, Double amount) {
Account account = accountDao.findAccountById(accountId);
if (account != null) {
account.setTotalAmount(account.getTotalAmount() + amount);
}
return account;
}
public void withDrawAndCredit(Long fromAccountId, Long toAccountId, Double amount) {
withDrawAccount(fromAccountId, amount);
creditAccount(toAccountId, amount);
}
}