package examples.ejb.basic.containerManaged; import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.Remote; import javax.ejb.*; import java.util.*; /** * AccountBean is an EntityBean. This EJBean illustrates: *
AccountHome.create()
, the container (which in WebLogic EJB is
* also the home) allocates an instance of this EJBean and
* calls AccountBean.ejbCreate()
.
*
* For container-managed persistence, ejbCreate()
returns
* a void, unlike the case of bean-managed
* persistence, where it returns a primary key.
*
* @param accountID String Account ID
* @param initialBalance double Initial Balance
* @param type String Account type
* @exception javax.ejb.CreateException
* if there is a problem creating the bean
*/
public void ejbCreate(String accountId, double initialBalance, String type)
throws CreateException
{
if (VERBOSE)
System.out.println("AccountBean.ejbCreate( id = " + System.identityHashCode(this) + ", PK = " +
accountId + ", " + "initial balance = $ " + initialBalance +
", type = " + type + ")");
this.accountId = accountId;
this.balance = initialBalance;
this.type = type;
}
/**
* This method is required by the EJB Specification,
* but is not used by this example.
*
* @param accountID String Account Identification
* @param initialBalance double Initial Balance
* @param type String Account type
*/
public void ejbPostCreate(String accountId, double initialBalance, String type)
{
if (VERBOSE)
System.out.println("AccountBean.ejbPostCreate (" + id() + ")");
}
// Application defined methods
/**
* Adds amount to balance.
*
* @param amount double Amount
* @return double balance
*/
public double deposit(double amount)
{
if (VERBOSE)
System.out.println("AccountBean.deposit: Depositing $" + amount + " into '" + accountId + "'");
balance += amount;
return balance;
}
/**
* Subtracts amount from balance.
*
* @param amount double Amount
* @return double Balance
* @exception ProcessingErrorException
* if Amount > Balance
*/
public double withdraw(double amount)
throws ProcessingErrorException
{
if (VERBOSE)
System.out.println("AccountBean.withdraw: Withdrawing $" + amount + " from '" + accountId + "'");
if (amount > balance) {
throw new ProcessingErrorException("Request to withdraw $" + amount +
"; is more than balance $" + balance +
" in account " + accountId);
}
balance -= amount;
return balance;
}
/**
* Returns current balance.
*
* @return double Balance
*/
public double balance() {
if (VERBOSE)
System.out.println("AccountBean.balance (" + id() + ")");
return balance;
}
/**
* Returns account type.
*
* @return String Type
*/
public String type() {
return type;
}
}