
import java.rmi.*;
import java.rmi.server.*;

public class BankAccountImpl
        extends UnicastRemoteObject
        implements BankAccount
{
  private float value;
    private BankAccountMonitor monitor;

  public BankAccountImpl() throws RemoteException {
    super();
    value = 0;
    monitor = null;
  }

  public void deposit (float amount) throws RemoteException {
    value += amount;
    if (balance()<100){
        if (monitor != null)
	    monitor.lowBalance(balance());
    }
  }

  public void withdraw (float amount) 
    throws OverdrawnException, RemoteException {
    if (value < amount)
      throw new OverdrawnException();
    value -= amount;
    if (balance()<100){
        if (monitor != null)
	    monitor.lowBalance(balance());
    }
  }

  public float balance() throws RemoteException {
    return value;
  }
  
    public void addMonitor(BankAccountMonitor monitor)throws RemoteException{
	this.monitor = monitor;

    }

  public static void main(String[] args){
    // Create and install a security manager
    // System.setSecurityManager(new RMISecurityManager());
    try {
      BankAccount acct = new BankAccountImpl();
      String url = "rmi://localhost:1972/account1";
      // bind url to remote object
      Naming.bind(url, acct);
    } catch (Exception e){
      System.out.println("Account creation/binding error: " + e);
    }
  }

}


