/***************************************
 ** Implementation for functions on the
 ** the class Committee
 ***************************************/
#include "Committee.h"
#include "Date.h"

void read(Committee &C, istream &IN)
{
  // Read the committee name
  char c;
  do {
    c = IN.get();
  } while (c == ' ' || c == '\t' || c == '\n');

  string s = "";
  do {
    s += c;
    c = IN.get();
  } while(c != '\n');
  C.name = s;

  // Read chair
  read(C.chair,IN);

  // Read rest of committee
  for(int i = 0; i < 4; i++)
    read(C.mems[i],IN);
}

void write(Committee C, ostream &OUT)
{
  OUT << C.name << endl;
  write(C.chair,OUT);
  OUT<< endl;
  for(int i = 0; i < 4; i++)
  { 
    write(C.mems[i],OUT);
    OUT << endl;
  }
}

void makechair(Committee &C, Member M)
{
  int i = find(C.mems,4,M);
  if (i < 4)
  {
    C.mems[i] = C.chair;
    C.chair = M;
  }
}

int find(Member *A, int n, Member M)
{
  int i = 0;
  while (i < n && (M.first != A[i].first || M.last != A[i].last))
    i++;
  return i;
}

void writeminutes(Committee C, ostream &OUT)
{
  // Read date
  Date D;
  read(D,cin);

  // Write date to file
  write(D,OUT);
  OUT << endl << endl;

  // Committee name and chair
  OUT << "Minutes of the " << C.name << endl;
  write(C.chair,OUT);
  OUT << ", Committee Chair" << endl << endl;

  // Write rest of committee in alphabetical order
  OUT << "Committee Members:" << endl;
  selectionsort(C.mems,4);
  for(int i = 0; i < 4; i++)
  {
    write(C.mems[i],OUT);
    OUT << endl;
  }
  OUT << endl;

  // Copy minutes to file
  OUT << "Minutes:" << endl;
  string s;
  while(cin >> s && s != "endminutes")
    OUT << s << ' ';
  OUT << endl;
}