//Code 1: Home Interface

import java.rmi.RemoteException;
import javax.ejb.*;

public interface AccountHome
    extends EJBHome
{
    public abstract Account create(String s, double d)
        throws CreateException, RemoteException;

    public abstract Account findByPrimaryKey(AccountPK accountpk)
        throws FinderException, RemoteException;
}

--------------


//Code 2a: Remote Interface
import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface Account
    extends EJBObject
{
    public abstract double deposit(double d)
        throws RemoteException;

    public abstract double withdraw(double d)
        throws RemoteException;

    public abstract double balance()
        throws RemoteException;

}

------------------------

//Code 2b: zu einem Entity Bean wird immer ein primary Key vereinbart
public class AccountPK implements java.io.Serializable {
  public String accountId;
}

---------------

//Code 3: Bean Implementierung
import java.rmi.RemoteException;
import javax.ejb.*;

public class AccountBean
    implements EntityBean
{

    // ejb-spezifische Methoden wie z.B.
    // ejbActivate(), ejbRemove, 
    // ejbLoad(), ejbStore()

    public AccountPK ejbFindByPrimaryKey(AccountPK accountpk)
        throws FinderException, RemoteException
    {}


    public AccountPK ejbCreate(String s, double d)
        throws CreateException
    {
      //erzeugen des EJBean...
    }

    public double deposit(double d)
    {
      //veraendern von balance...
balance += d;
      return balance;
    }

    public double withdraw(double d)
    {
//veraendern von balance...
balance -= d;
      return balance;
    }

    public double balance()
    {
        return balance;
    }

    public double balance;

}

-----------------------------

//Code 4: Client des EJBeans


public class Client {

public static void main(String[] args) {

    // Parse the argument list 

    double amount  = 100;
    double balance = 3000;
    AccountPK accountKey = new AccountPK();
    accountKey.accountId = accountId;

    try {
      // Contact container (the "AccountHome") through JNDI.
      Context ctx = getInitialContext();
AccountHome home = 
(AccountHome)ctx.lookup("containerManagedJDBC.AccountHome");

      // Find the Account or create it.
      Account ac = null;
      try {
        System.out.println("Looking up account " + accountId + "...\n");
        ac = (Account) home.findByPrimaryKey(accountKey);
      }
      catch (Exception ee) {
        System.out.println("Did not find " + accountId);
      }
      if (ac == null) {
        System.out.println("Account " + accountId + 
                      " being created; balance is $" + balance + "\n");
        ac = home.create(accountId, balance, ACCOUNT_TYPE);      
      }
      else {
        System.out.println("Account " + accountId +
                           " found; balance is $" + ac.balance() +
                           "; type: " + ac.type() + "\n");
      }


balance = ac.deposit(amount);

      amount = balance + 10;
   } catch (Exception e) {

  }
 


