BankAccountImpl
Home ] Up ] BankAccount ] [ BankAccountImpl ] BankAccountServer ] BankAccountClient ] Running the Example ]

 

 

Here's the BankAccountImpl class, which implements the BankAccount interface, and thus constitutes the remote object:

package rmiServer;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import rmi.BankAccount;

/**
 * A BankAccount RMI remote object class.
 *
 * @author Bryan Higgs
 * @version 1.0
 */
public class BankAccountImpl 
    extends UnicastRemoteObject
    implements BankAccount
{
  /**
   * Constructor
   * @param id the unique account id
   * @param holder the name of the account holder
   * @param initialDeposit the amount of the initial deposit.
   */
  public BankAccountImpl(int id, String holder, double initialDeposit) 
    throws RemoteException
  {
    m_id = id;
    m_holder = holder;
    m_balance = initialDeposit;
  }
  
  /**
   * Returns the unique account id
   * @return the unique account id
   */
  public int getAccountId() throws RemoteException
  {
    return m_id;
  }
  
  /**
   * Returns the name of the bank account holder.
   * @return the name of the holder of this bank account
   */
  public String getHolder() throws RemoteException
  {
    return m_holder;
  }
  
  /**
   * Deposit an amount
   * @param amount the amount to deposit
   * @return the balance after the deposit completed.
   */
  public double deposit(double amount) throws RemoteException
  {
    m_balance += amount;
    return m_balance;
  }

  /**
   * Withdraw an amount
   * @param amount the amount to withdraw
   * @return the balance after the withdrawal completed.
   */
  public double withdraw(double amount) throws RemoteException
  {
    m_balance -= amount;
    return m_balance;
  }

  /**
   * Gets the current balance
   * @return the current balance in the account
   */
  public double getBalance() throws RemoteException
  {
    return m_balance;
  }
  

  /////// Private data //////
  private int     m_id; 
  private String  m_holder;
  private double  m_balance;
}

Note that:

  • It extends from java.rmi.server.UnicastRemoteObject .  This causes the class to be exported automatically (done by the constructor for UnicastRemoteObject, which is automatically called from the BankAccountImpl constructor)
  • It has a constructor which allows us to construct an instance with a specified account ID, holder name, and initial balance.
 
The page was last updated February 19, 2008