#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include "students.h"
#include "grades.h"
#include "gradedThings.h"

// PRE: filename must be a string which is a file name
//      students is pointer to an instance of studentList (must exist)
//      gradedThings is a point to an instance of gradedThingList (must exist)
// POST: fills in the two lists with students and gradedThings from the file
void readInClass(char* filename, studentList* students,
		 gradedThingList* gradedThings)
{
  ifstream in;
  int numGradedThings, i;
  
  in.open(filename);
  
  if (!in) 
  {
    cerr << "Can not open " << filename << endl;
    exit(2);
  }
  
  // first line in the file is the number of gradedThings
  in >> numGradedThings;
  in.ignore(200,'\n'); // flush to next line.

  for (i=0; i<numGradedThings; i++)
    gradedThings->addGradedThing(in);
    
  while (in)
    students->addStudent(in,gradedThings);

  in.close();
}

// PRE: filename must be a string which is a file name
//      students is pointer to an instance of studentList (must exist)
//      gradedThings is a point to an instance of gradedThingList (must exist)
// POST: writes to file the current contents of the two lists.  Does not
//       change them.
void writeOutClass(char* filename, studentList* students,
		   gradedThingList* gradedThings)
{
  ofstream out;
  gradedThing* gcur;
  student* scur;

  out.open(filename);

  if (!out)
  {
    cerr << "Can not open " << filename << endl;
    exit(20);
  }

  out << gradedThings->getSize();

  gradedThings->reset();
  for (gcur = gradedThings->currentGradedThing(); gcur; gcur = gradedThings->nextGradedThing())
  {
    out << endl << setw(20) << gcur->getName();
    out << setw(4) << gcur->getShortName();
    out << " " << gcur->getWeight();
  }
  students->reset();
  for (scur = students->currentStudent(); scur; scur = students->nextStudent())
  {
    out << endl << setw(20) << scur->getFirstName();
    out << setw(20) << scur->getLastName();
    out << setw(11) << scur->getSSNo();
    for (scur->resetGradeList();
	 scur->currentGrade();
	 scur->nextGrade())
	out << setw(4) << scur->currentGrade()->getScore();
  }
  out.close();
}

