#include <string>
#include "person.h"

class Customer : public Person {
 private:
  int amountOfBusiness;
  string custID;
  static const char sep=':';

 public:

  int AmountOfBusiness() { return amountOfBusiness; }
  string CustID() { return custID; }
  void SetAOB(int v) { amountOfBusiness= v;}

  bool betterThan(const Customer& other)
    { return amountOfBusiness > other.amountOfBusiness; }

  friend bool operator < (const Customer&, const Customer&);

  friend istream& operator>>(istream&, Customer&);
  friend ostream& operator<<(ostream&, const Customer&);
  friend ifstream& operator>>(ifstream&, Customer&);
  friend ofstream& operator<<(ofstream&, const Customer&);

};

bool operator < (const Customer& c1, const Customer& c2)
{ return c1.amountOfBusiness < c2.amountOfBusiness; }

istream& operator >> (istream& i, Customer& c)
{
  i >> (Person&) c;
  
  if (i) {
    cout << "Business: ";
    i >> c.amountOfBusiness;
    cout << "ID: ";
    i >> c.custID;
  }

  return i; // most common mistake: forgetting to return the stream
}

ostream& operator << (ostream& o, const Customer& c)
{
  o << (Person&) c;

  o << "  Business: " << c.amountOfBusiness << ", ID: " << c.custID << endl;

  return o;  // most common mistake: forgetting to return the stream
}

ifstream& operator >> (ifstream& i, Customer& c)
{
  string line;
  parseCont fields;

  i >> (Person&) c;

  if (i) {
    getline(i,line);
    if (parse(fields,Customer::sep,line) == 2) {
      c.amountOfBusiness = atoi(fields[0].c_str());
      c.custID = fields[1];
    }
    else {
      cerr << "Bad format for Customer." << endl;
      exit(1);
    }
  }
  return i;
}

ofstream& operator << (ofstream& o, const Customer& c)
{
  o << (Person&) c;

  o << c.amountOfBusiness << Customer::sep << c.custID << endl;

  return o;
}

