// event.cc   A simple event class for use with the Baseball database
// P. Conrad, for CISC220, 06J

#include "event.h"

#include <iostream>
// no using namespace std; here; instead, explicitly use std::

#include <cassert> // for assert() statements.

#include "date.h"

// Assumptions regarding Event_C objects:
//  city and country are never null
//  state can be.

// Note: we must use the date(d) syntax here.
// We can't use date = d; without a default constructor for Date_C
//   which we have not implemented (on purpose).

Event_C::Event_C(const Date_C d,
	  const char * const ctry, 
	  const char * const st, 
	  const char * const cit)
  : date(d) 
{
  assert(ctry!=NULL);
  country = new char [strlen(ctry)+1]; strcpy(country,ctry);
  if (st == NULL)
    state = NULL;
  else
    {
      state   = new char [strlen(st)  +1]; strcpy(state  ,st  );
    }
  assert(cit!=NULL);
  city    = new char [strlen(cit )+1]; strcpy(city   ,cit );
}


// Don't repeat the initialization inside the .cc file
// It only goes in the class declaration in the .h file

void Event_C::print(std::ostream & out ) const
{
  date.print(out); out << " in " << city;
  if (state != NULL)
    {
      out << ", " << state;
    }
  out << ", " << country;
}


// Must use date(orig.date) syntax since no default constructor for Date_C

Event_C::Event_C(const Event_C &orig)
  :  date(orig.date)   
{
  assert(orig.country!=NULL);
  country = new char [strlen(orig.country)+1]; strcpy(country,orig.country);
  if (orig.state == NULL)
    state = NULL;
  else
    {
      state   = new char [strlen(orig.state)  +1]; strcpy(state  ,orig.state  );
    }
  assert(orig.city!=NULL);
  city    = new char [strlen(orig.city )+1]; strcpy(city   ,orig.city );
  
  
}

Event_C & Event_C::operator =(const Event_C &right)
{
  if (this == &right)
    return (*this);

  delete [] country;
  if (state != NULL) // not strictly necessary
    delete [] state; 
  delete [] city;

 
  date = right.date;

  assert(right.country!=NULL);
  country = new char [strlen(right.country)+1]; strcpy(country,right.country);
  if (right.state == NULL)
    state = NULL;
  else
    {
      state   = new char [strlen(right.state)  +1]; strcpy(state  ,right.state  );
    }
  assert(right.city!=NULL);
  city    = new char [strlen(right.city )+1]; strcpy(city   ,right.city );
   
  return (*this);

}
  

Event_C::~Event_C()
{
  delete [] country;
  if (state != NULL) // not strictly necessary
    delete [] state; 
  delete [] city;
}




