#include <fstream.h>
#include "account.h"


class Account_impl : virtual public _sk_Account
{
protected:
  CORBA::Long _current_balance;

public:
  Account_impl() 
  {
    _current_balance = 0;
    cout << "Initial balance = 0" << endl;
  };
  void deposit( CORBA::ULong amount )
  {
    _current_balance += amount;
    cout << "Deposit " << amount << ", new balance = " << _current_balance 
	 << endl; 
  };
  void withdraw( CORBA::ULong amount )
  {
    _current_balance -= amount;
    cout << "Withdraw " << amount << ", new balance = " << _current_balance 
	 << endl; 
  };
  CORBA::Long balance()
  {
    return _current_balance;
  };
};


class SavingsAccount_impl : virtual public _sk_SavingsAccount,
			    virtual public Account_impl
{
public:
  SavingsAccount_impl() : Account_impl() {}
  void creditInterest() 
  {
    CORBA::ULong interest = (long)(_current_balance * 0.05);
    _current_balance += interest;
    cout << "Interest credit " << interest
         << ", new balance = " << _current_balance 
	 << endl; 

  }
};



int main( int argc, char *argv[] )
{
  assert(argc == 2);

  // ORB initialization
  CORBA::ORB_var orb = CORBA::ORB_init(argc, argv, "omniORB2");
  CORBA::BOA_var boa = orb->BOA_init(argc, argv, "omniORB2_BOA");

  // server side
  SavingsAccount_impl* server = new SavingsAccount_impl;
  server->_obj_is_ready(boa);

  CORBA::String_var ref = orb->object_to_string( server );
  
  cout << "Writing the account object reference to file " 
       << argv[1] << endl;
  ofstream ostr(argv[1]);
  if (!ostr.is_open()) {
    cerr << "error: couldn't create file" << argv[1] << endl;
    exit(1);
  }

  ostr << ref.in() << endl;

  ostr.close();

  boa->impl_is_ready ();

  return 0;
}

