/*
 * Java Network Programming, Second Edition
 * Merlin Hughes, Michael Shoffner, Derek Hamner
 * Manning Publications Company; ISBN 188477749X
 *
 * http://nitric.com/jnp/
 *
 * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner;
 * all rights reserved; see license.txt for details.
 */

import Bank.*;

import org.omg.CORBA.*;
import org.omg.CosNaming.*;

public class AccountImpl extends _AccountImplBase implements Bank.Account {
  protected String name;
  protected int ssn;
  protected float balance;

  public AccountImpl (String name, int ssn) {
    this.name = name;
    this.ssn = ssn;
    balance = 100;
  }

  public synchronized void withdraw (float amount) throws Bank.InsufficientFunds {
    if (balance < amount)
      throw new Bank.InsufficientFunds (balance);
    balance -= amount;
  }

  public synchronized void deposit (float amount) {
    balance += amount;
  }

  public String name () {
    return name;
  }

  public void name (String name) {
    this.name = name;
  }

  public int ssn () {
    return ssn;
  }

  public float balance () {
    return balance;
  }

  public static void main (String[] args) throws UserException, InterruptedException {
    ORB orb = ORB.init (args, null);

    AccountImpl jimBean = new AccountImpl ("Jim Bean", 123456789);
    orb.connect (jimBean);
    
    org.omg.CORBA.Object nameService_ =
      orb.resolve_initial_references ("NameService");
    NamingContext nameService = NamingContextHelper.narrow (nameService_);

    NameComponent name = new NameComponent (jimBean.name (), "Account");
    NameComponent[] path = { name };
    nameService.rebind (path, jimBean);

    Thread.currentThread ().join ();
  }
}
