// Martian Adam Smyth
// CS510 Assignment 1
// Student Set test program

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#include "set.h"
#include "person.h"
#include "student.h"


class LinkStu : public Student, public LinkElement
{
  public:
    LinkStu() {};
    LinkStu(const char *n, const char *ssn, long bd, float g, int c, int h) : Student(n,ssn,bd,g,c,h), LinkElement(ssn,0) {};
    virtual LinkElement *makecopy(void);
    void print() {Student::print();}
};


LinkElement *LinkStu::makecopy()
{
  LinkStu *dest = new LinkStu;

  dest->setname(getname());
  dest->setssn(getssn());
  dest->setbd(getbd());

  dest->setgpa(getgpa());
  dest->setgrade(getgrade());
  dest->sethours(gethours());

  dest->linkto(next());
  dest->setkey(getkey());

  return dest;
}

int main (void)
{
  LinkStu *S;
  SetClass A, B, *C;
  char choice=0, junk=0, name[256], ssn[256];
  long bd, hrs, grd;
  float gpa;

  cout << " Student Sets - Example Implementation of SetClass\n";
  cout << "---------------------------------------------------\n";
  cout << " 1) Add a student to set A\n";
  cout << " 2) Add a student to set B\n";
  cout << " 3) Delete a student from set A\n";
  cout << " 4) Delete a student from set B\n";
  cout << " 5) Check membership in set A\n";
  cout << " 6) Check membership in set B\n";
  cout << " 7) Print set A\n";
  cout << " 8) Print set B\n";
  cout << " 9) Print the union of A and B\n";
  cout << " 0) Print the intersection of A and B\n";
  cout << " Q) Exit this program\n";

  while(1)
  {
    cout << "\n > ";

    cin.get(choice);
    if(choice!='\n')
      while(junk!='\n')
        cin.get(junk);
    junk=0;

    switch (toupper(choice))
    {
      case '\n':
        cout << " 1) Add a student to set A\n";
        cout << " 2) Add a student to set B\n";
        cout << " 3) Delete a student from set A\n";
        cout << " 4) Delete a student from set B\n";
        cout << " 5) Check membership in set A\n";
        cout << " 6) Check membership in set B\n";
        cout << " 7) Print set A\n";
        cout << " 8) Print set B\n";
        cout << " 9) Print the union of A and B\n";
        cout << " 0) Print the intersection of A and B\n";
        cout << " Q) Exit this program\n";
        break;
      case 'Q':
        cout << "Exiting...\n";
        exit(1);
      case '1':
        // Add to set A
        cout << "Add a student to set A\n";
        cout << "Name: ";
        cin.getline(name, 255);
        cout << "SSN: ";
        cin.getline(ssn, 255);
        cout << "Birthdate (long integer only, sorry.): ";
        cin >> bd;
        cout << "GPA: ";
        cin >> gpa;
        cout << "Grade (1 = Fr, 2 = So, 3 = Jr, 4 = Sr): ";
        cin >> grd;
        cout << "Hours completed: ";
        cin >> hrs;
        S = new LinkStu(name,ssn,bd,gpa,grd,hrs);
        cout << "\nAdding the following:\n";
        S->print();
        A.ins(S);
        while(junk!='\n')
          cin.get(junk);
        junk=0;
        break;
      case '2':
        // Add to set B
        cout << "Add a student to set B\n";
        cout << "Name: ";
        cin.getline(name, 255);
        cout << "SSN: ";
        cin.getline(ssn, 255);
        cout << "Birthdate (long integer only, sorry.): ";
        cin >> bd;
        cout << "GPA: ";
        cin >> gpa;
        cout << "Grade (1 = Fr, 2 = So, 3 = Jr, 4 = Sr): ";
        cin >> grd;
        cout << "Hours completed: ";
        cin >> hrs;
        S = new LinkStu(name,ssn,bd,gpa,grd,hrs);
        cout << "\nAdding the following:\n";
        S->print();
        B.ins(S);
        while(junk!='\n')
          cin.get(junk);
        junk=0;
        break;
      case '3':
        cout << "Delete a student from set A\n";
        cout << "SSN: ";
        cin.getline(ssn, 255);
        if(A.del(ssn))
          cout << "Deleted\n";
        else
          cout << "Not found\n";
        break;
      case '4':
        cout << "Delete a student from set B\n";
        cout << "SSN: ";
        cin.getline(ssn, 255);
        if(B.del(ssn))
          cout << "Deleted\n";
        else
          cout << "Not found\n";
        break;
      case '5':
        cout << "Check membership in set A\n";
        cout << "SSN: ";
        cin.getline(ssn, 255);
        if(A.is_member(ssn))
          cout << "Found.\n";
        else
          cout << "Not found\n";
        break;
      case '6':
        cout << "Check membership in set B\n";
        cout << "SSN: ";
        cin.getline(ssn, 255);
        if(B.is_member(ssn))
          cout << "Found.\n";
        else
          cout << "Not found\n";
        break;
      case '7':
        // Print A
        cout << "Print set A\n";
        A.printset();
        break;
      case '8':
        // Print B
        cout << "Print set B\n";
        B.printset();
        break;
      case '9':
        cout << "Print the union of A and B\n";
        C = A || B;
        C->printset();
        delete C;
        break;
      case '0':
        cout << "Print the intersection of A and B\n";
        C = A && B;
        C->printset();
        delete C;
        break;
      default:
        cout << "** Invalid command.\n\n";
        cout << " 1) Add a student to set A\n";
        cout << " 2) Add a student to set B\n";
        cout << " 3) Delete a student from set A\n";
        cout << " 4) Delete a student from set B\n";
        cout << " 5) Check membership in set A\n";
        cout << " 6) Check membership in set B\n";
        cout << " 7) Print set A\n";
        cout << " 8) Print set B\n";
        cout << " 9) Print the union of A and B\n";
        cout << " 0) Print the intersection of A and B\n";
        cout << " Q) Exit this program\n";
        break;
    }
  }
}

