/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package netukar.piggybank.ejb.session; import javax.ejb.Stateful; import netukar.piggybank.ejb.entity.Account; import netukar.piggybank.ejb.entity.Client; import netukar.piggybank.ejb.exception.DepositException; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; /** * * @author radovan */ @Stateful public class BankDeskBean implements BankDeskLocal { @PersistenceContext private EntityManager manager; private Client client; public void setClient(Client client) { this.client = client; } public double getBalance(Account account) { if (client == null) { throw new IllegalStateException("No client has been set"); } return account.getBalance(); } public void deposit(Account account, double amount) { if (client == null) { throw new IllegalStateException("No client has been set"); } if (amount <= 0) { throw new DepositException("Cannot deposit less or equal zero."); } account.setBalance(account.getBalance() + amount); manager.merge(client); } public void withdraw(Account account, double amount) { if (client == null) { throw new IllegalStateException("No client has been set"); } if (amount < 0) { throw new DepositException("Cannot withdraw less than zero."); } if (amount > account.getBalance()) { throw new DepositException("Cannot withdraw more than current leftover."); } account.setBalance(account.getBalance() - amount); manager.merge(client); } }