//--------------------------------------------------------------------
// IMPLEMENTATION FILE: myacct.cpp
//
// Implements: 1. class myBankAccount
//             2. overloaded == operator function
//             3. overloaded << operator function
//--------------------------------------------------------------------
#include "ourstuff.h"
#include "ourstr.h"

myBankAccount::myBankAccount(string initName,
                             string initPIN,
                             double initBalance)
{ // The initializer constructor called with 3 arguments
  accountName = initName;
  accountName.toUpper();
  accountPIN = initPIN;
  accountBalance = initBalance;
}

void myBankAccount::deposit(double amount)
{ // This allows negative deposits. Modify myBankAccount::deposit
  // so negative amounts have no affect on the balance.
  accountBalance = accountBalance + amount;
}

void myBankAccount::withdraw(double amount)
{ // This allows overdrafts and negative withdrawals. Modify
  // myBankAccount::withdraw so negative amounts have no affect
  // on accountBalance. Also make sure withdrawals greater than
  // accountBalance are prevented.
  accountBalance = accountBalance - amount;
}

string myBankAccount::name()
{                         
  return accountName;
}

string myBankAccount::PIN()
{
  return accountPIN;
}

double myBankAccount::balance()
{
  return accountBalance;
}

int operator == (myBankAccount left, myBankAccount right)
{ // Two myBankAccounts are equal if both the name and pin are equal. This
  // nested logic (rather than &&) is meant to avoid portability problems
  // with ome SUN version and to force short circuit Boolean evaluation.
  if(left.name() == right.name())
  {
    if(left.PIN()  == right.PIN() )
      return 1;
    else
      return 0;
  }
  else
    return 0;
}

// Auxilliary function for cout <<
ostream & operator << (ostream & os, myBankAccount acct)
// Auxilliary function for inserting output myBankAccount objects
// in to streams like this:    cout << anAcct;
{
  long oldFlags = decimals(os, 2);
  // Alter the output stream to show 0.00
  os << "{ myBankAccount: "
     << acct.name() << ", "
     << acct.PIN() << ", "
     << acct.balance()
     << " }";
  // Return output stream to the original state
  os.flags(oldFlags);
  return os;
}
