unit uCandidatePaperManager;

interface
  uses SysUtils, uCandidate, uResearchPaper;
//Q4.1 - 1
//class header correct
type TCandidatePaperManager = class
  private
    //Q4.2 - 2
    //private array of candidate objects
    //size set to 20
    cArr : array[1..20] of TCandidate;
    //Question 7
    //Q7.1 - 2
    //array of ResearchPaper object created with correct size
    rpArr : array[1..20] of TResearchPaper;
    //rpSize created and intialized
    rpSize : integer;
  public
    constructor Create;
    function displayAllCandidates : string;
    procedure sortByYear();
    function  reviewResearchPapers() : string;
    function findCandidate(sid : string) : TCandidate;
    function doctoralCandidates() : string;
    
end;

implementation

{ TCandidatePaperManager }
//Q4.3 - 9
//constructor header correct
constructor TCandidatePaperManager.Create;
var
  infile : textfile;
  line, studentID, name : string;
  year , i: integer;

begin
  if FileExists('candidates.txt') <> true then
    begin
      WriteLn('File Missing');    //error message
    end
  else
    begin
      //open file for reading
      AssignFile(infile, 'candidates.txt');
      Reset(infile);

      //loop through all 20 lines
      for i := 1 to length(cArr) do
      begin
        //read the line
        ReadLn(infile,line);
        //split the line into the required parts
        studentID := Copy(line, 1, Pos('#', line) - 1);
        Delete(line, 1, Pos('#', line));
        name := Copy(line, 1, Pos('#', line) - 1);
        Delete(line, 1, Pos('#', line));
        year := StrToInt(line);
        //create Candidate object
        //add to array
        //correct index
        cArr[i] :=  TCandidate.Create(studentID , name, year);
      end;



    end;


end;

//Q4.4 - 5
//header correct
function TCandidatePaperManager.displayAllCandidates: string;
var
  i : integer;
  output : string;
begin
  output := '';
  //loop through array
  for i := 1 to length(cArr) do
    begin
      //append to string
      //extra blank line
      output := output + cArr[i].toString + #13#10;
    end;

  Result := output; //appended string returned
end;

//Q4.5 - 8
//Header correct

procedure TCandidatePaperManager.sortByYear;
var
  i,j : integer;
  temp : TCandidate;
begin
  //loop through the array
   for i := 1 to length(cArr)-1 do
     begin
      //nested loop
      //correct for sort used
       for j := i+1 to length(cArr) do
         begin
          //compare the object years
          //correct objects referenced for sort
          //correct use of method
           if cArr[i].getYear() <   cArr[j].getYear() then
           begin
            //swopping elements
             temp := cArr[i];
             cArr[i] := cArr[j];
             cArr[j] := temp;
           end;

         end;
     end;
end;


//Q7.2 - 9
//header
function TCandidatePaperManager.reviewResearchPapers: string;
var
  accepted , rejected : string;
  inFile : textfile;
  line, title, department, reqCode, studentID : string;
  meetsDeptRequirements : boolean;
  year , i: integer;
  rp : TResearchPaper;
begin
    //string to store the accepted and rejected
  accepted := 'Accepted' + #13#10;
  rejected := 'Rejected' + #13#10;

  if FileExists('researchpapers.txt') <> true then
    begin
      WriteLn('File missing');
    end
  else
    begin
      AssignFile(inFile, 'researchpapers.txt');       //open file
      Reset(inFile);

      rpSize := 0;
      //read lines
      while NOT EOF(inFile) do
      begin

        //split tokens
        ReadLn(infile, line);


        title := Copy(line, 1, Pos('#', line) - 1);
        Delete(line, 1, Pos('#', line));

        department:= Copy(line, 1, Pos('#', line) - 1);
        Delete(line, 1, Pos('#', line));

        meetsDeptRequirements:= StrToBool(Copy(line, 1, Pos('#', line) - 1));
        Delete(line, 1, Pos('#', line));

        reqCode := Copy(line, 1, Pos('#', line) - 1);
        Delete(line, 1, Pos('#', line));

        studentID:= line;
        //Create researchPaper object
        rp := TResearchPaper.Create(title,department,meetsDeptRequirements,reqCode,studentID);
        //check if it meets requirements
        //must call method
        if rp.getMeetsDeptRequirements() then
          begin
            //add to array
            //increase rpsize
            inc(rpSize);
            rpArr[rpSize] := rp;
            accepted := accepted + rp.toString ;
          end
        else
          begin
            rejected := rejected + rp.toString ;
          end;


      end;

      //return string with accepted and reject lists
      Result:= accepted + #13#10 + rejected;

    end;


end;



 //Q8.1 - 17
 //header
function TCandidatePaperManager.doctoralCandidates: string;
var
  dept, currentDept, name , title , list: string;
  i : integer;
  f:TextFile;
begin



   //make date formatter
   //format date
   list := (FormatDateTime('YYYY/MM/dd HH:mm:ss', Now))  + #13#10;

   dept := 'MatSci#BusCom#HumArt#LawMed#';
   //separating departments
   //getting the list of department doctoral candidates

   currentDept := Copy(dept, 1, Pos('#', dept) - 1);
   Delete(dept, 1, Pos('#', dept));

   while Length(currentDept) <> 0 do
   begin
     list:= list + currentDept +  #13#10;
      //loop through all research papers
     for i := 1 to rpSize do
      begin
           //checking per department
         if CompareText(rpArr[i].getDepartment,currentDept) = 0 then
         begin
              //find the research paper's author's name.
              name := findCandidate(rpArr[i].getStudentID()).getName();

              title := rpArr[i].getTitle();
              //add to department list
              list := list + name;
              //check if on dean's list
              if rpArr[i].onDeansList then
              begin
                 //add (D)
                list := list + ' (D)' + #13#10;
              end
              else
              begin
                list := list + #13#10;
              end;

              //add to concatenated list
              //add extra line
              list := list + title +  #13#10  +  #13#10;

         end
      end;

      currentDept := Copy(dept, 1, Pos('#', dept) - 1);
      Delete(dept, 1, Pos('#', dept));
   end;


   //Create file to write to with correct filename
   AssignFile(f, 'doctors.txt');
   ReWrite(f);

   //save to file and close
   Writeln(f,list);
   CloseFile(f);


   Result:= list;

end;

function TCandidatePaperManager.findCandidate(sid:string): TCandidate;
var
  c : TCandidate;
  i : integer;

begin
  c := nil;
  //loop through candidates
  for i := 1 to Length(cArr) do
    begin
    //check for matching studentid
      if CompareText(cArr[i].getStudentID , sid) = 0 then
      begin
        c := cArr[i];
      end;

    end;

  Result := c;
end;







end.
